2 * MyPlugin: demonstration plugin
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
14 var AsapDateSelected = new Date();
18 var AsapPlugin = Plugin.extend({
21 * @brief Plugin constructor
22 * @param options : an associative array of setting values
24 * @return : a jQuery collection of objects on which the plugin is
25 * applied, which allows to maintain chainability of calls
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);
33 /* Member variables */
37 /* Setup query and record handlers */
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');
45 /* GUI setup and event binding */
51 // on_show like in querytable
54 return (elem=document.getElementById(id)).parentNode.removeChild(elem);
58 * Initialize the date picker, the table, the slider and the buttons. Once done, display scheduler.
65 dateToday = new Date();
67 $("#DateAsap").datepicker({
68 //dateFormat: "D, d M yy",
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 );
79 AsapDateSelected = ev.date;
81 }).datepicker('setValue', AsapDateSelected);
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);
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);
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
105 t_id = el.id.split("_");
106 self.send_lease(t_id[1], String(start_time), duration);
113 // a function to bind events here: click change
114 // how to raise manifold events
115 send_lease: function(resource_urn, start_time, duration)
117 var lease_key, new_lease, data;
119 lease_key = manifold.metadata.get_key('lease');
122 resource: resource_urn,
123 start_time: start_time,
125 //end_time: end_time.getTime()/1000,
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);
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);
144 /* GUI MANIPULATION */
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
150 show_hide_button: function()
152 // this.id, this.el, this.cl, this.elts
153 // same output as a jquery selector with some guarantees
158 // see in the html template
159 // How to load a template, use of mustache
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
169 on_filter_added: function(filter)
174 // ... be sure to list all events here
176 on_resources_field_state_changed: function(record)
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
185 var elm = document.createElement('div');
186 elm.innerHTML = record['value'];
187 elm.id = "asap_"+record['value'];
188 r = document.getElementById("resources");
190 }else if(record['op']==1){
191 this.remove("asap_"+record['value']);
196 on_leases_field_state_changed: function(record)
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
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");
215 /* RECORD HANDLERS */
216 on_all_new_record: function(record)
218 console.log("on_all_new_record ---- ");
222 /* INTERNAL FUNCTIONS */
224 // only convention, not strictly enforced at the moment
229 /* Plugin registration */
230 $.plugin('AsapPlugin', AsapPlugin);
232 // TODO Here use cases for instanciating plugins in different ways like in the pastie.