2 function debug_dict_keys (msg, o) {
4 for (var k in o) keys.push(k);
5 messages.debug ("debug_dict_keys: " + msg + " keys= " + keys);
7 function debug_dict (msg, o) {
8 for (var k in o) messages.debug ("debug_dict: " + msg + " [" + k + "]=" + o[k]);
10 function debug_value (msg, value) {
11 messages.debug ("debug_value: " + msg + " " + value);
13 function debug_query (msg, query) {
14 if (query === undefined) messages.debug ("debug_query: " + msg + " -> undefined");
15 else if (query == null) messages.debug ("debug_query: " + msg + " -> null");
16 else if ('query_uuid' in query) messages.debug ("debug_query: " + msg + query.__repr());
17 else messages.debug ("debug_query: " + msg + " query= " + query);
20 // http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
21 Object.toType = (function toType(global) {
22 return function(obj) {
26 return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
30 /* ------------------------------------------------------------ */
32 // Constants that should be somehow moved to a plugin.js file
34 var FILTER_REMOVED = 2;
35 var CLEAR_FILTERS = 3;
37 var FIELD_REMOVED = 5;
40 var CLEAR_RECORDS = 8;
41 var FIELD_STATE_CHANGED = 9;
43 var IN_PROGRESS = 101;
46 /* Update requests related to subqueries */
48 var SET_REMOVED = 202;
51 var FIELD_REQUEST_CHANGE = 301;
52 var FIELD_REQUEST_ADD = 302;
53 var FIELD_REQUEST_REMOVE = 303;
54 var FIELD_REQUEST_RESET = 304;
56 var FIELD_REQUEST_PENDING = 401;
57 var FIELD_REQUEST_SUCCESS = 402;
58 var FIELD_REQUEST_FAILURE = 403;
61 var STATUS_NONE = 500; // Query has not been started yet
62 var STATUS_GET_IN_PROGRESS = 501; // Query has been sent, no result has been received
63 var STATUS_GET_RECEIVED = 502; // Success
64 var STATUS_GET_ERROR = 503; // Error
65 var STATUS_UPDATE_PENDING = 504;
66 var STATUS_UPDATE_IN_PROGRESS = 505;
67 var STATUS_UPDATE_RECEIVED = 506;
68 var STATUS_UPDATE_ERROR = 507;
70 /* Requests for query cycle */
76 var TYPE_LIST_OF_VALUES = 3;
77 var TYPE_LIST_OF_RECORDS = 4;
80 // A structure for storing queries
82 function QueryExt(query, parent_query_ext, main_query_ext, update_query_ext, disabled) {
85 if (typeof query == "undefined")
86 throw "Must pass a query in QueryExt constructor";
88 this.parent_query_ext = (typeof parent_query_ext == "undefined") ? null : parent_query_ext;
89 this.main_query_ext = (typeof main_query_ext == "undefined") ? null : main_query_ext;
90 this.update_query_ext = (typeof update_query_ext == "undefined") ? null : update_query_ext;
91 this.update_query_orig_ext = (typeof update_query_orig_ext == "undefined") ? null : update_query_orig_ext;
92 this.disabled = (typeof update_query_ext == "undefined") ? false : disabled;
96 // update_query null unless we are a main_query (aka parent_query == null); only main_query_fields can be updated...
99 function QueryStore() {
101 this.main_queries = {};
102 this.analyzed_queries = {};
106 this.insert = function(query)
108 // We expect only main_queries are inserted
110 /* If the query has not been analyzed, then we analyze it */
111 if (query.analyzed_query == null) {
112 query.analyze_subqueries();
115 /* We prepare the update query corresponding to the main query and store both */
116 /* Note: they have the same UUID */
118 // XXX query.change_action() should become deprecated
119 update_query = query.clone();
120 update_query.action = 'update';
121 update_query.analyzed_query.action = 'update';
122 update_query.params = {};
123 update_query_ext = new QueryExt(update_query);
125 /* We remember the original query to be able to reset it */
126 update_query_orig_ext = new QueryExt(update_query.clone());
129 /* We store the main query */
130 query_ext = new QueryExt(query, null, null, update_query_ext, update_query_orig_ext, false);
131 manifold.query_store.main_queries[query.query_uuid] = query_ext;
132 /* Note: the update query does not have an entry! */
135 // The query is disabled; since it is incomplete until we know the content of the set of subqueries
136 // XXX unless we have no subqueries ???
137 // we will complete with params when records are received... this has to be done by the manager
138 // SET_ADD, SET_REMOVE will change the status of the elements of the set
139 // UPDATE will change also, etc.
140 // XXX We need a proper structure to store this information...
142 // We also need to insert all queries and subqueries from the analyzed_query
143 // XXX We need the root of all subqueries
144 query.iter_subqueries(function(sq, data, parent_query) {
146 parent_query_ext = manifold.query_store.find_analyzed_query_ext(parent_query.query_uuid);
148 parent_query_ext = null;
149 // XXX parent_query_ext == false
150 // XXX main.subqueries = {} # Normal, we need analyzed_query
151 sq_ext = new QueryExt(sq, parent_query_ext, query_ext)
152 manifold.query_store.analyzed_queries[sq.query_uuid] = sq_ext;
155 // XXX We have spurious update queries...
160 this.find_query_ext = function(query_uuid)
162 return this.main_queries[query_uuid];
165 this.find_query = function(query_uuid)
167 return this.find_query_ext(query_uuid).query;
170 this.find_analyzed_query_ext = function(query_uuid)
172 return this.analyzed_queries[query_uuid];
175 this.find_analyzed_query = function(query_uuid)
177 return this.find_analyzed_query_ext(query_uuid).query;
182 * This namespace holds functions for globally managing query objects
187 /**************************************************************************
189 **************************************************************************/
195 spin: function(locator, active /*= true */) {
196 active = typeof active !== 'undefined' ? active : true;
199 $(locator).spin(manifold.spin_presets);
201 $(locator).spin(false);
203 } catch (err) { messages.debug("Cannot turn spins on/off " + err); }
206 get_type: function(variable)
208 switch(Object.toType(variable)) {
215 if ((variable.length > 0) && (Object.toType(variable[0]) === 'object'))
216 return TYPE_LIST_OF_RECORDS;
218 return TYPE_LIST_OF_VALUES;
222 /**************************************************************************
223 * Metadata management
224 **************************************************************************/
228 get_table: function(method)
230 var table = MANIFOLD_METADATA[method];
231 return (typeof table === 'undefined') ? null : table;
234 get_columns: function(method)
236 var table = this.get_table(method);
241 return (typeof table.column === 'undefined') ? null : table.column;
244 get_key: function(method)
246 var table = this.get_table(method);
250 return (typeof table.key === 'undefined') ? null : table.key;
254 get_column: function(method, name)
256 var columns = this.get_columns(method);
260 $.each(columns, function(i, c) {
267 get_type: function(method, name)
269 var table = this.get_table(method);
273 return (typeof table.type === 'undefined') ? null : table.type;
278 /**************************************************************************
280 **************************************************************************/
282 query_store: new QueryStore(),
284 // XXX Remaining functions are deprecated since they are replaced by the query store
287 * Associative array storing the set of queries active on the page
293 * Insert a query in the global hash table associating uuids to queries.
294 * If the query has no been analyzed yet, let's do it.
295 * \fn insert_query(query)
297 * \param ManifoldQuery query Query to be added
299 insert_query : function (query) {
301 manifold.query_store.insert(query);
304 if (query.analyzed_query == null) {
305 query.analyze_subqueries();
307 manifold.all_queries[query.query_uuid]=query;
311 * Returns the query associated to a UUID
312 * \fn find_query(query_uuid)
314 * \param string query_uuid The UUID of the query to be returned
316 find_query : function (query_uuid) {
317 return manifold.all_queries[query_uuid];
320 /**************************************************************************
322 **************************************************************************/
324 // trigger a query asynchroneously
325 proxy_url : '/manifold/proxy/json/',
327 asynchroneous_debug : true,
330 * \brief We use js function closure to be able to pass the query (array)
331 * to the callback function used when data is received
333 success_closure: function(query, publish_uuid, callback /*domid*/)
335 return function(data, textStatus) {
336 manifold.asynchroneous_success(data, query, publish_uuid, callback /*domid*/);
340 run_query: function(query, callback)
342 // default value for callback = null
343 if (typeof callback === 'undefined')
346 var query_json = JSON.stringify(query);
348 /* Nothing related to pubsub here... for the moment at least. */
349 //query.iter_subqueries(function (sq) {
350 // manifold.raise_record_event(sq.query_uuid, IN_PROGRESS);
353 $.post(manifold.proxy_url, {'json': query_json} , manifold.success_closure(query, null, callback /*domid*/));
356 // Executes all async. queries
357 // input queries are specified as a list of {'query_uuid': <query_uuid>, 'id': <possibly null>}
358 asynchroneous_exec : function (query_publish_dom_tuples) {
361 // in case the spin stuff was not loaded, let's make sure we proceed to the exit
363 // if (manifold.asynchroneous_debug)
364 // messages.debug("Turning on spin with " + jQuery(".need-spin").length + " matches for .need-spin");
365 // jQuery('.need-spin').spin(manifold.spin_presets);
366 //} catch (err) { messages.debug("Cannot turn on spins " + err); }
368 // Loop through input array, and use publish_uuid to publish back results
369 jQuery.each(query_publish_dom_tuples, function(index, tuple) {
370 var query=manifold.find_query(tuple.query_uuid);
371 var query_json=JSON.stringify (query);
372 var publish_uuid=tuple.publish_uuid;
373 // by default we publish using the same uuid of course
374 if (publish_uuid==undefined) publish_uuid=query.query_uuid;
375 if (manifold.asynchroneous_debug) {
376 messages.debug("sending POST on " + manifold.proxy_url + " to be published on " + publish_uuid);
377 messages.debug("... ctd... with actual query= " + query.__repr());
380 query.iter_subqueries(function (sq) {
381 manifold.raise_record_event(sq.query_uuid, IN_PROGRESS);
384 // not quite sure what happens if we send a string directly, as POST data is named..
385 // this gets reconstructed on the proxy side with ManifoldQuery.fill_from_POST
386 jQuery.post(manifold.proxy_url, {'json':query_json} , manifold.success_closure(query, publish_uuid, tuple.callback /*domid*/));
391 * \brief Forward a query to the manifold backend
392 * \param query (dict) the query to be executed asynchronously
393 * \param callback (function) the function to be called when the query terminates
395 * \param domid (string) the domid to be notified about the results (null for using the pub/sub system
397 forward: function(query, callback /*domid*/) {
398 var query_json = JSON.stringify(query);
399 $.post(manifold.proxy_url, {'json': query_json} , manifold.success_closure(query, query.query_uuid, callback/*domid*/));
403 * Returns whether a query expects a unique results.
404 * This is the case when the filters contain a key of the object
405 * \fn query_expects_unique_result(query)
407 * \param ManifoldQuery query Query for which we are testing whether it expects a unique result
409 query_expects_unique_result: function(query) {
410 /* XXX we need functions to query metadata */
411 //var keys = MANIFOLD_METADATA[query.object]['keys']; /* array of array of field names */
412 /* TODO requires keys in metadata */
418 * \fn publish_result(query, results)
420 * \param ManifoldQuery query Query which has received results
421 * \param array results results corresponding to query
423 publish_result: function(query, result) {
424 if (typeof result === 'undefined')
428 manifold.raise_record_event(query.query_uuid, CLEAR_RECORDS);
429 $.each(result, function(i, record) {
430 manifold.raise_record_event(query.query_uuid, NEW_RECORD, record);
432 manifold.raise_record_event(query.query_uuid, DONE);
434 // OLD PLUGIN API BELOW
435 /* Publish an update announce */
436 var channel="/results/" + query.query_uuid + "/changed";
437 if (manifold.asynchroneous_debug)
438 messages.debug("publishing result on " + channel);
439 jQuery.publish(channel, [result, query]);
443 * Recursively publish result
444 * \fn publish_result_rec(query, result)
446 * \param ManifoldQuery query Query which has received result
447 * \param array result result corresponding to query
449 publish_result_rec: function(query, result) {
450 /* If the result is not unique, only publish the top query;
451 * otherwise, publish the main object as well as subqueries
452 * XXX how much recursive are we ?
454 if (manifold.query_expects_unique_result(query)) {
455 /* Also publish subqueries */
456 jQuery.each(query.subqueries, function(object, subquery) {
457 manifold.publish_result_rec(subquery, result[0][object]);
458 /* TODO remove object from result */
461 manifold.publish_result(query, result);
464 setup_update_query: function(query, records)
466 // We don't prepare an update query if the result has more than 1 entry
467 if (records.length != 1)
469 var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
471 var record = records[0];
473 var update_query_ext = query_ext.update_query_ext;
474 var update_query = update_query_ext.query;
475 var update_query_ext = query_ext.update_query_ext;
476 var update_query_orig = query_ext.update_query_orig_ext.query;
478 // Testing whether the result has subqueries (one level deep only)
479 // iif the query has subqueries
481 var obj = query.analyzed_query.subqueries;
482 for (method in obj) {
483 if (obj.hasOwnProperty(method)) {
484 var key = manifold.metadata.get_key(method);
491 var subrecords = record[method];
494 $.each(subrecords, function (i, subrecord) {
495 sq_keys.push(subrecord[key]);
497 update_query.params[method] = sq_keys;
498 update_query_orig.params[method] = sq_keys.slice();
504 update_query_ext.disabled = false;
505 update_query_orig_ext.disabled = false;
509 process_get_query_records: function(query, records)
511 this.setup_update_query(query, records);
513 /* Publish full results */
514 tmp_query = manifold.find_query(query.query_uuid);
515 manifold.publish_result_rec(tmp_query.analyzed_query, records);
520 * What we need to do when receiving results from an update query:
521 * - differences between what we had, what we requested, and what we obtained
522 * . what we had : update_query_orig (simple fields and set fields managed differently)
523 * . what we requested : update_query
524 * . what we received : records
525 * - raise appropriate events
527 * The normal process is that results similar to Get will be pushed in the
528 * pubsub mechanism, thus repopulating everything while we only need
529 * diff's. This means we need to move the publish functionalities in the
530 * previous 'process_get_query_records' function.
532 process_update_query_records: function(query, records)
534 // First issue: we request everything, and not only what we modify, so will will have to ignore some fields
535 var query_uuid = query.query_uuid;
536 var query_ext = manifold.query_store.find_analyzed_query_ext(query_uuid);
537 var update_query = query_ext.main_query_ext.update_query_ext.query;
538 var update_query_orig = query_ext.main_query_ext.update_query_orig_ext.query;
540 // Since we update objects one at a time, we can get the first record
541 var record = records[0];
543 // Let's iterate over the object properties
544 for (var field in record) {
545 switch (this.get_type(record[field])) {
547 // Did we ask for a change ?
548 var update_value = update_query[field];
550 // Not requested, if it has changed: OUT OF SYNC
552 // We assume it won't have changed
555 var result_value = record[field];
557 throw "Internal error";
560 request: FIELD_REQUEST_CHANGE,
562 value : update_value,
563 status: (update_value == result_value) ? FIELD_REQUEST_SUCCESS : FIELD_REQUEST_FAILURE,
565 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
569 throw "Not implemented";
572 case TYPE_LIST_OF_VALUES:
573 // Same as list of records, but we don't have to extract keys
574 var result_keys = record[field]
576 // The rest of exactly the same (XXX factorize)
577 var update_keys = update_query_orig.params[field];
578 var query_keys = update_query.params[field];
579 var added_keys = $.grep(query_keys, function (x) { return $.inArray(x, update_keys) == -1 });
580 var removed_keys = $.grep(update_keys, function (x) { return $.inArray(x, query_keys) == -1 });
583 $.each(added_keys, function(i, key) {
584 if ($.inArray(key, result_keys) == -1) {
586 request: FIELD_REQUEST_ADD,
589 status: FIELD_REQUEST_FAILURE,
593 request: FIELD_REQUEST_ADD,
596 status: FIELD_REQUEST_SUCCESS,
599 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
601 $.each(removed_keys, function(i, key) {
602 if ($.inArray(key, result_keys) == -1) {
604 request: FIELD_REQUEST_REMOVE,
607 status: FIELD_REQUEST_SUCCESS,
611 request: FIELD_REQUEST_REMOVE,
614 status: FIELD_REQUEST_FAILURE,
617 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
622 case TYPE_LIST_OF_RECORDS:
623 // example: slice.resource
624 // - update_query_orig.params.resource = resources in slice before update
625 // - update_query.params.resource = resource requested in slice
626 // - keys from field = resources obtained
627 var key = manifold.metadata.get_key(field);
630 if (key.length > 1) {
631 throw "Not implemented";
636 /* XXX should be modified for multiple keys */
637 var result_keys = $.map(record[field], function(x) { return x[key]; });
639 var update_keys = update_query_orig.params[field];
640 var query_keys = update_query.params[field];
641 var added_keys = $.grep(query_keys, function (x) { return $.inArray(x, update_keys) == -1 });
642 var removed_keys = $.grep(update_keys, function (x) { return $.inArray(x, query_keys) == -1 });
645 $.each(added_keys, function(i, key) {
646 if ($.inArray(key, result_keys) == -1) {
648 request: FIELD_REQUEST_ADD,
651 status: FIELD_REQUEST_FAILURE,
655 request: FIELD_REQUEST_ADD,
658 status: FIELD_REQUEST_SUCCESS,
661 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
663 $.each(removed_keys, function(i, key) {
664 if ($.inArray(key, result_keys) == -1) {
666 request: FIELD_REQUEST_REMOVE,
669 status: FIELD_REQUEST_SUCCESS,
673 request: FIELD_REQUEST_REMOVE,
676 status: FIELD_REQUEST_FAILURE,
679 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
687 // XXX Now we need to adapt 'update' and 'update_orig' queries as if we had done a get
688 this.setup_update_query(query, records);
691 process_query_records: function(query, records)
693 if (query.action == 'get') {
694 this.process_get_query_records(query, records);
695 } else if (query.action == 'update') {
696 this.process_update_query_records(query, records);
700 // if set domid allows the result to be directed to just one plugin
701 // most of the time publish_uuid will be query.query_uuid
702 // however in some cases we wish to publish the result under a different uuid
703 // e.g. an updater wants to publish its result as if from the original (get) query
704 asynchroneous_success : function (data, query, publish_uuid, callback /*domid*/) {
705 // xxx should have a nicer declaration of that enum in sync with the python code somehow
707 /* If a callback has been specified, we redirect results to it */
708 if (!!callback) { callback(data); return; }
710 if (data.code == 2) { // ERROR
711 // We need to make sense of error codes here
712 alert("Your session has expired, please log in again");
713 window.location="/logout/";
716 if (data.code == 1) { // WARNING
717 messages.error("Some errors have been received from the manifold backend at " + MANIFOLD_URL + " [" + data.description + "]");
718 // publish error code and text message on a separate channel for whoever is interested
720 $.publish("/results/" + publish_uuid + "/failed", [data.code, data.description] );
722 $("#notifications").notify("create", "sticky", {
724 text: data.description
731 // once everything is checked we can use the 'value' part of the manifoldresult
732 var result=data.value;
734 /* Eventually update the content of related queries (update, etc) */
735 this.process_query_records(query, result);
737 /* Publish results: disabled here, done in the previous call */
738 //tmp_query = manifold.find_query(query.query_uuid);
739 //manifold.publish_result_rec(tmp_query.analyzed_query, result);
743 /**************************************************************************
745 **************************************************************************/
747 raise_event_handler: function(type, query_uuid, event_type, value)
749 if ((type != 'query') && (type != 'record'))
750 throw 'Incorrect type for manifold.raise_event()';
752 var channels = [ manifold.get_channel(type, query_uuid), manifold.get_channel(type, '*') ];
754 $.each(channels, function(i, channel) {
755 if (value === undefined)
756 $('.plugin').trigger(channel, [event_type]);
758 $('.plugin').trigger(channel, [event_type, value]);
762 raise_query_event: function(query_uuid, event_type, value)
764 manifold.raise_event_handler('query', query_uuid, event_type, value);
767 raise_record_event: function(query_uuid, event_type, value)
769 manifold.raise_event_handler('record', query_uuid, event_type, value);
773 raise_event: function(query_uuid, event_type, value)
775 // Query uuid has been updated with the key of a new element
776 query_ext = manifold.query_store.find_analyzed_query_ext(query_uuid);
777 query = query_ext.query;
780 case FIELD_STATE_CHANGED:
781 // value is an object (request, key, value, status)
782 // update is only possible is the query is not pending, etc
783 // SET_ADD is on a subquery, FIELD_STATE_CHANGED on the query itself
784 // we should map SET_ADD on this...
786 // 1. Update internal query store about the change in status
788 // 2. Update the update query
789 update_query = query_ext.main_query_ext.update_query_ext.query;
790 update_query_orig = query_ext.main_query_ext.update_query_orig_ext.query;
792 switch(value.request) {
793 case FIELD_REQUEST_CHANGE:
794 update_query.params[value.key] = value.value;
796 case FIELD_REQUEST_ADD:
797 if ($.inArray(value.value, update_query_orig.params[value.key]) != -1)
798 value.request = FIELD_REQUEST_RESET;
799 update_query.params[value.key].push(value.value);
801 case FIELD_REQUEST_REMOVE:
802 if ($.inArray(value.value, update_query_orig.params[value.key]) == -1)
803 value.request = FIELD_REQUEST_RESET;
805 var arr = update_query.params[value.key];
806 arr = $.grep(arr, function(x) { return x != value.value; });
807 update_query.params[value.key] = arr;
810 case FIELD_REQUEST_RESET:
811 // XXX We would need to keep track of the original query
812 throw "Not implemented";
816 // 3. Inform others about the change
817 // a) the main query...
818 manifold.raise_record_event(query_uuid, event_type, value);
820 // b) subqueries eventually (dot in the key)
821 // XXX make this DOT a global variable... could be '/'
827 // update is only possible is the query is not pending, etc
830 // XXX we can only update subqueries of the main query. Check !
831 // assert query_ext.parent_query == query_ext.main_query
832 // old // update_query = query_ext.main_query_ext.update_query_ext.query;
834 // This SET_ADD is called on a subquery, so we have to
835 // recontruct the path of the key in the main_query
836 // We then call FIELD_STATE_CHANGED which is the equivalent for the main query
840 while (sq.parent_query_ext) {
843 path = sq.query.object + path;
844 sq = sq.parent_query_ext;
847 main_query = query_ext.main_query_ext.query;
849 request: (event_type == SET_ADD) ? FIELD_REQUEST_ADD : FIELD_REQUEST_REMOVE,
852 status: FIELD_REQUEST_PENDING,
854 this.raise_event(main_query.query_uuid, FIELD_STATE_CHANGED, data);
856 // old //update_query.params[path].push(value);
857 // old // console.log('Updated query params', update_query);
858 // NOTE: update might modify the fields in Get
859 // NOTE : we have to modify all child queries
860 // NOTE : parts of a query might not be started (eg slice.measurements, how to handle ?)
862 // if everything is done right, update_query should not be null. It is updated when we received results from the get query
863 // object = the same as get
864 // filter = key : update a single object for now
865 // fields = the same as get
866 manifold.raise_query_event(query_uuid, event_type, value);
871 manifold.run_query(query_ext.main_query_ext.update_query_ext.query);
875 manifold.raise_query_event(query_uuid, event_type, value);
878 manifold.raise_query_event(query_uuid, event_type, value);
881 main_query = query_ext.main_query_ext.query;
882 main_update_query = query_ext.main_query_ext.update_query;
885 // Here we need the full path through all subqueries
887 // XXX We might need the query name in the QueryExt structure
888 main_query.select(value);
890 // XXX When is an update query associated ?
891 // XXX main_update_query.select(value);
893 manifold.raise_query_event(query_uuid, event_type, value);
897 query = query_ext.query;
898 main_query = query_ext.main_query_ext.query;
899 main_update_query = query_ext.main_query_ext.update_query;
900 query.unselect(value);
901 main_query.unselect(value);
903 // We need to inform about changes in these queries to the respective plugins
904 // Note: query & main_query have the same UUID
905 manifold.raise_query_event(query_uuid, event_type, value);
908 // We need to inform about changes in these queries to the respective plugins
909 // Note: query, main_query & update_query have the same UUID
910 manifold.raise_query_event(query_uuid, event_type, value);
911 // We are targeting the same object with get and update
912 // The notion of query is bad, we should have a notion of destination, and issue queries on the destination
913 // NOTE: Editing a subquery == editing a local view on the destination
915 // XXX We might need to run the new query again and manage the plugins in the meantime with spinners...
916 // For the time being, we will collect all columns during the first query
919 /* Publish/subscribe channels for internal use */
920 get_channel: function(type, query_uuid)
922 if ((type !== 'query') && (type != 'record'))
924 return '/' + type + '/' + query_uuid;
927 }; // manifold object
928 /* ------------------------------------------------------------ */
932 // OLD PLUGIN API: extend jQuery/$ with pubsub capabilities
933 // https://gist.github.com/661855
935 $.subscribe = function( channel, selector, data, fn) {
936 /* borrowed from jQuery */
937 if ( data == null && fn == null ) {
940 data = selector = undefined;
941 } else if ( fn == null ) {
942 if ( typeof selector === "string" ) {
943 // ( channel, selector, fn )
947 // ( channel, data, fn )
950 selector = undefined;
955 /* We use an indirection function that will clone the object passed in
956 * parameter to the subscribe callback
958 * FIXME currently we only clone query objects which are the only ones
959 * supported and editable, we might have the same issue with results but
960 * the page load time will be severely affected...
962 o.on.apply(o, [channel, selector, data, function() {
963 for(i = 1; i < arguments.length; i++) {
964 if ( arguments[i].constructor.name == 'ManifoldQuery' )
965 arguments[i] = arguments[i].clone();
967 fn.apply(o, arguments);
971 $.unsubscribe = function() {
972 o.off.apply(o, arguments);
975 $.publish = function() {
976 o.trigger.apply(o, arguments);
981 /* ------------------------------------------------------------ */
983 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
984 //make sure to expose csrf in our outcoming ajax/post requests
986 beforeSend: function(xhr, settings) {
987 function getCookie(name) {
988 var cookieValue = null;
989 if (document.cookie && document.cookie != '') {
990 var cookies = document.cookie.split(';');
991 for (var i = 0; i < cookies.length; i++) {
992 var cookie = jQuery.trim(cookies[i]);
993 // Does this cookie string begin with the name we want?
994 if (cookie.substring(0, name.length + 1) == (name + '=')) {
995 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
1002 if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
1003 // Only send the token to relative URLs i.e. locally.
1004 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));