From 0452b7728e679f2a828f57a1f4d2725972b3b1db Mon Sep 17 00:00:00 2001 From: root Date: Sat, 27 Aug 2016 22:46:06 +0200 Subject: Added Global Configuration function for monitors Former-commit-id: a088873ff83c5ae7bdd6cfcb2d972b3199833384 --- www/js/MonitorCtrl.js | 168 ++++++++++++++++++++++++++++---------------------- 1 file changed, 94 insertions(+), 74 deletions(-) (limited to 'www/js') diff --git a/www/js/MonitorCtrl.js b/www/js/MonitorCtrl.js index ce6b7255..885796f0 100644 --- a/www/js/MonitorCtrl.js +++ b/www/js/MonitorCtrl.js @@ -6,8 +6,8 @@ // refer to comments in EventCtrl for the modal stuff. They are almost the same angular.module('zmApp.controllers') - .controller('zmApp.MonitorCtrl', ['$ionicPopup', 'zm', '$scope', 'ZMDataModel', 'message', '$ionicSideMenuDelegate', '$ionicLoading', '$ionicModal', '$state', '$http', '$rootScope', '$timeout', '$ionicHistory', '$ionicPlatform', '$translate', - function ($ionicPopup, zm, $scope, ZMDataModel, message, $ionicSideMenuDelegate, $ionicLoading, $ionicModal, $state, $http, $rootScope, $timeout, $ionicHistory, $ionicPlatform, $translate) { + .controller('zmApp.MonitorCtrl', ['$ionicPopup', 'zm', '$scope', 'ZMDataModel', 'message', '$ionicSideMenuDelegate', '$ionicLoading', '$ionicModal', '$state', '$http', '$rootScope', '$timeout', '$ionicHistory', '$ionicPlatform', '$translate', '$q', + function ($ionicPopup, zm, $scope, ZMDataModel, message, $ionicSideMenuDelegate, $ionicLoading, $ionicModal, $state, $http, $rootScope, $timeout, $ionicHistory, $ionicPlatform, $translate, $q) { //----------------------------------------------------------------------- @@ -86,6 +86,17 @@ angular.module('zmApp.controllers') //console.log("called with " + monitorId + ":" + enabled + ":" + func); if (enabled == '1') checked = true; + //if monitorId is not specified, all monitors will be changed + var monitorsIds = new Array(); + if (monitorId == '') { + for (var i = 0; i < $scope.monitors.length; i++) { + monitorsIds[i] = $scope.monitors[i].Monitor.Id; + } + } + else { + monitorsIds[0] = monitorId; + } + $scope.monFunctions = [ { text: $translate.instant('kMonModect'), @@ -114,8 +125,11 @@ angular.module('zmApp.controllers') ]; $scope.monfunc = { + mymonitorsIds: monitorsIds, myfunc: func, - myenabled: checked + myenabled: checked, + myfailedIds: new Array(), + mypromises: new Array() }; $rootScope.zmPopup = $ionicPopup.show({ @@ -133,87 +147,93 @@ angular.module('zmApp.controllers') { text: $translate.instant('kButtonSave'), onTap: function (e) { + $scope.monfunc.mymonitorsIds.forEach( function(item, index) { + ZMDataModel.zmDebug("MonitorCtrl:changeConfig selection:" + $scope.monfunc.myenabled + + $scope.monfunc.myfunc); + var loginData = ZMDataModel.getLogin(); + var apiRestart = loginData.apiurl + "/states/change/restart.json"; + var apiMon = loginData.apiurl + "/monitors/" + item + ".json"; + + ZMDataModel.zmDebug("MonitorCtrl: URLs for changeConfig save:" + apiMon); + + var isEnabled = ""; + isEnabled = ($scope.monfunc.myenabled == true) ? '1' : '0'; + + $ionicLoading.show({ + template: $translate.instant('kApplyingChanges') + "...", + noBackdrop: true, + duration: zm.largeHttpTimeout, + }); - ZMDataModel.zmDebug("MonitorCtrl:changeConfig selection:" + $scope.monfunc.myenabled + - $scope.monfunc.myfunc); - var loginData = ZMDataModel.getLogin(); - var apiRestart = loginData.apiurl + "/states/change/restart.json"; - var apiMon = loginData.apiurl + "/monitors/" + monitorId + ".json"; - - ZMDataModel.zmDebug("MonitorCtrl: URLs for changeConfig save:" + apiMon); - - var isEnabled = ""; - isEnabled = ($scope.monfunc.myenabled == true) ? '1' : '0'; - - $ionicLoading.show({ - template: $translate.instant('kApplyingChanges') + "...", - noBackdrop: true, - duration: zm.largeHttpTimeout, - }); - - $http({ - url: apiMon, - method: 'post', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - 'Accept': '*/*', - }, - transformRequest: function (obj) { - var str = []; - for (var p in obj) - str.push(encodeURIComponent(p) + "=" + - encodeURIComponent(obj[p])); - var foo = str.join("&"); - // console.log("****RETURNING " + foo); - ZMDataModel.zmDebug("MonitorCtrl: parmeters constructed: " + foo); - return foo; - }, - data: { - 'Monitor[Function]': $scope.monfunc.myfunc, - 'Monitor[Enabled]': isEnabled, - } - - }) - - // I am not restarting ZM after monitor change + var httpPromise = $http({ + url: apiMon, + method: 'post', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Accept': '*/*', + }, + transformRequest: function (obj) { + var str = []; + for (var p in obj) + str.push(encodeURIComponent(p) + "=" + + encodeURIComponent(obj[p])); + var foo = str.join("&"); + // console.log("****RETURNING " + foo); + ZMDataModel.zmDebug("MonitorCtrl: parmeters constructed: " + foo); + return foo; + }, + data: { + 'Monitor[Function]': $scope.monfunc.myfunc, + 'Monitor[Enabled]': isEnabled, + } - .success(function () { - $ionicLoading.hide(); + }) + .success(function () { ZMDataModel.zmDebug("MonitorCtrl: Not restarting ZM - Make sure you have the patch installed in MonitorsController.php or this won't work"); - doRefresh(); - /* ZMDataModel.zmDebug ("MonitorCtrl: Restarting ZM"); - $ionicLoading.show({ - template: "Successfully changed Monitor. Please wait, restarting ZoneMinder...", - noBackdrop: true, - duration: zm.largeHttpTimeout, - }); - $http.post(apiRestart) - .then(function (success) { - $ionicLoading.hide(); - var refresh = ZMDataModel.getMonitors(1); - refresh.then(function (data) { - $scope.monitors = data; - $scope.$broadcast('scroll.refreshComplete'); - }); - - }, - function (error) { - $ionicLoading.hide(); - - } - );*/ - }) .error(function (data, status, headers, config) { ZMDataModel.zmDebug("MonitorCtrl: Error changing monitor " + JSON.stringify(data)); - $ionicLoading.hide(); + $scope.monfunc.myfailedIds.push(item); + }); + + $scope.monfunc.mypromises.push(httpPromise); + }); + + $q.all($scope.monfunc.mypromises).then(function(e) { + $ionicLoading.hide(); + // if there's a failed ID, an error has occurred + if ($scope.monfunc.myfailedIds.length != 0) { $ionicLoading.show({ - template: $translate.instant('kErrorChangingMonitors') + "...", + template: $translate.instant('kErrorChangingMonitors') + ". Monitor IDs : " + $scope.monfunc.myfailedIds.toString(), noBackdrop: true, duration: 3000, }); - }); - + } + else { + // I am not restarting ZM after monitor change + /* ZMDataModel.zmDebug ("MonitorCtrl: Restarting ZM"); + $ionicLoading.show({ + template: "Successfully changed Monitor. Please wait, restarting ZoneMinder...", + noBackdrop: true, + duration: zm.largeHttpTimeout, + }); + $http.post(apiRestart) + .then(function (success) { + $ionicLoading.hide(); + var refresh = ZMDataModel.getMonitors(1); + refresh.then(function (data) { + $scope.monitors = data; + $scope.$broadcast('scroll.refreshComplete'); + }); + + }, + function (error) { + $ionicLoading.hide(); + + });*/ + doRefresh(); + } + }); } -- cgit v1.2.3 From 5e7d231a702b087542da7e4d19534ae9b375a5ca Mon Sep 17 00:00:00 2001 From: Pliable Pixels Date: Sat, 27 Aug 2016 19:01:51 -0400 Subject: eliminate JSHint 'prefer array literal notation' warning and use kAll Former-commit-id: 3c2665b549b7de527f6a00ed06112897f707b8b9 --- www/js/MonitorCtrl.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'www/js') diff --git a/www/js/MonitorCtrl.js b/www/js/MonitorCtrl.js index 885796f0..e56ae47d 100644 --- a/www/js/MonitorCtrl.js +++ b/www/js/MonitorCtrl.js @@ -83,11 +83,17 @@ angular.module('zmApp.controllers') //----------------------------------------------------------------------- $scope.changeConfig = function (monitorName, monitorId, enabled, func) { var checked = false; + + if (monitorName == 'All') + { + monitorName = $translate.instant('kAll'); + } + //console.log("called with " + monitorId + ":" + enabled + ":" + func); if (enabled == '1') checked = true; //if monitorId is not specified, all monitors will be changed - var monitorsIds = new Array(); + var monitorsIds = []; if (monitorId == '') { for (var i = 0; i < $scope.monitors.length; i++) { monitorsIds[i] = $scope.monitors[i].Monitor.Id; @@ -128,8 +134,8 @@ angular.module('zmApp.controllers') mymonitorsIds: monitorsIds, myfunc: func, myenabled: checked, - myfailedIds: new Array(), - mypromises: new Array() + myfailedIds: [], + mypromises: [] }; $rootScope.zmPopup = $ionicPopup.show({ -- cgit v1.2.3