Advertisement
Guest User

Untitled

a guest
Aug 1st, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. globalApp
  2.     .provider(
  3.         "menuService",
  4.         function () {
  5.             this.items = [];
  6.             this.allItems = [];
  7.  
  8.             this.currCount = 0;
  9.  
  10.             var that = this;
  11.  
  12.             this.tryPush = function (item) {
  13.  
  14.                 item.order = item.order || 100; //default position;
  15.  
  16.                 //Eliminate similar srefs
  17.                 for (var ii in that.allItems) {
  18.                     var i = that.allItems[ii];
  19.  
  20.                     var iKey = (i.sref || "") + (i.click || "");
  21.                     var itemKey = (item.sref || "") + (item.click || "");
  22.  
  23.  
  24.                     if (iKey === itemKey) {
  25.                         for (var k in item) i[k] = item[k];
  26.                         return null;
  27.                     }
  28.                 }
  29.  
  30.                 that.currCount++;
  31.                 item.insertionOrder = that.currCount;
  32.                     that.allItems.push(item);
  33.                     return item;
  34.             }
  35.  
  36.             return ({
  37.                 register: function (item) { // Allows this provider to be consumed on Config() stage
  38.                     that.tryPush(item);
  39.                     return this;
  40.                 },
  41.                 $get: function (authService) {
  42.  
  43.                     that.sortItems = function () {
  44.  
  45.                         that.items.sort(function (a, b) {
  46.                             return a.insertionOrder - b.insertionOrder;
  47.                         });
  48.  
  49.  
  50.                         that.items.sort(function (a, b) {
  51.                             return a.order - b.order;
  52.                         });
  53.                     };
  54.  
  55.                     that.evaluate = function (item, doNotRefresh) {
  56.  
  57.                         if(!item)return;
  58.  
  59.                         var accept = item.permissionSet ? authService.hasPermission(item.permissionSet) : true;
  60.  
  61.                         if (accept) {
  62.                             that.items.push(item);
  63.                             if (!!!doNotRefresh)
  64.                                 that.sortItems();
  65.                         }
  66.                     };
  67.  
  68.                     that.evaluateAll = function () {
  69.  
  70.                         that.items.length = 0;
  71.  
  72.                         angular.forEach(that.allItems, function (item) {
  73.                             that.evaluate(item, true);
  74.                         });
  75.  
  76.                         that.sortItems();
  77.                     };
  78.  
  79.                     authService.events.onChange(null, function () {
  80.                         that.evaluateAll();
  81.                     });
  82.  
  83.                     return {
  84.                         // Normal usage during Run() stage
  85.                         register: function (item) {
  86.                             that.evaluate(that.tryPush(item));
  87.                         },
  88.                         evaluateAll: that.evaluateAll,
  89.                         items: that.items
  90.                     };
  91.                 }
  92.             });
  93.         });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement