Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- globalApp
- .provider(
- "menuService",
- function () {
- this.items = [];
- this.allItems = [];
- this.currCount = 0;
- var that = this;
- this.tryPush = function (item) {
- item.order = item.order || 100; //default position;
- //Eliminate similar srefs
- for (var ii in that.allItems) {
- var i = that.allItems[ii];
- var iKey = (i.sref || "") + (i.click || "");
- var itemKey = (item.sref || "") + (item.click || "");
- if (iKey === itemKey) {
- for (var k in item) i[k] = item[k];
- return null;
- }
- }
- that.currCount++;
- item.insertionOrder = that.currCount;
- that.allItems.push(item);
- return item;
- }
- return ({
- register: function (item) { // Allows this provider to be consumed on Config() stage
- that.tryPush(item);
- return this;
- },
- $get: function (authService) {
- that.sortItems = function () {
- that.items.sort(function (a, b) {
- return a.insertionOrder - b.insertionOrder;
- });
- that.items.sort(function (a, b) {
- return a.order - b.order;
- });
- };
- that.evaluate = function (item, doNotRefresh) {
- if(!item)return;
- var accept = item.permissionSet ? authService.hasPermission(item.permissionSet) : true;
- if (accept) {
- that.items.push(item);
- if (!!!doNotRefresh)
- that.sortItems();
- }
- };
- that.evaluateAll = function () {
- that.items.length = 0;
- angular.forEach(that.allItems, function (item) {
- that.evaluate(item, true);
- });
- that.sortItems();
- };
- authService.events.onChange(null, function () {
- that.evaluateAll();
- });
- return {
- // Normal usage during Run() stage
- register: function (item) {
- that.evaluate(that.tryPush(item));
- },
- evaluateAll: that.evaluateAll,
- items: that.items
- };
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement