evoapp.controller('MainController', function($rootScope, $scope, $timeout, $compile, $injector, $filter, globals) { var me = this; $scope.storeUserSession = new globals.dataService.store({ model: null, actions: { get: 'checkLogin', destroy: 'doLogout' } }); $scope.storeUserSession.broadcastEvents = true; //will contain all user session info ... $rootScope.UserSession = null; //will contain all the application HTML $scope.viewCache = null; //the last module rendered by method renderModule. $scope.moduleRendered = null; $scope.idCounter = 0; $scope.currentModuleName = null; //loaded modules in main view $scope.loadedModules = []; //current module in view $scope.currentModule = null; $scope.menuItems = []; //if true, indicates a session timeout $scope.sessionTimeout = false; $scope.beforeSessionTimeoutModule = null; //this div will contain all modules currently not in view var divTemp = $('
'); //MAIN APP ENTRANCE $scope.boot = function(){ $scope.divMainPanel = $('body #main-panel'); $scope.storeUserSession.get(function(response, status, headers, config, items){ if(response.success) { //TODO: repeated code ??? $('#main-panel').show(); //globals.plugins.setDefaults(); //globals.plugins.setBootstrap(); $rootScope.$broadcast('loadingStatusInactive'); $rootScope.$emit('onloginsuccessfull', {response: response}); } else { $scope.renderModule('body #main-panel', 'Login', null, true, function(className, scope){ $timeout(function() { //TODO: repeated code ??? $('#main-panel').show(); //globals.plugins.setDefaults(); //globals.plugins.setBootstrap(); $rootScope.$broadcast('loadingStatusInactive'); }); }); } }); }; $rootScope.$on('onloginsuccessfull', function(event, args){ //set user session $rootScope.UserSession = { userName: args.response.data, tiposFichas: globals.staticData.tiposFichas//TODO: this one needs to come from database }; // go to the default module and/or landing page $scope.loadModule({Controller: 'Search'}, function(className, scope, isNewModule){ }); }); //creates/renders a new controller+view into the given placeholder $scope.renderModule = function(placeholder, className, parameters, broadcastCreation, callback){ var view = (className + 'View'), controller = (className + 'Controller'), placeholder = $(placeholder), domEl = $scope.viewCache.find('#' + view); if(placeholder.length == 0) { throw Error('placeholder ' + placeholder.selector + ' not found!'); } if(domEl.length > 1) { throw Error('more than one view ' + view + ' was found!'); } if(domEl.length > 0) { var html = domEl[0].outerHTML, controllerView = $(html); controllerView.attr('ng-controller', controller); controllerView.attr('id', (view + '-' + $scope.idCounter)); $scope.idCounter++; html = controllerView[0].outerHTML; controllerView = $(html); $injector.invoke(function($rootScope, $compile) { var compiled = $compile(controllerView)($scope); placeholder.append(compiled); var scope = angular.element(compiled).scope(); //add view dom el reference scope.domEl = $(controllerView[0]); //¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ //TODO: WE MIGHT NEED TO ITERATE VIEW TO FIND ALL FORM ELEMENTS AND DO THE SAME AS BELLOW: //add view form dom el reference if available - the default is editForm //we cannot be using this fixed form name: editForm !!!??? var editForm = scope.domEl.find('form[name="editForm"]:first'); if(editForm.length > 0 && scope.editForm) { scope.editForm['domEl'] = $(editForm[0]); if(scope.editForm.domEl != undefined && scope.editForm.domEl != null) { //reset form function - clear all fields values scope.editForm['$reset'] = function(){ scope.editForm.domEl.parsley('destroy'); //TODO: should we clear all others fields this way also? //clear all datepicker fields var datePickers = scope.editForm.domEl.find('input[datepicker]'); if(datePickers.length > 0) { $.each(datePickers, function(index, el) { $(el).val(null); }); } }; //is valid function scope.editForm['$isValid'] = function(){ return scope.editForm.domEl.parsley('validate'); }; //add/remove from a specific field all the parsley validations (required) scope.editForm['$removeConstraints'] = function(field, remove){ var fieldDomEl = null; if(angular.isString(field)) { fieldDomEl = scope.domEl.find('[name="' + field + '"]:first'); } else { fieldDomEl = field; } if(fieldDomEl.length > 0) { var fieldEl = $(fieldDomEl[0]); scope.editForm.domEl.parsley('removeItem', fieldEl); if(!remove) { scope.editForm.domEl.parsley('addItem', fieldEl); fieldEl.parsley('addConstraint', {'required': true }); } } }; scope.editForm['$setDisabled'] = function(disabled){ var fields = scope.editForm.domEl.find('input, select, textarea'); angular.forEach(fields, function (fieldEl) { var field = $(fieldEl); if(disabled) { field.prop('disabled', true); } else { field.prop('disabled', false); } }); }; //add names to fields who do not have one var fields = scope.editForm.domEl.find('input[type=checkbox], input[type=radio]'); if(fields.length > 0) { var fieldNameCounter = 0; angular.forEach(fields, function (fieldEl) { var field = $(fieldEl), name = field.prop('name'), type = field.attr('type'); if(name == '') { var newFieldName = (type + '-' + fieldNameCounter); field.prop('name', newFieldName); fieldNameCounter++; } }); } } } //pass parameters to controller, if available. if(parameters != null) { //new controller scope angular.extend(scope, parameters); } //set control class name scope.ClassName = className; //notify listeners that a new controller as been created if(broadcastCreation) { $scope.moduleRendered = scope; } //broadcast afterrender event scope.$broadcast('afterrender'); //¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ //Inject plugins, etc... globals.plugins.setBootstrap(); //mantain the correct hashtag window.location.hash = '#/' + className; $timeout(function() { controllerView.removeClass('hidden'); //execute callback if available if(callback != undefined) { globals.utils.passArguments(callback, [className, scope]); } }); }); } else { throw Error('Controller/View ' + view + ' not found!'); } }; //load a module into view or render it if it never as been loaded $scope.loadModule = function(subModule, callback, configs){ $timeout(function() { var module = $scope.getModule(subModule.Controller); var isNewModule = module == null; //render module if not rendered yet... if(module == null) { $scope.renderModule('body #main-panel', subModule.Controller, (configs || null), true, function(className, scope){ $("body, html").animate({scrollTop : 0}, 0); //execute callback if available if(callback != undefined) { globals.utils.passArguments(callback, [className, scope, isNewModule]); } }); } else { //mantain the correct hashtag window.location.hash = '#/' + module.ClassName; $scope.showModule(module); $("body, html").animate({scrollTop : 0}, 0); //execute callback if available if(callback != undefined) { globals.utils.passArguments(callback, [module.Controller, module, isNewModule]); } } $scope.currentModuleName = subModule.Controller; }); }; $scope.showModule = function(module){ if($scope.currentModule != null) { //remove current module from main-panel $scope.currentModule.domEl.appendTo(divTemp); } $scope.currentModule = module; var isInMainPanel = ($scope.divMainPanel.find(module.domEl).length > 0); if(!isInMainPanel) { //add current module to main-panel if not there yet module.domEl.appendTo($scope.divMainPanel); } } $scope.getModule = function(className){ var module = _.where($scope.loadedModules, {ClassName: className}); return module.length > 0 ? module[0] : null; }; //TODO: REMOVE TOP MENU LOGIC FROM HERE!!! $scope.buildTopMenu = function(){ $scope.menuItems = [ { Name: 'Form 1', Controller: 'Form1', SubMenuItems: [] }, { Name: 'Form2', Controller: 'Form2', SubMenuItems: [] } ]; }; $scope.selectMenuItem = function(item){ if(item.Controller != null) { $scope.loadModule(item, function(className, scope, isNewModule){ }); } }; // executes a logout from the application and perfomrs cleanup operations $scope.logout = function(){ globals.message.confirm({ question: 'Tem a certeza que deseja sair?', answer: function(result){ if(result) { $rootScope.$broadcast('loadingStatusActive'); $scope.storeUserSession.destroy(null, function(response){ $rootScope.$broadcast('loadingStatusInactive'); if(response.success) { $scope.clearSession(); $scope.loadModule({Controller: 'Login'}, function(className, scope, isNewModule){ }); } else { } }); } } }); }; // cleanup all session objects $scope.clearSession = function(){ $rootScope.UserSession = null; $scope.currentModuleName = null; $scope.loadedModules = []; $scope.currentModule = null; divTemp.empty(); $('#main-panel').empty(); }; $rootScope.$on('loadingStatusActive', function(event, args){ $('body').modalmanager('loading'); }); $rootScope.$on('loadingStatusInactive', function(event, args){ $('body').modalmanager('removeLoading'); //this on restores the body scrollbar //$('body').removeClass('modal-open'); }); //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ // SERVER RESPONSE $scope.FirstMessage = null; $scope.MessageCount = 0; $scope.isTimerRunning = false; $scope.ServerResponse = []; $scope.notification = null; $scope.defaultExceptionMessage = { success: false, data: null, messages: [ { type: 'ERROR', messageData: 'Ocorreu um erro de sistema.' } ] }; $rootScope.$on('response', function(event, args){ var response = args.response, msgCss = 'default'; if(response.data == undefined || response.messages == undefined) { response = $scope.defaultExceptionMessage; } if(response != undefined && response != null && response.messages.length > 0) { $scope.ServerResponse = response; $scope.FirstMessage = response.messages[0]; msgCss = globals.utils.getMessageTypeCSS($scope.FirstMessage.type); $scope.MessageCount = response.messages.length; } if($scope.notification != null) { $scope.notification.close(); } $scope.notification = noty({ layout: 'topRight', text: $scope.FirstMessage.messageData, maxVisible: 1, template: '', timeout: 6000, animation: { open: {opacity: 'toggle'}, close: {opacity: 'toggle'}, easing: 'swing', speed: 125 }, closeWith: ['button'], }); $compile($scope.notification.$bar[0])($scope); }); $scope.viewServerResponse = function(){ if($scope.notification != null) { $scope.notification.close(); } //TODO: show all messages modal window ? //$scope.windowViewServerResponse.show(); //$scope.windowViewServerResponse.viewController.list($scope.ServerResponse); }; $rootScope.$on('onsessiontimeout', function(event, args){ var eee = args.response; $scope.sessionTimeout = true; $scope.beforeSessionTimeoutModule = $scope.currentModule; $scope.loadModule({Controller: 'Login'}, function(className, scope, isNewModule){ }); }); //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ //fires whenever a new module is rendered, if broadcastCreation is true on method renderModule $scope.$watch('moduleRendered', function(newVal, oldVal){ if(newVal != null) { var module = $scope.getModule(newVal.ClassName); if(module == null) { $scope.loadedModules.push(newVal); $scope.showModule(newVal); $scope.currentModule = newVal; } else { $scope.currentModule = module; } } }); });