cleanup spin functions - actually use our defaults - provide means to tweak defaults...
[unfold.git] / manifold / static / js / plugin.js
index f392961..a751e17 100644 (file)
@@ -30,6 +30,9 @@ var Plugin = Class.extend({
         // reference and a normal reference
         this.element  = element;
         this.$element = $(element);
+       // programmatically add specific class for publishing events
+       // used in manifold.js for triggering API events
+       if ( ! this.$element.hasClass('pubsub')) this.$element.addClass('pubsub');
 
         // return this so we can chain/use the bridge with less code.
         return this;
@@ -189,7 +192,32 @@ var Plugin = Class.extend({
 
     id_from_key: function(key_field, value) {
         
-        return key_field + manifold.separator + unfold.escape_id(value).replace(/\\/g, '');
+        return key_field + manifold.separator + this.escape_id(value).replace(/\\/g, '');
+    },
+
+    // NOTE
+    // at some point in time we used to have a helper function named 'flat_id' here
+    // the goals was to sort of normalize id's but it turned out we can get rid of that
+    // in a nutshell, we would have an id (can be urn, hrn, whatever) and 
+    // we want to be able to retrieve a DOM element based on that (e.g. a checkbox)
+    // so we did something like <tag id="some-id-that-comes-from-the-db">
+    // and then $("#some-id-that-comes-from-the-db")
+    // however the syntax for that selector prevents from using some characters in id
+    // and so for some of our ids this won't work
+    // instead of 'flattening' we now do this instead
+    // <tag some_id="then!we:can+use.what$we!want">
+    // and to retrieve it
+    // $("[some_id='then!we:can+use.what$we!want']")
+    // which thanks to the quotes, works; and you can use this with id as well in fact
+    // of course if now we have quotes in the id it's going to squeak, but well..
+
+    // escape (read: backslashes) some meta-chars in input
+    escape_id: function(id) {
+        if( id !== undefined){
+            return id.replace( /(:|\.|\[|\])/g, "\\$1" );
+        }else{
+            return "undefined-id";
+        }
     },
 
     id_from_record: function(method, record) {
@@ -226,14 +254,28 @@ var Plugin = Class.extend({
         return array[arguments.length + 1];
     },
 
-    /* SPIN */
+    // TOGGLE
+    // plugin-helper.js is about managing toggled state
+    // it would be beneficial to merge it in here
+    toggle_on: function () { return this.toggle("true"); },
+    toggle_off: function () { return this.toggle("false"); },
+    toggle: function (status) {
+       plugin_helper.set_toggle_status (this.options.plugin_uuid,status);
+    },
 
-    spin: function() {
-        manifold.spin(this.element);
+    /* SPIN */
+    // use spin() to get our default spin settings (called presets)
+    // use spin(true) to get spin's builtin defaults
+    // you can also call spin_presets() yourself and tweak what you need to, like validatebutton does
+    spin: function (presets) {
+       var presets = ( presets === undefined ) ? spin_presets() : presets;
+       try { this.$element.spin(presets); }
+       catch (err) { messages.debug("Cannot turn on spin " + err); }
     },
 
     unspin: function() {
-        manifold.spin(this.element, false);
+       try { this.$element.spin(false); }
+       catch (err) { messages.debug("Cannot turn off spin " + err); }
     },
 
     /* TEMPLATE */