js updated for accessing nitos nodes
[myslice.git] / plugins / asap / static / js / asap.js
1 /**
2  * MyPlugin:    demonstration plugin
3  * Version:     0.1
4  * Description: Template for writing new plugins and illustrating the different
5  *              possibilities of the plugin API.
6  *              This file is part of the Manifold project 
7  * Requires:    js/plugin.js
8  * URL:         http://www.myslice.info
9  * Author:      Jordan AugĂ© <jordan.auge@lip6.fr>
10  * Copyright:   Copyright 2012-2013 UPMC Sorbonne UniversitĂ©s
11  * License:     GPLv3
12  */
13
14 var AsapDateSelected = new Date();
15
16 (function($){
17
18     var AsapPlugin = Plugin.extend({
19
20         /** XXX to check
21          * @brief Plugin constructor
22          * @param options : an associative array of setting values
23          * @param element : 
24          * @return : a jQuery collection of objects on which the plugin is
25          *     applied, which allows to maintain chainability of calls
26          */
27         init: function(options, element) {
28             // for debugging tools
29             this.classname="myplugin";
30             // Call the parent constructor, see FAQ when forgotten
31             this._super(options, element);
32
33             /* Member variables */
34
35             /* Plugin events */
36
37             /* Setup query and record handlers */
38
39             // Explain this will allow query events to be handled
40             // What happens when we don't define some events ?
41             // Some can be less efficient
42             this.listen_query(options.query_uuid, 'resources');
43             this.listen_query(options.query_lease_uuid, 'leases');
44
45             /* GUI setup and event binding */
46             // call function
47             this._initUI();
48         },
49
50         /* PLUGIN EVENTS */
51         // on_show like in querytable
52
53         remove: function(id) {
54             return (elem=document.getElementById(id)).parentNode.removeChild(elem);
55         },
56         /* GUI EVENTS */
57         /**
58          * Initialize the date picker, the table, the slider and the buttons. Once done, display scheduler.
59          */
60         _initUI: function() 
61         {
62             var self = this;
63
64             /*
65             dateToday = new Date();
66
67             $("#DateAsap").datepicker({
68                 //dateFormat: "D, d M yy",
69                 minDate: dateToday,
70                 defaultDate: dateToday,
71             }).on('changeDate', function(ev) {
72                 if(ev.date < dateToday){
73                     $("#asap_msg").show();
74                     $("#asap_msg").css("color","red");
75                     $("#asap_msg").html("Please select an end date today or later");
76                     $("#asap_msg").fadeOut(4000);
77                     $("#DateAsap").datepicker( "setDate", dateToday );
78                 }else{
79                     AsapDateSelected = ev.date;
80                 }
81             }).datepicker('setValue', AsapDateSelected);
82             */
83             $("#asap_schedule").click(function() {
84                 r = document.getElementById("resources");
85                 if(r.childNodes.length == 0){
86                     $("#asap_msg").show();
87                     $("#asap_msg").css("color","orange");
88                     $("#asap_msg").html("Please select at least one IoTLab resource");
89                     $("#asap_msg").fadeOut(4000);
90                 }else{
91                     start_time = 0;
92                     duration = $("#duration").val();
93                     if(!parseInt(duration)){
94                         $("#asap_msg").show();
95                         $("#asap_msg").css("color","red");
96                         $("#asap_msg").html("Please set the duration with a number");
97                         $("#asap_msg").fadeOut(4000);
98                     }else{
99                         elements = r.childNodes;
100                         // length of elements changes during loop as we remove elements one by one
101                         var size = elements.length;
102                         for (i = 0; i < size; i++) { 
103                             // lets take the first element
104                             el = elements[0];
105                             t_id = el.id.split("_");
106                             self.send_lease(t_id[1], String(start_time), duration);
107                         }
108                     }
109                 }
110             });
111         },
112
113         // a function to bind events here: click change
114         // how to raise manifold events
115         send_lease: function(resource_urn, start_time, duration)
116         {
117             var lease_key, new_lease, data;
118
119             lease_key = manifold.metadata.get_key('lease');
120
121             new_lease = {
122                 resource:   resource_urn,
123                 start_time: start_time,
124                 duration: duration,
125                 //end_time:   end_time.getTime()/1000,
126             };
127
128             // This is needed to create a hashable object
129             new_lease.hashCode = manifold.record_hashcode(lease_key.sort());
130             new_lease.equals   = manifold.record_equals(lease_key);
131
132             data = {
133                 state: STATE_SET,
134                 key  : null,
135                 op   : STATE_SET_ADD,
136                 value: new_lease
137             }
138             prev_state = manifold.query_store.get_record_state(this.options.query_uuid, resource_urn, STATE_SET);
139             manifold.raise_event(this.options.query_lease_uuid, FIELD_STATE_CHANGED, data);
140
141
142         },
143
144         /* GUI MANIPULATION */
145
146         // We advise you to write function to change behaviour of the GUI
147         // Will use naming helpers to access content _inside_ the plugin
148         // always refer to these functions in the remaining of the code
149
150         show_hide_button: function() 
151         {
152             // this.id, this.el, this.cl, this.elts
153             // same output as a jquery selector with some guarantees
154         },
155
156         /* TEMPLATES */
157
158         // see in the html template
159         // How to load a template, use of mustache
160
161         /* QUERY HANDLERS */
162
163         // How to make sure the plugin is not desynchronized
164         // He should manifest its interest in filters, fields or records
165         // functions triggered only if the proper listen is done
166
167         // no prefix
168
169         on_filter_added: function(filter)
170         {
171
172         },
173
174         // ... be sure to list all events here
175         
176         on_resources_field_state_changed: function(record)
177         {
178             if(record["op"] != null){
179                 resource = manifold.query_store.get_record(this.options.query_uuid,record['value']);
180                 console.log(resource);
181                 // TODO: Define which facilities support ASAP
182                 if(resource["authority_id"]=="iotlab"){
183                     // TODO: use filters Pending or Unconfigured
184                     if(record['op']==2){
185                         var elm = document.createElement('div');
186                         elm.innerHTML = record['value'];
187                         elm.id = "asap_"+record['value'];
188                         r = document.getElementById("resources");
189                         r.appendChild(elm);
190                     }else if(record['op']==1){
191                         this.remove("asap_"+record['value']);
192                     }
193                 }
194             }
195         },
196         on_leases_field_state_changed: function(record)
197         {
198             resource = manifold.query_store.get_record(this.options.query_uuid,record['value']['resource']);
199             console.log(resource);
200             // TODO: Define which facilities support ASAP
201             if(resource["authority_id"]=="iotlab"){
202                 // TODO: use filters Pending or Unconfigured
203                 if(record['op']==2){
204                     this.remove("asap_"+record['value']['resource']);
205                 }else if(record['op']==1){
206                     var elm = document.createElement('div');
207                     elm.innerHTML = record['value']['resource'];
208                     elm.id = "asap_"+record['value']['resource'];
209                     r = document.getElementById("resources");
210                     r.appendChild(elm);
211                 }
212             }
213         },
214
215         /* RECORD HANDLERS */
216         on_all_new_record: function(record)
217         {
218             console.log("on_all_new_record ---- ");
219             console.log(record);
220         },
221
222         /* INTERNAL FUNCTIONS */
223         _dummy: function() {
224             // only convention, not strictly enforced at the moment
225         },
226
227     });
228
229     /* Plugin registration */
230     $.plugin('AsapPlugin', AsapPlugin);
231
232     // TODO Here use cases for instanciating plugins in different ways like in the pastie.
233
234 })(jQuery);