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.
63 dateToday = new Date();
65 $("#DateAsap").datepicker({
66 //dateFormat: "D, d M yy",
68 defaultDate: dateToday,
70 onRender: function(date) {
71 return date.valueOf() < now.valueOf() ? 'disabled' : '';
74 }).on('changeDate', function(ev) {
75 if(ev.date < dateToday){
76 $("#asap_msg").show();
77 $("#asap_msg").css("color","red");
78 $("#asap_msg").html("Please select an end date today or later");
79 $("#asap_msg").fadeOut(4000);
80 $("#DateAsap").datepicker( "setDate", dateToday );
82 AsapDateSelected = ev.date;
84 }).datepicker('setValue', AsapDateSelected);
86 .on('changeDate', function(ev) {
87 AsapDateSelected = new Date(ev.date);
88 AsapDateSelected.setHours(0,0,0,0);
89 // Set slider to origin
90 //$('#tblSlider').slider('setValue', 0); // XXX
92 self._scope_clear_leases();
93 self._set_all_lease_slots();
95 self._get_scope().$apply();
96 }).datepicker('setValue', AsapDateSelected); //.data('datepicker');
98 $("#asap_schedule").click(function() {
100 r = document.getElementById("resources");
101 if(r.childNodes.length == 0){
102 $("#asap_msg").show();
103 $("#asap_msg").css("color","orange");
104 $("#asap_msg").html("Please select at least one resource");
105 $("#asap_msg").fadeOut(4000);
108 hours = $("#hours").val();
109 minutes = $("#minutes").val();
110 AsapDateSelected.setHours(hours,minutes,0,0);
111 if(AsapDateSelected <= dateNow){
112 $("#asap_msg").show();
113 $("#asap_msg").css("color","red");
114 $("#asap_msg").html("Please select a correct end time");
115 $("#asap_msg").fadeOut(4000);
117 $.each(r.childNodes, function(i, el){
118 t_id = el.id.split("_");
119 console.log(t_id[1]);
120 self.send_lease(t_id[1], start_time, AsapDateSelected);
127 // a function to bind events here: click change
128 // how to raise manifold events
129 send_lease: function(resource_urn, start_time, end_time)
131 var lease_key, new_lease, data;
133 lease_key = manifold.metadata.get_key('lease');
136 resource: resource_urn,
137 start_time: start_time,
138 end_time: end_time.getTime()/1000,
141 // This is needed to create a hashable object
142 new_lease.hashCode = manifold.record_hashcode(lease_key.sort());
143 new_lease.equals = manifold.record_equals(lease_key);
151 prev_state = manifold.query_store.get_record_state(this.options.query_uuid, resource_urn, STATE_SET);
152 manifold.raise_event(this.options.query_lease_uuid, FIELD_STATE_CHANGED, data);
157 /* GUI MANIPULATION */
159 // We advise you to write function to change behaviour of the GUI
160 // Will use naming helpers to access content _inside_ the plugin
161 // always refer to these functions in the remaining of the code
163 show_hide_button: function()
165 // this.id, this.el, this.cl, this.elts
166 // same output as a jquery selector with some guarantees
171 // see in the html template
172 // How to load a template, use of mustache
176 // How to make sure the plugin is not desynchronized
177 // He should manifest its interest in filters, fields or records
178 // functions triggered only if the proper listen is done
182 on_filter_added: function(filter)
187 // ... be sure to list all events here
189 on_resources_field_state_changed: function(record)
191 console.log("on_resources_field_state_changed ---- ");
194 var elm = document.createElement('div');
195 elm.innerHTML = record['value'];
196 elm.id = "asap_"+record['value'];
197 r = document.getElementById("resources");
199 }else if(record['op']==1){
200 this.remove("asap_"+record['value']);
203 on_leases_field_state_changed: function(record)
207 this.remove("asap_"+record['value']['resource']);
208 }else if(record['op']==1){
209 var elm = document.createElement('div');
210 elm.innerHTML = record['value']['resource'];
211 elm.id = "asap_"+record['value']['resource'];
212 r = document.getElementById("resources");
217 /* RECORD HANDLERS */
218 on_all_new_record: function(record)
220 console.log("on_all_new_record ---- ");
224 /* INTERNAL FUNCTIONS */
226 // only convention, not strictly enforced at the moment
231 /* Plugin registration */
232 $.plugin('AsapPlugin', AsapPlugin);
234 // TODO Here use cases for instanciating plugins in different ways like in the pastie.