(wip)
[myslice.git] / plugins / scheduler / static / js / scheduler.js
index 79292e5..3c95832 100644 (file)
@@ -48,8 +48,7 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
             this._super(options, element);
 
             /* Member variables */
-            this.options = options;
-            this.canvas_id = 'leases_area-' + options.plugin_uuid;
+            this._canvas_id = this.id('leases_area');
 
             this.query_uuid = options.query_uuid;
             this.rows = null;
@@ -57,13 +56,17 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
             //this.current_resources = Array();
             //this.current_leases = Array();
 
+            /* Managing asynchronous reception of resources and leases */
+            this._tmp_resources = Array();
+            this.listLeases    = Array();
+            this._received_resources = false;
+            this._received_leases    = false;
+
             this.myLeases = Array();
             this.allLeases = Array();
 
-            this.listLeases = Array();
-
             this.axisx = Array();
-            this.axisy = Array();
+            this.resources = Array();
             this.data = Array();
             this.default_granularity = 1800; /* 30 min */
             this.initial_timestamp = null;
@@ -80,7 +83,7 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
 
             /* Listening to queries */
             this.listen_query(options.query_uuid);
-            this.listen_query(options.query_lease_uuid);
+            this.listen_query(options.query_lease_uuid, 'lease');
 
             /* XXX GUI setup and event binding */
             jQuery("#datepicker").datetimepicker({
@@ -139,13 +142,13 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
              * Implement an action of filtering while typing
              * filter what correspond to the user choice
              * 
-             */
             $(function() {
                 var availableTags = ['omf','nitos','ple', s.nodelabel];
             $( "#search" ).autocomplete({
               source: availableTags
                 });
             });
+             */
 
             this.init_axisx('');
             this.draw();
@@ -180,40 +183,54 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
 
         /* TEMPLATES */
 
-        /* QUERY HANDLERS */
+        /*************************** RECORD HANDLER ***************************/
 
-        set_resources: function(resources) 
-        {
-            //console.log(resources);
-            var scheduler = this;
-            jQuery.each(resources, function(i, resource) {
-                // ... add reservable ones to the x axis
-                if ((typeof resource.exclusive != 'undefined') && (resource.exclusive)) {
-                    scheduler.axisy.push(Array(resource.urn, resource.resource_hrn, resource.type));
-                }
-                // ... if we do not have information about slivers (first update), update it
-                if (typeof resource.sliver != 'undefined') {
-                    // XXX
-                }
-            });
-
-            this.draw(this.canvas_id);
-        }
+        /* ------------------------------------------------------------------
+         * Resources
+         * ------------------------------------------------------------------ */
 
-        this.set_leases = function(leases) {
-            this.initial_leases=leases;
-            this.draw(this.canvas_id);
-        }
+        on_new_record: function(record)
+        {
+            if ((typeof record.exclusive != 'undefined') && (record.exclusive)) {
+                this.resources.push(Array(record.urn, record.record_hrn, record.type));
+            }
+            // ... if we do not have information about slivers (first update), update it
+            if (typeof record.sliver != 'undefined') {
+                // XXX
+            }
+        },
 
-        this.update_resources = function(resources) {
-            //
-        }
+        on_query_in_progress: function() {
+            this.spin();
+        },
 
-        this.update_leases = function(leases) {
-            //
-        }
+        on_query_done: function() {
+            /* We have received all leases */
+            if (this._received_resources) {
+                this.draw(this._canvas_id);
+                this.unspin();
+            }
+            this._received_leases = true;
+        },
+        /* ------------------------------------------------------------------
+         * Leases
+         * ------------------------------------------------------------------ */
+        
+        on_lease_new_record: function(record)
+        {
+            this.listLeases.push(record);
+            // this.initial_leases=leases;
+        },
 
-        /* RECORD HANDLERS */
+        on_lease_query_done: function(record)
+        {
+            /* We have received all resources */
+            if (this._received_leases) {
+                this.draw(this._canvas_id);
+                this.unspin();
+            }
+            this._received_resources = true;
+        },
 
         /* INTERNAL FUNCTIONS */
 
@@ -254,13 +271,13 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
          */
         draw: function() 
         { 
-            var canvas_id = this.canvas_id;
+            var canvas_id = this._canvas_id;
             var o = this.options;
             var total_width = o.x_nodelabel + this.nb_grains() * this.options.leases_w;
             var total_height = 2 * o.y_header /* the timelabels */
                              + 2 * o.y_sep    /* extra space */
-                             + o.y_node          /* all-nodes & timebuttons row */ 
-                             + (this.axisy.length)*(o.y_node+o.y_sep);  /* the regular nodes and preceding space */
+                             + o.y_node          /* all-nodes & timebuttons row */ 
+                             + (this.resources.length)*(o.y_node+o.y_sep);  /* the regular nodes and preceding space */
 
             /* reuse for paper if exists with same size, or (re-)create otherwise */
             var paper;
@@ -270,7 +287,7 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
                 paper=this.paper;
                 paper.clear();
             } else {
-                $$("#"+canvas_id)[0].innerHTML="";
+                $("#"+canvas_id)[0].innerHTML="";
                 paper = Raphael (canvas_id, total_width+o.x_sep, total_height);
             }
             this.paper=paper;
@@ -279,7 +296,7 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
             this.timebutton_path = "M1,0L"+(this.options.leases_w-1)+",0L"+(this.options.leases_w/2)+","+o.y_header+"L1,0";
 
             var axisx = this.axisx;
-            var axisy = this.axisy;
+            var axisy = this.resources;
 
             /* maintain the list of nodelabels for the 'all nodes' button */
             this.nodelabels=[];
@@ -315,7 +332,7 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
                     paper.path(half_daymarker_path).attr({'translation':left+','+top}).attr(attr_daymarker);
                 }
                 left += this.options.leases_w;
-            },
+            }
 
             ////////// the row with the timeslot buttons (the one labeled 'All nodes')
             this.granularity= this.min_granularity; // XXX axisx[1][0]-axisx[0][0];
@@ -327,14 +344,14 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
             var allnodes = paper.text (o.x_nodelabel-o.x_sep,top+o.y_node/2,"All nodes").attr(txt_allnodes)
                 .attr ({"font-size":o.y_node, "text-anchor":"end","baseline":"bottom"});
             allnodes.scheduler=this;
-            allnodes.click(allnodes_methods.click);
+            allnodes.click(this.allnodes_methods.click);
             
             // timeslot buttons [it's the triangles above the slots]
             for (var i=0, len=axisx.length; i < len; ++i) {
                 var timebutton=paper.path(this.timebutton_path).attr({'translation':left+','+top}).attr(attr_timebutton);
                 timebutton.from_time=axisx[i][0];
                 timebutton.scheduler=this;
-                timebutton.click(timebutton_methods.click);
+                timebutton.click(this.timebutton_methods.click);
                 left+=(this.options.leases_w);
             }
             
@@ -355,9 +372,9 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
                 var nodelabel = paper.text(o.x_nodelabel-o.x_sep,top+o.y_node/3,nodename).attr(txt_nodelabel)
                 .attr ({"font-size":"12px", "text-anchor":"end","baseline":"bottom"});    
                 //console.log(nodelabel);
-                nodelabel_methods.selected(nodelabel,1);
+                this.nodelabel_methods.selected(nodelabel,1);
                 //nodelabel_methods.selected( this, ! this.selected );
-                nodelabel.click(nodelabel_methods.click); //click action works here
+                nodelabel.click(this.nodelabel_methods.click); //click action works here
                 //timebutton.click(timebutton_methods.click);
                 
                // lease_methods.init_free(nodelabel.click, lease_methods.click_mine);
@@ -453,7 +470,7 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
                 var lease=this.leases[i];
                 if (lease.current != lease.initial) {
                 if (lease.initial == 'free') lease_methods.init_free(lease,lease_methods.click_mine);
-                else                        lease_methods.init_mine(lease,lease_methods.click_free);
+                else                             lease_methods.init_mine(lease,lease_methods.click_free);
                 }
             }
         },
@@ -473,7 +490,7 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
                 /* if at least one is not selected : select all */
                 var new_state = (unselected >0) ? 1 : 0;
                 for (var i=0, len=scheduler.nodelabels.length; i<len; ++i) 
-                    nodelabel_methods.selected(scheduler.nodelabels[i],new_state);
+                    this.nodelabel_methods.selected(scheduler.nodelabels[i],new_state);
             }
         },