wip metadata
[unfold.git] / manifold / js / manifold.js
index 221986d..94b6442 100644 (file)
@@ -29,15 +29,84 @@ var CLEAR_FIELDS   = 6;
 var NEW_RECORD     = 7;
 var CLEAR_RECORDS  = 8;
 
-var IN_PROGRESS     = 101;
+var IN_PROGRESS    = 101;
 var DONE           = 102;
 
+var SET_ADD        = 201;
+var SET_REMOVED    = 202;
+
+// A structure for storing queries
+
+function QueryExt(query, parent_query, main_query) {
+
+    /* Constructor */
+    if (typeof query == "undefined")
+        throw "Must pass a query in QueryExt constructor";
+    this.query        = query
+    this.parent_query = (typeof parent_query == "undefined") ? false : parent_query
+    this.main_query   = (typeof main_query   == "undefined") ? false : main_query
+
+    // How to link to an update query ? they both have the same UUID !!
+
+}
+
+function QueryStore() {
+
+    var main_queries     = {};
+    var analyzed_queries = {};
+
+    /* Insertion */
+
+    this.insert = function(query)
+    {
+        if (query.analyzed_query == null) {
+            query.analyze_subqueries();
+        }
+
+        query_ext = QueryExt(query, null, null)
+        manifold.query_store.main_queries[query.query_uuid] = query_ext;
+
+        // We also need to insert all queries and subqueries from the analyzed_query
+        query.iter_subqueries(function(sq, data, parent_query) {
+            parent_query_ext = this.find_analyzed_query_ext(parent_query.query_uuid);
+            sq_ext = QueryExt(sq, parent_query_ext, query_ext)
+            this.analyzed_queries[sq.query_uuid] = sq_ext;
+        });
+    }
+
+    /* Searching */
+
+    this.find_query_ext = function(query_uuid)
+    {
+        return this.main_queries[query_uuid];
+    }
+
+    this.find_query = function(query_uuid)
+    {
+        return this.find_query_ext(query_uuid).query;
+    }
+
+    this.find_analyzed_query_ext = function(query_uuid)
+    {
+        return this.analyzed_queries[query_uuid];
+    }
+
+    this.find_analyzed_query = function(query_uuid)
+    {
+        return this.find_analyzed_query_ext(query_uuid).query;
+    }
+}
+
 /*!
  * This namespace holds functions for globally managing query objects
  * \Class Manifold
  */
 var manifold = {
 
+    /************************************************************************** 
+     * Helper functions
+     **************************************************************************/ 
+
     spin_presets: {},
 
     spin: function(locator, active /*= true */) {
@@ -51,6 +120,14 @@ var manifold = {
         } catch (err) { messages.debug("Cannot turn spins on/off " + err); }
     },
 
+    /************************************************************************** 
+     * Query management
+     **************************************************************************/ 
+
+    query_store: QueryStore(),
+
+    // XXX Remaining functions are deprecated since they are replaced by the query store
+
     /*!
      * Associative array storing the set of queries active on the page
      * \memberof Manifold
@@ -81,6 +158,10 @@ var manifold = {
         return manifold.all_queries[query_uuid];
     },
 
+    /************************************************************************** 
+     * Query execution
+     **************************************************************************/ 
+
     // trigger a query asynchroneously
     proxy_url : '/manifold/proxy/json/',
 
@@ -122,7 +203,7 @@ var manifold = {
             }
 
             query.iter_subqueries(function (sq) {
-                $('.plugin').trigger(manifold.get_record_channel(sq.query_uuid), [IN_PROGRESS]);
+                manifold.raise_record_event(sq.query_uuid, IN_PROGRESS);
             });
 
             // not quite sure what happens if we send a string directly, as POST data is named..
@@ -169,12 +250,11 @@ var manifold = {
             result = [];
 
         // NEW PLUGIN API
-        var channel = manifold.get_record_channel(query.query_uuid);
-        $('.plugin').trigger(channel, [CLEAR_RECORDS]);
+        manifold.raise_record_event(query.query_uuid, CLEAR_RECORDS);
         $.each(result, function(i, record) {
-            $('.plugin').trigger(channel, [NEW_RECORD, record]);
+            manifold.raise_record_event(query.query_uuid, NEW_RECORD, record);
         });
-        $('.plugin').trigger(channel, [DONE]);
+        manifold.raise_record_event(query.query_uuid, DONE);
 
         // OLD PLUGIN API BELOW
         /* Publish an update announce */
@@ -217,6 +297,7 @@ var manifold = {
         if (!!callback) { callback(data); return; }
 
         if (data.code == 2) { // ERROR
+            // We need to make sense of error codes here
             alert("Your session has expired, please log in again");
             window.location="/logout/";
             return;
@@ -225,6 +306,15 @@ var manifold = {
             messages.error("Some errors have been received from the manifold backend at " + MANIFOLD_URL + " [" + data.description + "]");
             // publish error code and text message on a separate channel for whoever is interested
             jQuery.publish("/results/" + publish_uuid + "/failed", [data.code, data.description] );
+
+            $("#notifications").notify("create", "sticky", {
+              title: 'Warning',
+              text: data.description
+            },{
+              expires: false,
+              speed: 1000
+            });
+            
         }
         // once everything is checked we can use the 'value' part of the manifoldresult
         var result=data.value;
@@ -247,6 +337,55 @@ var manifold = {
         }
     },
 
+    /************************************************************************** 
+     * Plugin API helpers
+     **************************************************************************/ 
+
+    raise_event_handler: function(type, query_uuid, event_type, value)
+    {
+        if (type == 'query') {
+            var channels = [ manifold.get_query_channel(query_uuid), manifold.get_query_channel('*') ];
+        } else if (type == 'record') {
+            var channels = [ manifold.get_record_channel(query_uuid), manifold.get_record_channel('*') ];
+
+        } else {
+            throw 'Incorrect type for manifold.raise_event()';
+        }
+        $.each(channels, function(i, channel) {
+            if (value === undefined)
+                $('.plugin').trigger(channel, [event_type]);
+            else
+                $('.plugin').trigger(channel, [event_type, value]);
+        });
+    },
+
+    raise_query_event: function(query_uuid, event_type, value)
+    {
+        manifold.raise_event_handler('query', query_uuid, event_type, value);
+    },
+
+    raise_record_event: function(query_uuid, event_type, value)
+    {
+        manifold.raise_event_handler('record', query_uuid, event_type, value);
+    },
+
+
+    raise_event: function(query_uuid, event_type, value)
+    {
+        switch(event_type) {
+            case SET_ADD:
+                // Query uuid has been updated with the key of a new element
+                query = manifold.find_query(query_uuid);
+
+                // XXX We need to find the parent to update the property
+                // XXX We need to find the non-analyzed query so that it can be updated also
+                break;
+            case SET_REMOVED:
+                // Query uuid has been updated with the key of a removed element
+                break;
+        }
+    },
+
     /* Publish/subscribe channels for internal use */
     get_query_channel:  function(uuid) { return '/query/'  + uuid },
     get_record_channel: function(uuid) { return '/record/' + uuid },