From c854d6c9189298487cd698934f96be28b1bc9bad Mon Sep 17 00:00:00 2001 From: PliablePixels Date: Fri, 7 Aug 2015 20:33:53 -0400 Subject: Lots of refactoring - API is not tied into Portal Auth - bugs may need to be ironed out --- www/js/DataModel.js | 6 ++- www/js/LoginCtrl.js | 33 +++++++++++++--- www/js/MontageCtrl.js | 11 ++++-- www/js/PortalLoginCtrl.js | 36 ++++++++++++++++++ www/js/app.js | 95 ++++++++++++++++++++++++++++++++++------------- 5 files changed, 145 insertions(+), 36 deletions(-) create mode 100644 www/js/PortalLoginCtrl.js (limited to 'www/js') diff --git a/www/js/DataModel.js b/www/js/DataModel.js index 695ebd18..a79a6cd0 100644 --- a/www/js/DataModel.js +++ b/www/js/DataModel.js @@ -7,7 +7,7 @@ // that many other controllers use // It's grown over time. I guess I may have to split this into multiple services in the future -angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ionicLoading', '$ionicBackdrop', '$fileLogger', 'zm',function ($http, $q, $ionicLoading, $ionicBackdrop,$fileLogger,zm) { +angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ionicLoading', '$ionicBackdrop', '$fileLogger', 'zm','$rootScope',function ($http, $q, $ionicLoading, $ionicBackdrop,$fileLogger,zm, $rootScope) { var zmAppVersion="unknown"; var monitorsLoaded = 0; @@ -293,6 +293,10 @@ angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ion maxWidth: 200, showDelay: 0 }); + + + + var d = $q.defer(); if ((monitorsLoaded == 0) || (forceReload == 1)) // monitors are empty or force reload { diff --git a/www/js/LoginCtrl.js b/www/js/LoginCtrl.js index 05631b1d..3b5b368b 100644 --- a/www/js/LoginCtrl.js +++ b/www/js/LoginCtrl.js @@ -2,7 +2,7 @@ /* jslint browser: true*/ /* global cordova,StatusBar,angular,console */ -angular.module('zmApp.controllers').controller('zmApp.LoginCtrl', ['$scope', '$rootScope','zm', '$ionicModal', 'ZMDataModel', '$ionicSideMenuDelegate', '$ionicPopup', '$http', '$q', '$ionicLoading', function ($scope, $rootScope,zm, $ionicModal, ZMDataModel, $ionicSideMenuDelegate, $ionicPopup, $http, $q, $ionicLoading) { +angular.module('zmApp.controllers').controller('zmApp.LoginCtrl', ['$scope', '$rootScope','zm', '$ionicModal', 'ZMDataModel', '$ionicSideMenuDelegate', '$ionicPopup', '$http', '$q', '$ionicLoading', 'zmAutoLogin', function ($scope, $rootScope,zm, $ionicModal, ZMDataModel, $ionicSideMenuDelegate, $ionicPopup, $http, $q, $ionicLoading, zmAutoLogin) { $scope.openMenu = function () { $ionicSideMenuDelegate.toggleLeft(); }; @@ -137,7 +137,7 @@ function addhttp(url) { // Let's do a sanity check to see if the URLs are ok - +/* $ionicLoading.show({ template: 'Checking data...', animation: 'fade-in', @@ -145,16 +145,26 @@ function addhttp(url) { duration: zm.loadingTimeout, maxWidth: 200, showDelay: 0 - }); - + });*/ +/* + FIXME: REDO this $q.all([ - $http.get(apiurl), + // $http.get(apiurl), $http.get(portalurl), //$http.get(streamingurl), ]).then( function (results) { $ionicLoading.hide(); + $http.get(apiurl) + .then (function (data) + { + }, + function (err) + { + }); + + $ionicPopup.alert({ title: 'Settings Saved', template: 'Please explore the menu and enjoy zmNinja!' @@ -189,8 +199,19 @@ function addhttp(url) { } - ); + );*/ ZMDataModel.setLogin($scope.loginData); + zmAutoLogin.doLogin("Logging into ZoneMinder") + .then( function(data) + { + + console.log ("THE DATA WAS " + data); + $ionicPopup.alert({ + title: 'Settings Saved', + template: 'Please explore the menu and enjoy zmNinja!' + }).then(function(res) { $ionicSideMenuDelegate.toggleLeft();}); + }); + }; diff --git a/www/js/MontageCtrl.js b/www/js/MontageCtrl.js index 99b6d5b5..4b9ed5be 100644 --- a/www/js/MontageCtrl.js +++ b/www/js/MontageCtrl.js @@ -11,7 +11,8 @@ angular.module('zmApp.controllers').controller('zmApp.MontageCtrl', ['$scope', ' // Controller main //--------------------------------------------------------------------- - + console.log("******** HAVE ALL MONITORS"); + $scope.monitors = message; document.addEventListener("pause", onPause, false); document.addEventListener("resume", onResume, false); @@ -41,8 +42,7 @@ angular.module('zmApp.controllers').controller('zmApp.MontageCtrl', ['$scope', ' $scope.slider = {}; $scope.slider.monsize = ZMDataModel.getMontageSize(); - console.log("******** HAVE ALL MONITORS"); - $scope.monitors = message; + // The difference between old and original is this: // old will have a copy of the last re-arranged monitor list @@ -86,7 +86,7 @@ angular.module('zmApp.controllers').controller('zmApp.MontageCtrl', ['$scope', ' } // at this stage, the monitor arrangement is not matching // the montage order. Its in true order. Let us first process the hiddenOrder part // now - + for (i = 0; i < montageOrder.length; i++) { montageOrder[i] = parseInt(montageOrder[i]); hiddenOrder[i] = parseInt(hiddenOrder[i]); @@ -95,9 +95,12 @@ angular.module('zmApp.controllers').controller('zmApp.MontageCtrl', ['$scope', ' // disabled monitors if (hiddenOrder[i] == 1) { // $scope.monitors[i].Monitor.listDisplay='noshow'; + + if ($scope.monitors[i] !== undefined) $scope.monitors[i].Monitor.listDisplay = 'noshow'; ZMDataModel.zmLog("Monitor " + i + " is marked as hidden in montage"); } else { + if ($scope.monitors[i] !== undefined) $scope.monitors[i].Monitor.listDisplay = 'show'; } } diff --git a/www/js/PortalLoginCtrl.js b/www/js/PortalLoginCtrl.js new file mode 100644 index 00000000..65f92b48 --- /dev/null +++ b/www/js/PortalLoginCtrl.js @@ -0,0 +1,36 @@ +/* jshint -W041 */ +/* jshint -W083 */ +/*This is for the loop closure I am using in line 143 */ +/* jslint browser: true*/ +/* global vis,cordova,StatusBar,angular,console,moment */ +angular.module('zmApp.controllers').controller('zmApp.PortalLoginCtrl', ['$ionicPlatform', '$scope', 'zm', 'ZMDataModel', '$ionicSideMenuDelegate', '$rootScope', '$http', '$q', '$state', '$ionicLoading', '$ionicPopover', '$ionicScrollDelegate', '$ionicModal', '$timeout', 'zmAutoLogin', '$ionicHistory',function ($ionicPlatform, $scope, zm, ZMDataModel, $ionicSideMenuDelegate, $rootScope, $http, $q, $state, $ionicLoading, $ionicPopover, $ionicScrollDelegate, $ionicModal, $timeout, zmAutoLogin, $ionicHistory) { + + // Main + +console.log ("***** INSIDE LOGIN CONTROLLER"); + + $ionicHistory.nextViewOptions({ + disableBack: true + }); + + var loginData = ZMDataModel.getLogin(); + + if (ZMDataModel.isLoggedIn()) { + ZMDataModel.zmLog ("User credentials are provided"); + // console.log("VALID CREDENTIALS. Grabbing Monitors"); + zmAutoLogin.doLogin("Logging into ZoneMinder...") + .then (function(data) + { + + ZMDataModel.getKeyConfigParams(1); + $state.go('montage'); + }); + } + else + { + $state.go('login'); + + } + + + }]); \ No newline at end of file diff --git a/www/js/app.js b/www/js/app.js index 3cf72c73..026da708 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -201,11 +201,19 @@ angular.module('zmApp', [ { var zmSess=cookies.match("ZMSESSID=(.*?);"); - if (zmSess[1]) + + if (zmSess) { - console.log ("***RESPONSE HEADER COOKIE " + zmSess[1]); - console.log ("WHOLE STRING " + cookies); - zmCookie=zmSess[1]; + if (zmSess[1]) + { + //console.log ("***RESPONSE HEADER COOKIE " + zmSess[1]); + console.log ("WHOLE STRING " + cookies); + zmCookie=zmSess[1]; + } + } + else + { + console.log ("WHOLE STRING " + cookies); } } return response; @@ -219,12 +227,29 @@ angular.module('zmApp', [ // This service automatically logs into ZM at periodic intervals //------------------------------------------------------------------ -.factory('zmAutoLogin', function($interval, ZMDataModel, $http,zm, $browser,$timeout) { +.factory('zmAutoLogin', function($interval, ZMDataModel, $http,zm, $browser,$timeout,$q, $rootScope, $ionicLoading) { var zmAutoLoginHandle; - function doLogin() + var d = $q.defer(); + function doLogin(str) { + /* if ($rootScope.loggedIntoZm == 1) + { + d.resolve("Already logged in"); + return (d.promise); + }*/ console.log ("**** ZM AUTO LOGIN CALLED"); - ZMDataModel.zmLog("zmAutologin timer started"); + ZMDataModel.zmLog("zmAutologin called"); + + if (str) + { + $ionicLoading.show({ + template: str, + noBackdrop: true, + duration: zm.httpTimeout + }); + } + + var loginData = ZMDataModel.getLogin(); $http({ method:'POST', @@ -253,27 +278,39 @@ angular.module('zmApp', [ }) .success(function(data,status,headers) { + $ionicLoading.hide(); + + $rootScope.loggedIntoZm = 1; console.log ("**** ZM Login OK"); ZMDataModel.zmLog("zmAutologin successfully logged into Zoneminder"); //$cookies.myFavorite = 'oatmeal'; - $timeout( function() {console.log ("***** ALL COOKIES:" + JSON.stringify( $browser.cookies()));},1000); + //$timeout( function() {console.log ("***** ALL COOKIES:" + JSON.stringify( $browser.cookies()));},1000); console.log ("***** ALL HEADERS:" + headers('cookie')); - + d.resolve("Login Success"); + return (d.promise); + }) .error(function(error) { + $ionicLoading.hide(); + $rootScope.loggedIntoZm = -1; console.log ("**** ZM Login FAILED"); ZMDataModel.zmLog ("zmAutologin Error " + JSON.stringify(error), "error"); + d.resolve("Login Error"); + return d.promise; }); + return d.promise; + } function start() { + $rootScope.loggedIntoZm = 0; $interval.cancel(zmAutoLoginHandle); - doLogin(); + //doLogin(); zmAutoLoginHandle = $interval(function() { - doLogin(); + doLogin(""); },zm.loginInterval); // Auto login every 5 minutes // PHP timeout is around 10 minutes @@ -282,13 +319,15 @@ angular.module('zmApp', [ function stop() { $interval.cancel(zmAutoLoginHandle); + $rootScope.loggedIntoZm = 0; ZMDataModel.zmLog("Cancelling zmAutologin timer"); } return { start: start, - stop: stop + stop: stop, + doLogin: doLogin }; }) @@ -333,6 +372,7 @@ angular.module('zmApp', [ $rootScope.toTime=""; $rootScope.fromString=""; $rootScope.toString=""; + $rootScope.loggedIntoZm = 0; ZMDataModel.init(); // for making sure we canuse $state.go with ng-click @@ -342,13 +382,7 @@ angular.module('zmApp', [ var loginData = ZMDataModel.getLogin(); - if (ZMDataModel.isLoggedIn()) { - ZMDataModel.zmLog ("User is logged in"); - console.log("VALID CREDENTIALS. Grabbing Monitors"); - ZMDataModel.getMonitors(0); - ZMDataModel.getKeyConfigParams(1); - - } + // This code takes care of trapping the Android back button // and takes it to the menu. @@ -512,7 +546,7 @@ angular.module('zmApp', [ // lets POST so we get a session ID right hre - //console.log ("Setting up POST LOGIN timer"); + console.log ("Setting up POST LOGIN timer"); zmAutoLogin.start(); }) //run @@ -651,16 +685,26 @@ angular.module('zmApp', [ templateUrl: "templates/log.html", controller: 'zmApp.LogCtrl', }) + + .state('zm-portal-login', { + data: { + requireLogin: false + }, + url: "/zm-portal-login", + templateUrl: "templates/zm-portal-login.html", + controller: 'zmApp.PortalLoginCtrl', + }) .state('montage', { data: { requireLogin: true }, resolve: { - message: function (ZMDataModel) { - console.log("Inside app.montage resolve"); + message: function (ZMDataModel) { + console.log("Inside app.events resolve"); return ZMDataModel.getMonitors(0); - } + } + }, url: "/montage", templateUrl: "templates/montage.html", @@ -671,7 +715,7 @@ angular.module('zmApp', [ // if none of the above states are matched, use this as the fallback - var defaultState = "/montage"; + var defaultState = "/zm-portal-login"; //var defaultState = "/login"; // as it turns out I can't really inject a factory in config the normal way @@ -684,7 +728,8 @@ angular.module('zmApp', [ $urlRouterProvider.otherwise(function ($injector, $location) { var $state = $injector.get("$state"); - $state.go("montage"); + console.log ("** PORTAL LOGIN STATE"); + $state.go("zm-portal-login"); }); }); //config -- cgit v1.2.3