Merge branch 'fed4fire' into onelab
[myslice.git] / plugins / queryupdater / static / js / queryupdater.js
index 2e73629..f1d6b32 100644 (file)
     // Record state through the query cycle
 
 
-    var QueryUpdater = Plugin.extend({
+    var QueryUpdaterPlugin = Plugin.extend({
+
+        /**************************************************************************
+         *                          CONSTRUCTOR
+         **************************************************************************/
 
         init: function(options, element) {
                this.classname="queryupdater";
             this.selected_resources = Array();
 
             this.table = this.elmt('table').dataTable({
-// the original querytable layout was
-//                sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t<'row'<'col-xs-5'i><'col-xs-7'p>>",
-// however the bottom line with 'showing blabla...' and the navigation widget are not really helpful
                 sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t>",
-// so this does not matter anymore now that the pagination area is turned off
-//                sPaginationType: 'bootstrap',
-                     bAutoWidth: true,
-//                bJQueryUI      : true,
-//                bRetrieve      : true,
-//                sScrollX       : '100%',                 // Horizontal scrolling 
-//                bSortClasses   : false,              // Disable style for the sorted column
-//                aaSorting      : [[ 0, 'asc' ]],        // Default sorting on URN
-//                fnDrawCallback: function() {      // Reassociate close click every time the table is redrawn
-//                    /* Prevent to loop on click while redrawing table  */
-//                    $('.ResourceSelectedClose').unbind('click');
-//                    /* Handle clicks on close span */
-//                    /* Reassociate close click every time the table is redrawn */
-//                    $('.ResourceSelectedClose').bind('click', self, self._close_click);
-//                }
-             });
+                       bAutoWidth: true,
+            });
             
-            // XXX This should not be done at init...
-            this.elmt('update').click(this, this.do_checksla);
-            this.elmt('refresh').click(this, this.do_refresh);
-            this.elmt('reset').click(this, this.do_reset);
-            this.elmt('clear_annotations').click(this, this.do_clear_annotations);
+            this.elmt('update').click(this, this.do_ok);
+            this.elmt('refresh').click(this, this.do_cancel);
 
             this.listen_query(options.query_uuid);
         },
                     $('#sla-table-body').empty();
                     //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
                 }
-            }); 
-        
-            $("#cancel_sla").unbind().click(function(){
+            });
+
+               $("#cancel_sla").unbind().click(function(){
                 $('#slamodal').modal('hide');
                 $('#sla-table-body').empty();
             }); 
         },
+               /************************** GUI MANIPULATION **************************/
 
-     
-        do_update: function(e) {
-            var self = e.data;
-
-            var username = e.data.options.username;
-            var urn = data.value;
-            // XXX check that the query is not disabled
-
-            self.spin();
-            console.log("do_update");
-            // XXX check that the query is not disabled
-            //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
-
-            // how to stop the spinning after the event? 
-            // this should be triggered by some on_updatequery_done ?
-
-        },
-
-       // related buttons are also disabled in the html template
-        do_refresh: function(e)
-        {
-            throw 'resource_selected.do_refresh Not implemented';
-        },
-
-        do_reset: function(e)
+        populate_table: function()
         {
-            throw 'queryupdater.do_reset Not implemented';
-        },
+            var state;
 
-        do_clear_annotations: function(e)
-        {
-            throw 'queryupdater.do_clear_annotations Not implemented';
+            // Loop over records and display pending ones
+            manifold.query_store.iter_records(this.options.query_uuid, function (record_key, record) {
+                state = manifold.query_store.get_record_state(this.options.query_uuid, null, STATE_SET);
+            
+            });
         },
         
-        /************************** GUI MANIPULATION **************************/
-
-        
         set_button_state: function(name, state)
         {
             this.elmt(name).attr('disabled', state ? false : 'disabled');
             return cols[0];
         },
 
-        set_state: function(data)
-        {
-            console.log("function set_state");
-            var action;
-            var msg;
-            var button = '';
-
-            var row;
-           
-           // make sure the change is visible : toggle on the whole plugin
-           // this might have to be made an 'auto-toggle' option of this plugin..
-           // also it might be needed to be a little finer-grained here
-
-        // XXX we don't want to show automaticaly the pending when a checkbox is checked
-           //this.toggle_on();
-           
-            switch(data.request) {
-                case FIELD_REQUEST_ADD_RESET:
-                case FIELD_REQUEST_REMOVE_RESET:
-                    // find line and delete it
-                    row = this.find_row(data.value);
-                    if (row)
-                        this.table.fnDeleteRow(row.nTr);
-                        $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
-                        $("#badge-pending").text($("#badge-pending").data('number'));
-                    return;
-                case FIELD_REQUEST_CHANGE:
-                    action = 'UPDATE';
-                    break;
-                case FIELD_REQUEST_ADD:
-                    action = 'ADD';
-                    break;
-                case FIELD_REQUEST_REMOVE:
-                    action = 'REMOVE';
-                    break;
-            }
-
-            switch(data.status) {
-                case FIELD_REQUEST_PENDING:
-                    msg   = 'PENDING';
-                    button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
-                    break;
-                case FIELD_REQUEST_SUCCESS:
-                    msg   = 'SUCCESS';
-                    break;
-                case FIELD_REQUEST_FAILURE:
-                    msg   = 'FAILURE';
-                    break;
-            }
+        do_update: function(e) {
+            var self = e.data;
 
-            var status = msg + status;
+            var username = e.data.options.username;
+            var urn = data.value;
+            // XXX check that the query is not disabled
 
-            
+            self.spin();
+            console.log("do_update");
+            // XXX check that the query is not disabled
+            //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
 
-            // find line
-            // if no, create it, else replace it
-            // XXX it's not just about adding lines, but sometimes removing some
-            // XXX how do we handle status reset ?
-            data.value = JSON.stringify(data.value);
-            data.selected_resources = this.selected_resources;
-            row = this.find_row(data.value);
-            newline = [
-                action,
-                data.key,
-                data.value,
-                msg,
-                button
-            ];
-            if (!row) {
-                // XXX second parameter refresh = false can improve performance. todo in querytable also
-                this.table.fnAddData(newline);
-                row = this.find_row(data.value);
-                $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
-                $("#badge-pending").text($("#badge-pending").data('number'));
-            } else {
-                // Update row text...
-                this.table.fnUpdate(newline, row.nTr);
-            }
+            // how to stop the spinning after the event? 
+            // this should be triggered by some on_updatequery_done ?
 
-            // Change cell color according to status
-            if (row) {
-                $(row.nTr).removeClass('add remove')
-                var cls = action.toLowerCase();
-                if (cls)
-                    $(row.nTr).addClass(cls);
-            }
         },
 
-        /*************************** QUERY HANDLER ****************************/
-
-        // NONE
+        do_ok: function(e)
+        {
+            throw 'queryupdater.do_reset Not implemented';
+        },
 
-        /*************************** RECORD HANDLER ***************************/
+        do_cancel: function(e)
+        {
+            throw 'queryupdater.do_clear_annotations Not implemented';
+        },
+        
+       /**************************************************************************
+        *                           QUERY HANDLERS
+        **************************************************************************/ 
 
         on_new_record: function(record)
         {
-            console.log("query_updater on_new_record");
-            console.log(record);
 
             // if (not and update) {
 
 
         on_query_in_progress: function()
         {
-               messages.debug("queryupdater.on_query_in_progress");
             this.spin();
         },
 
+        on_query_done: function()
+        {
+            this.populate_table();
+            this.unspin();
+        },
+
         // D : Data present
         // - on_clear_records (Get)
         // - on_new_record (shared with AD) XXX
 
         on_query_done: function()
         {
-            console.log("on_query_done");
             this.unspin();
         },
 
         // NOTE: record_key could be sufficient 
         on_added_record: function(record)
         {
-            console.log("on_added_record = ",record);
             this.set_record_state(record, RECORD_STATE_ADDED);
             // update pending number
         },
 
         on_removed_record: function(record_key)
         {
-            console.log("on_removed_record = ",record_key);
             this.set_record_state(RECORD_STATE_REMOVED);
         },
 
         // - Key and confirmation could be sufficient, or key and record state
         // XXX move record state to the manifold plugin API
 
-        on_field_state_changed: function(result)
+        on_field_state_changed: function(data)
         {
-            console.log("on_field_state_changed");
-            console.log(result);
+            /*
             if(result.request == FIELD_REQUEST_ADD){
                 this.selected_resources.push(result.value);
             } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
                     this.selected_resources.splice(i,1);
                 }
             }
-            console.log("Resources: " + self.selected_resources);
-            messages.debug(result)
-            /* this.set_state(result.request, result.key, result.value, result.status); */
             this.set_state(result);
+            */
+
+            var action, msg, row, status, button = '';
+
+            switch(data.state) {
+                case STATE_VALUE:
+                    switch(data.op) {
+                        // XXX other events missing !!
+                        case STATE_VALUE_CHANGE_PENDING:
+                            action = 'UPDATE';
+                            break;
+                    }
+                    break;
+
+                case STATE_SET:
+                    switch(data.op) {
+                        case STATE_SET_IN_PENDING:
+                            action = 'ADD';
+                            msg   = 'PENDING';
+                            button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
+                            break;  
+
+                        case STATE_SET_OUT_PENDING:
+                            action = 'REMOVE';
+                            msg   = 'PENDING';
+                            button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
+                            break;
+
+                        case STATE_SET_IN:
+                        case STATE_SET_OUT:
+                            // find line and delete it
+                            row = this.find_row(data.value);
+                            if (row)
+                                this.table.fnDeleteRow(row.nTr);
+                            return;
+
+                        case STATE_SET_IN_SUCCESS:
+                        case STATE_SET_OUT_SUCCESS:
+                            msg   = 'SUCCESS';
+                            break;
+
+                        case STATE_SET_IN_FAILURE:
+                        case STATE_SET_OUT_FAILURE:
+                            msg   = 'FAILURE';
+                            break;
+
+                    }
+                    break;
+
+                default:
+                    return;
+            }
+
+            status = msg + status;
+
+            // find line
+            // if no, create it, else replace it
+            // XXX it's not just about adding lines, but sometimes removing some
+            // XXX how do we handle status reset ?
+
+            // Jordan : I don't understand this. I added this test otherwise we have string = ""..."" double quoted twice.
+            if (typeof(data.value) !== "string")
+                data.value = JSON.stringify(data.value);
+            data.selected_resources = this.selected_resources;
+            row = this.find_row(data.value);
+            newline = [action, data.key, data.value, msg, button];
+            if (!row) {
+                // XXX second parameter refresh = false can improve performance. todo in querytable also
+                this.table.fnAddData(newline);
+                row = this.find_row(data.value);
+            } else {
+                // Update row text...
+                this.table.fnUpdate(newline, row.nTr);
+            }
+
+            // Change cell color according to status
+            if (row) {
+                $(row.nTr).removeClass('add remove')
+                var cls = action.toLowerCase();
+                if (cls)
+                    $(row.nTr).addClass(cls);
+            }
         },
 
         // XXX we will have the requests for change
 
     });
 
-    $.plugin('QueryUpdater', QueryUpdater);
+    $.plugin('QueryUpdaterPlugin', QueryUpdaterPlugin);
 
 })(jQuery);