angular-masterrow
Version:
The plugin necessary to use MasterRow tool in AngularJS.
134 lines (102 loc) • 2.98 kB
JavaScript
/*
angular-masterrow
Author: Amós Batista
*/
angular.module('angular-masterrow', [])
.directive('masterrow', function(){
return {
restrict: 'E',
link: function (scope, element, attrs){
// Generate a random ID code, to allow more than one table.
scope.uniqueID = 'mrContainer_' + Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
// Set the element ID, BEFORE start the Masterrow
element[0].id = scope.uniqueID;
// Options object, to adequate old version of Angular
if(scope.mrOptions != undefined){
scope.masterrowOptions = scope.mrOptions;
scope.masterrowOptions.containerId = scope.uniqueID;
}
else{
scope.masterrowOptions = {
containerId: scope.uniqueID,
filters: scope.mrFilters,
pagination: scope.mrPagination,
detailing: scope.mrDetailing,
personalization: scope.mrPersonalization,
rowAction: scope.mrRowAction,
sorting: scope.mrSorting,
exporting: scope.mrExporting,
toProcessData: scope.mrToProcessData
}
}
// Directive parameters from caller
var table = new MasterRow(scope.masterrowOptions);
// Masterrow reload function
var reload = function(){
table.pageLoadEventCheck ();
}
// The time directive is loaded, process the function
table.toProcessData (scope.masterrowOptions.toProcessData);
/* Event handler. */
/*When it receives the broadcast from "angularMasterrowService" service,
execute the function again*/
scope.$on('executeFunctionHandler', function(){
table.toProcessData ();
});
/* Detection of all Angular Single Page tools.
When the state change, force the Masterrow reload*/
// $location
scope.$on('$locationChangeSuccess', function(){
reload();
});
// $router
scope.$on('$routeChangeSuccess', function(){
reload();
});
// $ui-router
scope.$on('$stateChangeSuccess', function(){
reload();
});
// Clear filters processor
scope.$on('clearFiltersHandler', function(){
table.clearFilters();
});
// The destroy event
scope.$on('$destroy', function(){
scope.masterrowOptions = {};
table.end();
});
},
template: '<div></div>',
scope: {
mrFilters: '=',
mrPagination: '=',
mrDetailing: '=',
mrToProcessData: '=',
mrPersonalization: '=',
mrRowAction: '=',
mrSorting: '=',
mrExporting: '=',
// Global Object
mrOptions: '='
}
};
})
/* Service necessary to any external controller interact with the directive.*/
.factory('angularMasterrowService', [
'$rootScope',
function(
rootScope
){
return {
executeProcess: function(){
rootScope.$broadcast('executeFunctionHandler');
},
clearFilters: function(){
rootScope.$broadcast("clearFiltersHandler");
}
}
}
]);