Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / permissions_by_term / js / src / client / term-collector.prototype.js
diff --git a/web/modules/contrib/permissions_by_term/js/src/client/term-collector.prototype.js b/web/modules/contrib/permissions_by_term/js/src/client/term-collector.prototype.js
new file mode 100644 (file)
index 0000000..07b52c4
--- /dev/null
@@ -0,0 +1,54 @@
+const _ = require('lodash');
+
+let TermCollector = function(){
+  this.selectedTids = [];
+};
+
+TermCollector.prototype.getSelectedTids = function() {
+  return this.selectedTids;
+}
+
+TermCollector.prototype.termExists = function(key) {
+  if (!this.selectedTids || (this.selectedTids.constructor !== Array && this.selectedTids.constructor !== Object)) {
+    return false;
+  }
+  for (let i = 0; i < this.selectedTids.length; i++) {
+    if (this.selectedTids[i] === key) {
+      return true;
+    }
+  }
+  return key in this.selectedTids;
+}
+
+TermCollector.prototype.addSelectedTid = function(tid) {
+  if (!this.termExists(tid)) {
+    this.selectedTids.push(tid);
+  }
+}
+
+TermCollector.prototype.addSelectedTids = function(tids) {
+  if (!_.isEmpty(tids)) {
+    _.flatten(tids).forEach((tid) => {
+
+      if (_.isArray(tid)) {
+        throw 'Wanted to add array. Must be string.';
+      }
+
+      this.addSelectedTid(tid);
+    });
+  }
+}
+
+TermCollector.prototype.removeTid = function(value, formElementCssClass) {
+  const index = this.selectedTids[formElementCssClass].indexOf(parseInt(value));
+
+  if (index !== -1) {
+    this.selectedTids[formElementCssClass].splice(index, 1);
+  }
+}
+
+TermCollector.prototype.resetData = function(formElementCssClass) {
+  this.selectedTids[formElementCssClass] = [];
+}
+
+export default TermCollector;
\ No newline at end of file