"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .controller('ChatHistoryController', ['$scope', 'chatModel', 'userModel', function ($scope, chatModel, userModel) { $scope.state = { loadingUserHistory: true, searchingConversations: false, loadingConversation: false }; $scope.searchResults = []; $scope.selectedConv = null; $scope.currentUser = userModel.userFullData || userModel.userInfo; $scope.selectConversation = function (conv) { if (conv.messages) { $scope.selectedConv = conv; } else { $scope.state.loadingConversation = true; chatModel.getArchivedConversationDetails(conv).then(function (messagesList) { conv.messages = messagesList; $scope.selectedConv = conv; }) .finally(function () { $scope.state.loadingConversation = false; }); } }; $scope.chatModel = chatModel; $scope.filterHistoryList = function (historyItem) { if (!$scope.searchFor || $scope.searchFor.length < 2) { return true; } var parent = historyItem.parent, roster = historyItem.roster; var parentMatch = parent ? !!testHistoryItem(parent.displayId + '_' + (parent.summary || '') + '_' + (parent.desc || '')) : false, userMatch = roster ? !!_.find(roster, testHistoryItem) : false; return parentMatch || userMatch; }; function init() { chatModel.getConversationsHistory() .then(function (convList) { $scope.searchResults = convList.arr; $scope.state.loadingUserHistory = false; }); } function testHistoryItem(item) { if (!_.isString(item) && !_.isObject(item)) { return false; } var reg = new RegExp($scope.searchFor, 'gi'); if (_.isString(item)) { return reg.test(item); } for (var key in item) { if (item.hasOwnProperty(key) && key.indexOf('Name') >= 0) { if (reg.test(item[key]) && _.isString(item[key])) { return true; } } } return false; } var removeWatcher = $scope.$watch('chatModel.connected', function (newVal) { if (newVal) { init(); removeWatcher(); } }); //.format('MMMM Do YYYY, h:mm:ss a'); } ]); })();