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 \r
68             SchedulerSlots = schedulerGetSlots(60/schedulerSlotsPerHour);\r
69             //selection from table \r
70             $(window).keydown(function (evt) {\r
71                 if (evt.which == 17) { // ctrl\r
72                     schedulerCtrlPressed = true;\r
73                 }\r
74             }).keyup(function (evt) {\r
75                 if (evt.which == 17) { // ctrl\r
76                     schedulerCtrlPressed = false;\r
77                 }\r
78             });\r
79             $("#" + schedulerTblId).on('mousedown', 'td', rangeMouseDown).on('mouseup', 'td', rangeMouseUp).on('mousemove', 'td', rangeMouseMove);\r
80 \r
81             // Explain this will allow query events to be handled\r
82             // What happens when we don't define some events ?\r
83             // Some can be less efficient\r
84 \r
85             if (schedulerDebug) console.time("Listening_to_queries");\r
86             /* Listening to queries */\r
87             this.listen_query(options.query_uuid, 'all_ev');\r
88             this.listen_query(options.query_all_resources_uuid, 'all_resources');\r
89             this.listen_query(options.query_lease_uuid, 'lease');\r
90             //this.listen_query(options.query_lease_uuid, 'lease');\r
91             if (schedulerDebug) console.timeEnd("Listening_to_queries");\r
92 \r
93         },\r
94 \r
95         /* Handlers */\r
96 \r
97         /* all_ev QUERY HANDLERS Start */\r
98         on_all_ev_clear_records: function (data) {\r
99             //alert('all_ev clear_records');\r
100         },\r
101         on_all_ev_query_in_progress: function (data) {\r
102            // alert('all_ev query_in_progress');\r
103         },\r
104         on_all_ev_new_record: function (data) {\r
105             //alert('all_ev new_record');\r
106         },\r
107         on_all_ev_query_done: function (data) {\r
108             //alert('all_ev query_done');\r
109         },\r
110         //another plugin has modified something, that requires you to update your display. \r
111         on_all_ev_field_state_changed: function (data) {\r
112             //alert('all_ev query_done');\r
113         },\r
114         /* all_ev QUERY HANDLERS End */\r
115         /* all_resources QUERY HANDLERS Start */\r
116         on_all_resources_clear_records: function (data) {\r
117             //data is empty on load\r
118         },\r
119         on_all_resources_query_in_progress: function (data) {\r
120             //data is empty on load\r
121         },\r
122         on_all_resources_new_record: function (data) {\r
123             //alert(data.toSource());\r
124             if (data.exclusive == null)\r
125                 SchedulerData.push({ id: data.urn, index: SchedulerData.length, name: data.hrn, granularity: data.granularity, leases: schedulerGetLeases(60 / schedulerSlotsPerHour), type: data.type });\r
126             //alert(data.toSource());\r
127 \r
128         },\r
129         on_all_resources_query_done: function (data) {\r
130             _resourcesDone = true;\r
131             this._initScheduler();\r
132         },\r
133         //another plugin has modified something, that requires you to update your display. \r
134         on_all_resources_field_state_changed: function (data) {\r
135             //alert('all_resources query_done');\r
136         },\r
137         /* all_resources QUERY HANDLERS End */\r
138         /* lease QUERY HANDLERS Start */\r
139         on_lease_clear_records: function (data) { console.log('clear_records'); },\r
140         on_lease_query_in_progress: function (data) { console.log('lease_query_in_progress'); },\r
141         on_lease_new_record: function (data) {\r
142             tmpSchedulerLeases.push({\r
143                 id: schedulerGetSlotId(data.start_time, data.duration, data.granularity),\r
144                 slice: data.slice,\r
145                 status: 'reserved',\r
146                 resource: data.resource,\r
147                 network: data.network,\r
148                 start_time: data.start_time,\r
149                 lease_type: data.lease_type,\r
150                 granularity: data.granularity,\r
151                 duration: data.duration\r
152             });\r
153             //console.log(data.toSource()); console.log('lease_new_record');\r
154         },\r
155         on_lease_query_done: function (data) {\r
156             _leasesDone = true;\r
157             this._initScheduler();\r
158             // console.log('lease_query_done');\r
159         },\r
160         //another plugin has modified something, that requires you to update your display. \r
161         on_lease_field_state_changed: function (data) { console.log('lease_field_state_changed'); },\r
162         /* lease QUERY HANDLERS End */\r
163 \r
164 \r
165         // no prefix\r
166         on_filter_added: function (filter) {\r
167             var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
168             tmpScope.initSchedulerResources(schedulerMaxRows, filter);\r
169         },\r
170 \r
171         // ... be sure to list all events here\r
172 \r
173         /* RECORD HANDLERS */\r
174         on_all_new_record: function (record) {\r
175             //alert('on_all_new_record');\r
176         },\r
177 \r
178         debug : function (logTxt) {\r
179             if (typeof window.console != 'undefined') {\r
180                 console.debug(logTxt);\r
181             }\r
182         },\r
183 \r
184         /* INTERNAL FUNCTIONS */\r
185         _initScheduler: function () {\r
186             if (_resourcesDone && _leasesDone)\r
187             {\r
188                 /* GUI setup and event binding */\r
189                 this._FixLeases();\r
190                 this._initUI();\r
191             }\r
192         },\r
193 \r
194         _initUI: function () {\r
195             //alert(1);\r
196             if (schedulerDebug) console.time("_initUI");\r
197             //init DatePicker Start\r
198             $("#DateToRes").datepicker({\r
199                 dateFormat: "yy-mm-dd",\r
200                 minDate: 0,\r
201                 numberOfMonths: 3\r
202             }).change(function () {\r
203                 //Scheduler2.loadWithDate();\r
204             }).click(function () {\r
205                 $("#ui-datepicker-div").css("z-index", 5);\r
206             });\r
207             //End init DatePicker\r
208             \r
209             //init Table\r
210             this._FixTable();\r
211             //End init Table\r
212 \r
213             //init Slider\r
214             $('#tblSlider').slider({\r
215                 min: 0,\r
216                 max: SchedulerTotalCells - SchedulerTotalVisibleCells - 1,\r
217                 value: 0,\r
218                 slide: function (event, ui) {\r
219                     //$("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);\r
220                     //console.log(ui.value);\r
221                     var angScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
222                     if (_schedulerCurrentCellPosition > ui.value)\r
223                         angScope.moveBackSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
224                     else if (_schedulerCurrentCellPosition < ui.value)\r
225                         angScope.moveFrontSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
226                     _schedulerCurrentCellPosition = ui.value;\r
227                 }\r
228             });\r
229             //End init Slider\r
230 \r
231             //other stuff\r
232             $("#plugin-scheduler-loader").hide();\r
233             $("#plugin-scheduler").show();\r
234             //fixOddEvenClasses();\r
235             //$("#" + schedulerTblId + " td:not([class])").addClass("free");\r
236             if (schedulerDebug) console.timeEnd("_initUI");\r
237         },\r
238 \r
239         _FixLeases  : function () {\r
240             for (var i = 0; i < tmpSchedulerLeases.length; i++) {\r
241                 var tmpLea = tmpSchedulerLeases[i];\r
242                 var tmpRes = schedulerFindResourceById(SchedulerData, tmpLea.resource);\r
243                 if (tmpRes != null) {\r
244                     //alert(tmpLea.id + '-' + tmpLea.start_time);\r
245                     tmpRes.leases[tmpLea.id] = tmpLea;\r
246                 }\r
247             }\r
248         },\r
249 \r
250         _FixTable: function () {\r
251             var colWidth = 50;\r
252             SchedulerTotalCells = SchedulerSlots.length;\r
253             $('#' + schedulerTblId + ' thead tr th:eq(0)').css("width", schedulerTblFirstColWidth);\r
254             //this get width might need fix depending on the template \r
255             var tblwidth = $('#scheduler-tab').parent().outerWidth();\r
256             SchedulerTotalVisibleCells = parseInt((tblwidth - schedulerTblFirstColWidth) / colWidth);\r
257 \r
258             //if (SchedulerData.length == 0) {\r
259             //    //puth some test data\r
260             //    SchedulerData.push({ name: 'xyz+aaa', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+aaa', type: 'node' });\r
261             //    SchedulerData.push({ name: 'xyz+bbb', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+bbb', type: 'node' });\r
262             //    SchedulerData.push({ name: 'xyz+ccc', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+ccc', type: 'node' });\r
263             //    SchedulerData.push({ name: 'nitos1', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'nitos1', type: 'node' });\r
264             //}\r
265             var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
266             tmpScope.initSchedulerResources(schedulerMaxRows, null);\r
267         },\r
268         _SetPeriodInPage: function (start, end) {\r
269         }\r
270     });\r
271 \r
272     //Sched2 = new Scheduler2();\r
273 \r
274     /* Plugin registration */\r
275     $.plugin('Scheduler2', scheduler2);\r
276 \r
277     // TODO Here use cases for instanciating plugins in different ways like in the pastie.\r
278 \r
279 \r
280 })(jQuery);\r
281 \r
282 \r
283 \r