Chrysostomos for scheduler
[unfold.git] / plugins / scheduler2 / static / js / scheduler2.js
1 /*\r
2 #\r
3 # Copyright (c) 2013 NITLab, University of Thessaly, CERTH, Greece\r
4 #\r
5 # Permission is hereby granted, free of charge, to any person obtaining a copy\r
6 # of this software and associated documentation files (the "Software"), to deal\r
7 # in the Software without restriction, including without limitation the rights\r
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
9 # copies of the Software, and to permit persons to whom the Software is\r
10 # furnished to do so, subject to the following conditions:\r
11 #\r
12 # The above copyright notice and this permission notice shall be included in\r
13 # all copies or substantial portions of the Software.\r
14 #\r
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE\r
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
21 # THE SOFTWARE.\r
22 #\r
23 #\r
24 # This is a MySlice plugin for the NITOS Scheduler\r
25 # Nitos Scheduler v1\r
26 #\r
27 */\r
28 \r
29 /* some params */\r
30 //is ctrl keyboard button pressed\r
31 var schedulerCtrlPressed = false;\r
32 //table Id\r
33 var schedulerTblId = "scheduler-reservation-table";\r
34 var schedulerTblFirstColWidth = 150;\r
35 //Some Data\r
36 var schedulerSlotsPerHour = 6;\r
37 var schedulerMaxRows = 12;\r
38 var SchedulerData = [];\r
39 var SchedulerSlots = [];\r
40 var SchedulerDataViewData = [];\r
41 var SchedulerSlotsViewData = [];\r
42 var SchedulerTotalCells;\r
43 var SchedulerTotalVisibleCells;\r
44 //Help Variables\r
45 var _schedulerCurrentCellPosition = 0;\r
46 var _leasesDone = false;\r
47 var _resourcesDone = false;\r
48 //Enable Debug\r
49 var schedulerDebug = false;\r
50 //tmp to delete\r
51 var tmpSchedulerLeases = [];\r
52 \r
53 (function ($) {\r
54     var scheduler2 = Plugin.extend({\r
55 \r
56         /** XXX to check\r
57          * @brief Plugin constructor\r
58          * @param options : an associative array of setting values\r
59          * @param element : \r
60          * @return : a jQuery collection of objects on which the plugin is\r
61          *     applied, which allows to maintain chainability of calls\r
62          */\r
63         init: function (options, element) {\r
64             this.classname="scheduler2";\r
65             // Call the parent constructor, see FAQ when forgotten\r
66             this._super(options, element);\r
67             // We need to remember the active filter for datatables filtering
68             this.filters = Array();\r
69 \r
70 \r
71             SchedulerSlots = schedulerGetSlots(60/schedulerSlotsPerHour);\r
72             //selection from table \r
73             $(window).keydown(function (evt) {\r
74                 if (evt.which == 17) { // ctrl\r
75                     schedulerCtrlPressed = true;\r
76                 }\r
77             }).keyup(function (evt) {\r
78                 if (evt.which == 17) { // ctrl\r
79                     schedulerCtrlPressed = false;\r
80                 }\r
81             });\r
82             $("#" + schedulerTblId).on('mousedown', 'td', rangeMouseDown).on('mouseup', 'td', rangeMouseUp).on('mousemove', 'td', rangeMouseMove);\r
83 \r
84             // Explain this will allow query events to be handled\r
85             // What happens when we don't define some events ?\r
86             // Some can be less efficient\r
87 \r
88             if (schedulerDebug) console.time("Listening_to_queries");\r
89             /* Listening to queries */\r
90
91             this.listen_query(options.query_uuid);
92             this.listen_query(options.query_all_uuid, 'all');\r
93             this.listen_query(options.query_all_resources_uuid, 'all_resources');\r
94             this.listen_query(options.query_lease_uuid, 'lease');\r
95             //this.listen_query(options.query_lease_uuid, 'lease');\r
96             if (schedulerDebug) console.timeEnd("Listening_to_queries");\r
97 \r
98         },\r
99 \r
100         /* Handlers */\r
101 \r
102         /* all_ev QUERY HANDLERS Start */\r
103         on_all_ev_clear_records: function (data) {\r
104             //alert('all_ev clear_records');\r
105         },\r
106         on_all_ev_query_in_progress: function (data) {\r
107            // alert('all_ev query_in_progress');\r
108         },\r
109         on_all_ev_new_record: function (data) {\r
110             //alert('all_ev new_record');\r
111         },\r
112         on_all_ev_query_done: function (data) {\r
113             //alert('all_ev query_done');\r
114         },\r
115         //another plugin has modified something, that requires you to update your display. \r
116         on_all_ev_field_state_changed: function (data) {\r
117             //alert('all_ev query_done');\r
118         },\r
119         /* all_ev QUERY HANDLERS End */\r
120         /* all_resources QUERY HANDLERS Start */\r
121         on_all_resources_clear_records: function (data) {\r
122             //data is empty on load\r
123         },\r
124         on_all_resources_query_in_progress: function (data) {\r
125             //data is empty on load\r
126         },\r
127         on_all_resources_new_record: function (data) {\r
128             //alert(data.toSource());\r
129             if (data.exclusive == true)\r
130                 SchedulerData.push({\r
131                     id: data.urn,\r
132                     index: SchedulerData.length,\r
133                     name: data.hrn,\r
134                     granularity: data.granularity,\r
135                     leases: schedulerGetLeases(60 / schedulerSlotsPerHour),\r
136                     type: data.type,\r
137                     org_resource: data\r
138                 });\r
139             //alert(data.toSource());\r
140 \r
141         },\r
142         on_all_resources_query_done: function (data) {\r
143             _resourcesDone = true;\r
144             this._initScheduler();\r
145         },\r
146         //another plugin has modified something, that requires you to update your display. \r
147         on_all_resources_field_state_changed: function (data) {\r
148             //alert('all_resources query_done');\r
149         },\r
150         /* all_resources QUERY HANDLERS End */\r
151         /* lease QUERY HANDLERS Start */\r
152         on_lease_clear_records: function (data) { console.log('clear_records'); },\r
153         on_lease_query_in_progress: function (data) { console.log('lease_query_in_progress'); },\r
154         on_lease_new_record: function (data) {\r
155             tmpSchedulerLeases.push({\r
156                 id: schedulerGetSlotId(data.start_time, data.duration, data.granularity),\r
157                 slice: data.slice,\r
158                 status: 'reserved',\r
159                 resource: data.resource,\r
160                 network: data.network,\r
161                 start_time: new Date(data.start_time * 1000),\r
162                 start_time_unixtimestamp: data.start_time,\r
163                 lease_type: data.lease_type,\r
164                 granularity: data.granularity,\r
165                 duration: data.duration\r
166             });\r
167             //console.log(data.toSource()); console.log('lease_new_record');\r
168         },\r
169         on_lease_query_done: function (data) {\r
170             _leasesDone = true;\r
171             this._initScheduler();\r
172             // console.log('lease_query_done');\r
173         },\r
174         //another plugin has modified something, that requires you to update your display. \r
175         on_lease_field_state_changed: function (data) { console.log('lease_field_state_changed'); },\r
176         /* lease QUERY HANDLERS End */\r
177 \r
178 \r
179         // no prefix\r
180         on_filter_added: function (filter) {\r
181             this.filters.push(filter);\r
182             this._SetFiletredResources(this.filters);\r
183             //angular and UI\r
184             var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
185             if (SchedulerDataViewData.length == 0) {\r
186                 $("#plugin-scheduler").hide();\r
187                 $("#plugin-scheduler-empty").show();\r
188                 tmpScope.clearStuff();\r
189             } else {\r
190                 $("#plugin-scheduler-empty").hide();\r
191                 $("#plugin-scheduler").show();\r
192                 tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length);\r
193             }\r
194         },
195
196         on_filter_removed: function (filter) {
197             // Remove corresponding filters
198             this.filters = $.grep(this.filters, function (x) {\r
199                 return x == filter;\r
200             });\r
201             this._SetFiletredResources(this.filters);\r
202             //angular and UI\r
203             var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
204             if (SchedulerDataViewData.length == 0) {\r
205                 $("#plugin-scheduler").hide();\r
206                 $("#plugin-scheduler-empty").show();\r
207                 tmpScope.clearStuff();\r
208             } else {\r
209                 $("#plugin-scheduler-empty").hide();\r
210                 $("#plugin-scheduler").show();\r
211                 tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length);\r
212             }\r
213         },
214
215         on_filter_clear: function () {\r
216             this.filters = [];\r
217             this._SetFiletredResources(this.filters);\r
218             //angular and UI\r
219             var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
220             if (SchedulerDataViewData.length == 0) {\r
221                 $("#plugin-scheduler").hide();\r
222                 $("#plugin-scheduler-empty").show();\r
223                 tmpScope.clearStuff();\r
224             } else {\r
225                 $("#plugin-scheduler-empty").hide();\r
226                 $("#plugin-scheduler").show();\r
227                 tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length);\r
228             }\r
229         },\r
230 \r
231         // ... be sure to list all events here\r
232 \r
233         /* RECORD HANDLERS */\r
234         on_all_new_record: function (record) {\r
235             //alert('on_all_new_record');\r
236         },\r
237 \r
238         debug : function (logTxt) {\r
239             if (typeof window.console != 'undefined') {\r
240                 console.debug(logTxt);\r
241             }\r
242         },\r
243 \r
244         /* INTERNAL FUNCTIONS */\r
245         _initScheduler: function () {\r
246             if (_resourcesDone && _leasesDone)\r
247             {\r
248                 SchedulerDataViewData = SchedulerData;\r
249                 /* GUI setup and event binding */\r
250                 this._FixLeases();\r
251                 this._initUI();\r
252             }\r
253         },\r
254 \r
255         _initUI: function () {\r
256             //alert(1);\r
257             if (schedulerDebug) console.time("_initUI");\r
258             //init DatePicker Start\r
259             $("#DateToRes").datepicker({\r
260                 dateFormat: "yy-mm-dd",\r
261                 minDate: 0,\r
262                 numberOfMonths: 3\r
263             }).change(function () {\r
264                 //Scheduler2.loadWithDate();\r
265             }).click(function () {\r
266                 $("#ui-datepicker-div").css("z-index", 5);\r
267             });\r
268             //End init DatePicker\r
269             \r
270             //init Table\r
271             this._FixTable();\r
272             //End init Table\r
273 \r
274             //init Slider\r
275             $('#tblSlider').slider({\r
276                 min: 0,\r
277                 max: SchedulerTotalCells - SchedulerTotalVisibleCells,\r
278                 value: 0,\r
279                 slide: function (event, ui) {\r
280                     //$("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);\r
281                     //console.log(ui.value);\r
282                     var angScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
283                     if (_schedulerCurrentCellPosition > ui.value) {\r
284                         angScope.moveBackSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
285                     }\r
286                     else if (_schedulerCurrentCellPosition < ui.value) {\r
287                         angScope.moveFrontSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
288                     }\r
289                     _schedulerCurrentCellPosition = ui.value;\r
290                 }\r
291             });\r
292             //End init Slider\r
293 \r
294             //other stuff\r
295             $("#plugin-scheduler-loader").hide();\r
296             $("#plugin-scheduler").show();\r
297             //fixOddEvenClasses();\r
298             //$("#" + schedulerTblId + " td:not([class])").addClass("free");\r
299             if (schedulerDebug) console.timeEnd("_initUI");\r
300         },\r
301 \r
302         _FixLeases  : function () {\r
303             for (var i = 0; i < tmpSchedulerLeases.length; i++) {\r
304                 var tmpLea = tmpSchedulerLeases[i];\r
305                 var tmpRes = schedulerFindResourceById(SchedulerData, tmpLea.resource);\r
306                 if (tmpRes != null) {\r
307                     //alert(tmpLea.id + '-' + tmpLea.start_time);\r
308                     tmpRes.leases[tmpLea.id] = tmpLea;\r
309                 }\r
310             }\r
311         },\r
312 \r
313         _FixTable: function () {\r
314             var colWidth = 50;\r
315             SchedulerTotalCells = SchedulerSlots.length;\r
316             $('#' + schedulerTblId + ' thead tr th:eq(0)').css("width", schedulerTblFirstColWidth); //.css("display", "block");\r
317             //this get width might need fix depending on the template \r
318             var tblwidth = $('#scheduler-tab').parent().outerWidth();\r
319             SchedulerTotalVisibleCells = parseInt((tblwidth - schedulerTblFirstColWidth) / colWidth);\r
320 \r
321             //if (SchedulerData.length == 0) {\r
322             //    //puth some test data\r
323             //    SchedulerData.push({ name: 'xyz+aaa', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+aaa', type: 'node' });\r
324             //    SchedulerData.push({ name: 'xyz+bbb', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+bbb', type: 'node' });\r
325             //    SchedulerData.push({ name: 'xyz+ccc', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+ccc', type: 'node' });\r
326             //    SchedulerData.push({ name: 'nitos1', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'nitos1', type: 'node' });\r
327             //}\r
328             var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
329             tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length);\r
330 \r
331         },\r
332 \r
333         _SetFiletredResources : function (filters) {\r
334             if (filters.length > 0) {\r
335                 SchedulerDataViewData = new Array();\r
336                 var tmpAddIt = true;\r
337                 for (var i = 0; i < SchedulerData.length; i++) {\r
338                     loopfilters:\r
339                     for (var f = 0; f < filters.length; f++) {\r
340                         tmpAddIt = this._FilterResource(SchedulerData[i], filters[f]);\r
341                         if (tmpAddIt == false) break loopfilters;\r
342                     }\r
343                     if (tmpAddIt) {\r
344                         SchedulerDataViewData.push(SchedulerData[i]);\r
345                     }\r
346                 }\r
347             } else {\r
348                 SchedulerDataViewData = SchedulerData;\r
349             }\r
350         },\r
351 \r
352         _FilterResource: function (resource, filter) {\r
353             var key = filter[0];\r
354             var op = filter[1];\r
355             var value = filter[2];\r
356             var colValue = resource.org_resource[key];\r
357             var ret = true;\r
358 \r
359             if (op == '=' || op == '==') {\r
360                 if (colValue != value || colValue == null || colValue == "" || colValue == "n/a")
361                     ret = false;\r
362             } else if (op == 'included') {\r
363                 $.each(value, function (i, x) {\r
364                     if (x == colValue) {\r
365                         ret = true;\r
366                         return false;\r
367                     } else {\r
368                         ret = false;\r
369                     }\r
370                 });\r
371             } else if (op == '!=') {\r
372                 if (colValue == value || colValue == null || colValue == "" || colValue == "n/a")
373                     ret = false;\r
374             }\r
375 \r
376             return ret;\r
377         },\r
378 \r
379         _SetPeriodInPage: function (start, end) {\r
380         }\r
381     });\r
382 \r
383     //Sched2 = new Scheduler2();\r
384 \r
385     /* Plugin registration */\r
386     $.plugin('Scheduler2', scheduler2);\r
387 \r
388     // TODO Here use cases for instanciating plugins in different ways like in the pastie.\r
389 \r
390 \r
391 })(jQuery);\r
392 \r
393 \r
394 \r