forked from Coded/SIPRP
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
481 lines
16 KiB
481 lines
16 KiB
evoapp.controller('MainController', function($rootScope, $scope, $timeout, $compile, $injector, $filter, globals)
|
|
{
|
|
var me = this;
|
|
|
|
//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 = [];
|
|
|
|
//this div will contain all modules currently not in view
|
|
var divTemp = $('<div></div>');
|
|
|
|
//MAIN APP ENTRANCE
|
|
$scope.boot = function(){
|
|
|
|
$scope.divMainPanel = $('body #main-panel');
|
|
|
|
$scope.renderModule('body #main-panel', 'Login', null, true, function(className, scope){
|
|
|
|
$timeout(function() {
|
|
|
|
$('#main-panel').show();
|
|
|
|
//globals.plugins.setDefaults();
|
|
//globals.plugins.setBootstrap();
|
|
|
|
$rootScope.$broadcast('loadingStatusInactive');
|
|
});
|
|
});
|
|
|
|
//$scope.buildTopMenu();
|
|
|
|
|
|
};
|
|
|
|
$rootScope.$on('onloginsuccessfull', function(event, args){
|
|
|
|
$scope.loadModule({Controller: 'Search'}, function(className, scope, isNewModule){
|
|
|
|
});
|
|
|
|
//$scope.renderModule('body #main-panel', 'Search', null, true, function(className, scope){
|
|
|
|
// $timeout(function() {
|
|
|
|
// });
|
|
//});
|
|
|
|
//var form1 = $scope.getModule('Form1');
|
|
|
|
////load default module: Form1
|
|
//$scope.renderModule('body #main-panel', 'Form1', null, true, function(className, scope){
|
|
|
|
// $timeout(function() {
|
|
|
|
// //$('#main-panel').show();
|
|
|
|
// //globals.plugins.setDefaults();
|
|
// //globals.plugins.setBootstrap();
|
|
|
|
// //$rootScope.$broadcast('loadingStatusInactive');
|
|
// });
|
|
//});
|
|
});
|
|
|
|
//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){
|
|
|
|
//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);
|
|
|
|
//execute callback if available
|
|
if(callback != undefined)
|
|
{
|
|
globals.utils.passArguments(callback, [module.Controller, module, isNewModule]);
|
|
}
|
|
}
|
|
|
|
$scope.currentModuleName = subModule.Name;
|
|
});
|
|
};
|
|
|
|
$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){
|
|
|
|
});
|
|
}
|
|
};
|
|
|
|
$scope.logout = function(){
|
|
|
|
globals.message.confirm({
|
|
question: 'Tem a certeza que deseja sair?',
|
|
answer: function(result){
|
|
|
|
if(result)
|
|
{
|
|
$scope.loadModule({Controller: 'Login'}, function(className, scope, isNewModule){
|
|
|
|
});
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
$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;
|
|
|
|
$rootScope.$on('response', function(event, args){
|
|
|
|
//var response = args.response.response.ServerResponse;
|
|
|
|
//if(response != undefined && response != null && response.length > 0)
|
|
//{
|
|
// $scope.ServerResponse = response;
|
|
|
|
// $scope.FirstMessage = response[0];
|
|
|
|
// $scope.FirstMessage.MessageType = globals.utils.getMessageTypeCSS($scope.FirstMessage.MessageType);
|
|
|
|
// $scope.MessageCount = response.length;
|
|
|
|
// //TODO: etc...
|
|
//}
|
|
|
|
if($scope.notification != null)
|
|
{
|
|
$scope.notification.close();
|
|
}
|
|
|
|
$scope.notification = noty({
|
|
layout: 'topRight',
|
|
text: 'your message goes here',
|
|
maxVisible: 1,
|
|
template:
|
|
'<div ng-click="viewServerResponse()" class="bg-' + 'success' + ' noty_message">' +
|
|
'<span ng-show="MessageCount > 1" class="label label-default pull-left noty-label">+{{MessageCount-1}}</span>' +
|
|
'<span class="noty_text"></span>' +
|
|
'<div class="noty_close"></div>' +
|
|
'</div>',
|
|
timeout: 6000,
|
|
animation: {
|
|
open: {opacity: 'toggle'},
|
|
close: {opacity: 'toggle'},
|
|
easing: 'swing',
|
|
speed: 125
|
|
},
|
|
closeWith: ['button'],
|
|
});
|
|
|
|
$compile($scope.notification.$bar[0])($scope);
|
|
});
|
|
|
|
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
|
|
|
|
//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;
|
|
}
|
|
}
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|