Merge branch 'fibre' of ssh://git.onelab.eu/git/myslice into fibre
[unfold.git] / manifoldapi / static / js / plugin.js
index 495bdc7..82a3cd0 100644 (file)
@@ -19,6 +19,108 @@ ManifoldApp.filter('offset', function() {
   };
 });
 
+// http://stackoverflow.com/questions/19992090/angularjs-group-by-directive
+ManifoldApp.filter('groupBy', ['$parse', function ($parse) {
+    return function (list, group_by) {
+
+        var filtered = [];
+        var prev_item = null;
+        var group_changed = false;
+        // this is a new field which is added to each item where we append "_CHANGED"
+        // to indicate a field change in the list
+        //was var new_field = group_by + '_CHANGED'; - JB 12/17/2013
+        var new_field = 'group_by_CHANGED';
+
+        // loop through each item in the list
+        angular.forEach(list, function (item) {
+
+            group_changed = false;
+
+            // if not the first item
+            if (prev_item !== null) {
+
+                // check if any of the group by field changed
+
+                //force group_by into Array
+                group_by = angular.isArray(group_by) ? group_by : [group_by];
+
+                //check each group by parameter
+                for (var i = 0, len = group_by.length; i < len; i++) {
+                    if ($parse(group_by[i])(prev_item) !== $parse(group_by[i])(item)) {
+                        group_changed = true;
+                    }
+                }
+
+
+            }// otherwise we have the first item in the list which is new
+            else {
+                group_changed = true;
+            }
+
+            // if the group changed, then add a new field to the item
+            // to indicate this
+            if (group_changed) {
+                item[new_field] = true;
+            } else {
+                item[new_field] = false;
+            }
+
+            filtered.push(item);
+            prev_item = item;
+
+        });
+
+        return filtered;
+    };
+}]);
+
+// https://github.com/angular-ui/angular-ui-OLDREPO/blob/master/modules/filters/unique/unique.js
+/**
+ * Filters out all duplicate items from an array by checking the specified key
+ * @param [key] {string} the name of the attribute of each object to compare for uniqueness
+ if the key is empty, the entire object will be compared
+ if the key === false then no filtering will be performed
+ * @return {array}
+ */
+ManifoldApp.filter('unique', function () {
+
+  return function (items, filterOn) {
+
+    if (filterOn === false) {
+      return items;
+    }
+
+    if ((filterOn || angular.isUndefined(filterOn)) && angular.isArray(items)) {
+      var hashCheck = {}, newItems = [];
+
+      var extractValueToCompare = function (item) {
+        if (angular.isObject(item) && angular.isString(filterOn)) {
+          return item[filterOn];
+        } else {
+          return item;
+        }
+      };
+
+      angular.forEach(items, function (item) {
+        var valueToCheck, isDuplicate = false;
+
+        for (var i = 0; i < newItems.length; i++) {
+          if (angular.equals(extractValueToCompare(newItems[i]), extractValueToCompare(item))) {
+            isDuplicate = true;
+            break;
+          }
+        }
+        if (!isDuplicate) {
+          newItems.push(item);
+        }
+
+      });
+      items = newItems;
+    }
+    return items;
+  };
+});
+
 // INHERITANCE
 // http://alexsexton.com/blog/2010/02/using-inheritance-patterns-to-organize-large-jquery-applications/
 // We will use John Resig's proposal
@@ -318,7 +420,7 @@ var Plugin = Class.extend({
     // you can also call spin_presets() yourself and tweak what you need to, like topmenuvalidation does
     spin: function (message) {
        if (!message) {
-               message = 'Please be patient, this can take a minute or two.';
+               message = 'Please be patient, this operation can take a minute or two.';
        }
        $('div.loading').fadeIn('fast');
        $('div.loading').find('.message').text(message);