<commit_before>/*

Copyright 2015 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

'use strict';

/**
 * @ngdoc function
 * @name consoleApp.controller:DeviceconsoleCtrl
 * @description
 * # DeviceconsoleCtrl
 * Controller of the consoleApp
 */

//  http://plnkr.co/edit/VUkeNvzmOLVYVjKfsCiy?p=preview


angular.module('consoleApp')
  .controller('DeviceconsoleCtrl', function ($scope, $rootScope, $timeout, $routeParams, $location, projectService, httpRequestService) {
    
 
    Utils.controllerhelpers.getProject($scope, $routeParams, projectService);

    var projectListener = $rootScope.$on('project', function (event, data) {
      $scope.init();
    });

    $scope.$on('$destroy', projectListener);

    $scope.$on('$routeChangeSuccess', function(event) {
      $scope.init();
    });

    $scope.init = function(){
      $scope.emptyEvents();
      $scope.initSelectedBoard();
      $scope.initCommands();
    };


    /*
    * BEGIN
    * BOARD SELECTION MANAGEMENT
    */
    $scope._selectedboard = undefined;

    $scope.initSelectedBoard = function(){
      if ($routeParams.boardid && $scope.project.data.boards) {

        $scope._selectedboard = $scope.project.data.boards.find(function(board){
          return board.ID === $routeParams.boardid;
        });
        $scope.selectBoard();
      };
    };

    Object.defineProperty($scope, "selectedboard", {
      get: function () { 
        return $scope._selectedboard;
      },
      set: function (board) { 
        var pathparts = $location.path().split("/");

        //change in location will trigger routeChangeSuccess and in turn call selectBoard()
        $location.path('/' + pathparts[1] + '/' + pathparts[2] + '/' + board.ID);
      },
    });

    $scope.selectBoard = function() {
      var jid = $scope._selectedboard.jid;

      // Adjust for JID inconsistency
      if (jid && jid.substring(jid.length - 3) != "/WI" && jid.substring(0,2) == "wi") {
        jid += '/WI';
      } else if (jid && jid.substring(jid.length - 3) != "/IB") {
        jid += '/IB';
      }

      $scope.pollPresenceForBoard();

      $scope.loadLastMessages();

    }

    $scope.boardisSelected = function(){
      return $scope.selectedboard;
    };
    
    /*
    * END
    * BOARD SELECTION MANAGEMENT
    */




    /*
    * BEGIN
    * MESSAGE MANAGEMENT
    */

    $scope.filter = {
      numMessages: 10,
      queryJson: undefined 
    }

    $scope.emptyEvents = function(){
      $scope.events = [];     
    };

    $scope.messagetoJSON = function(message){
      return messagetoJSON(message);
    };    

    var myListener = $rootScope.$on('events', function (event, data) {
      console.log('events');
      data.msgs.forEach(function(item){
        if ($scope.boardisSelected() && item.from.indexOf($scope.selectedboard.jid) > -1) {
          $scope.appendMessage(item);
        }
        $scope.$apply();
      });      
    });
    $scope.$on('$destroy', myListener);

    $scope.appendMessage = function(message) {
      var el = null;

      function toDateString(dateInMs) {
        if (!dateInMs) {
          return "0000-00-00T00:00:00";
        }
        return new Date(dateInMs).toLocalISOString().split(/[\.\+]/)[0];<commit_msg>fixed scrolltop for message panel<commit_after>/*

Copyright 2015 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

'use strict';

/**
 * @ngdoc function
 * @name consoleApp.controller:DeviceconsoleCtrl
 * @description
 * # DeviceconsoleCtrl
 * Controller of the consoleApp
 */

//  http://plnkr.co/edit/VUkeNvzmOLVYVjKfsCiy?p=preview


angular.module('consoleApp')
  .controller('DeviceconsoleCtrl', function ($scope, $rootScope, $timeout, $routeParams, $location, projectService, httpRequestService) {
    
 
    Utils.controllerhelpers.getProject($scope, $routeParams, projectService);

    var projectListener = $rootScope.$on('project', function (event, data) {
      $scope.init();
    });

    $scope.$on('$destroy', projectListener);

    $scope.$on('$routeChangeSuccess', function(event) {
      $scope.init();
    });

    $scope.init = function(){
      $scope.emptyEvents();
      $scope.initSelectedBoard();
      $scope.initCommands();
    };


    /*
    * BEGIN
    * BOARD SELECTION MANAGEMENT
    */
    $scope._selectedboard = undefined;

    $scope.initSelectedBoard = function(){
      if ($routeParams.boardid && $scope.project.data.boards) {

        $scope._selectedboard = $scope.project.data.boards.find(function(board){
          return board.ID === $routeParams.boardid;
        });
        $scope.selectBoard();
      };
    };

    Object.defineProperty($scope, "selectedboard", {
      get: function () { 
        return $scope._selectedboard;
      },
      set: function (board) { 
        var pathparts = $location.path().split("/");

        //change in location will trigger routeChangeSuccess and in turn call selectBoard()
        $location.path('/' + pathparts[1] + '/' + pathparts[2] + '/' + board.ID);
      },
    });

    $scope.selectBoard = function() {
      var jid = $scope._selectedboard.jid;

      // Adjust for JID inconsistency
      if (jid && jid.substring(jid.length - 3) != "/WI" && jid.substring(0,2) == "wi") {
        jid += '/WI';
      } else if (jid && jid.substring(jid.length - 3) != "/IB") {
        jid += '/IB';
      }

      $scope.pollPresenceForBoard();

      $scope.loadLastMessages();

    }

    $scope.boardisSelected = function(){
      return $scope.selectedboard;
    };
    
    /*
    * END
    * BOARD SELECTION MANAGEMENT
    */




    /*
    * BEGIN
    * MESSAGE MANAGEMENT
    */

    $scope.filter = {
      numMessages: 10,
      queryJson: undefined 
    }

    $scope.emptyEvents = function(){
      $scope.events = [];     
    };

    $scope.messagetoJSON = function(message){
      return messagetoJSON(message);
    };    

    var myListener = $rootScope.$on('events', function (event, data) {
      console.log('events');
      data.msgs.forEach(function(item){
        if ($scope.boardisSelected() && item.from.indexOf($scope.selectedboard.jid) > -1) {
          $scope.appendMessage(item);
        }
        $scope.$apply();

        // #messages-panel > div.panel-body > div
        var msgpanel = $('#messages-panel > div.panel-body > div');
        msgpanel.scrollTop(msgpanel[0].scrollHeight);
      });      
    });
    $scope.$on('$destroy', myListener);

    $scope.appendMessage = function(message) {
      var el = null;

      function toDateString(dateInMs) {
        if (!dateInMs) {
          return "0000-00-00T00:00:00";
        }
        return new Date(dateInMs).toLocalISOString().split(/[\.\+]/)[0];
{'fraction_non_alphanumeric': 0.14463840399002495, 'fraction_numerical': 0.008531303320645754, 'mean_word_length': 2.6511739338763776, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2371395', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2261, 'n_words': 512}