Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
[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 = 50;\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 (SchedulerData.length < schedulerMaxRows)\r
125                 SchedulerData.push({ id: data.urn, name: data.hrn, leases: schedulerGetLeases(60 / schedulerSlotsPerHour), type: data.type });\r
126             //alert(data.toSource());\r
127         },\r
128         on_all_resources_query_done: function (data) {\r
129             _resourcesDone = true;\r
130             this._initScheduler();\r
131         },\r
132         //another plugin has modified something, that requires you to update your display. \r
133         on_all_resources_field_state_changed: function (data) {\r
134             //alert('all_resources query_done');\r
135         },\r
136         /* all_resources QUERY HANDLERS End */\r
137         /* lease QUERY HANDLERS Start */\r
138         on_lease_clear_records: function (data) { console.log('clear_records'); },\r
139         on_lease_query_in_progress: function (data) { console.log('lease_query_in_progress'); },\r
140         on_lease_new_record: function (data) {\r
141             tmpSchedulerLeases.push({\r
142                 id: schedulerGetSlotId(data.start_time, data.duration, data.granularity),\r
143                 slice: data.slice,\r
144                 status: 'reserved',\r
145                 resource: data.resource,\r
146                 network: data.network,\r
147                 start_time: data.start_time,\r
148                 lease_type: data.lease_type,\r
149                 granularity: data.granularity,\r
150                 duration: data.duration\r
151             });\r
152             //console.log(data.toSource()); console.log('lease_new_record');\r
153         },\r
154         on_lease_query_done: function (data) {\r
155             _leasesDone = true;\r
156             this._initScheduler();\r
157             // console.log('lease_query_done');\r
158         },\r
159         //another plugin has modified something, that requires you to update your display. \r
160         on_lease_field_state_changed: function (data) { console.log('lease_field_state_changed'); },\r
161         /* lease QUERY HANDLERS End */\r
162 \r
163 \r
164         // no prefix\r
165 \r
166         on_filter_added: function (filter) {\r
167 \r
168         },\r
169 \r
170         // ... be sure to list all events here\r
171 \r
172         /* RECORD HANDLERS */\r
173         on_all_new_record: function (record) {\r
174             //alert('on_all_new_record');\r
175         },\r
176 \r
177         debug : function (logTxt) {\r
178             if (typeof window.console != 'undefined') {\r
179                 console.debug(logTxt);\r
180             }\r
181         },\r
182 \r
183         /* INTERNAL FUNCTIONS */\r
184         _initScheduler: function () {\r
185             if (_resourcesDone && _leasesDone)\r
186             {\r
187                 /* GUI setup and event binding */\r
188                 this._FixLeases();\r
189                 this._initUI();\r
190             }\r
191         },\r
192 \r
193         _initUI : function () {\r
194             if (schedulerDebug) console.time("_initUI");\r
195             //init DatePicker Start\r
196             $("#DateToRes").datepicker({\r
197                 dateFormat: "yy-mm-dd",\r
198                 minDate: 0,\r
199                 numberOfMonths: 3\r
200             }).change(function () {\r
201                 //Scheduler2.loadWithDate();\r
202             }).click(function () {\r
203                 $("#ui-datepicker-div").css("z-index", 5);\r
204             });\r
205             //End init DatePicker\r
206             \r
207             //init Table\r
208             this._FixTable();\r
209             //End init Table\r
210 \r
211             //init Slider\r
212             $('#tblSlider').slider({\r
213                 min: 0,\r
214                 max: SchedulerTotalCells - SchedulerTotalVisibleCells - 1,\r
215                 value: 0,\r
216                 slide: function (event, ui) {\r
217                     //$("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);\r
218                     //console.log(ui.value);\r
219                     if (_schedulerCurrentCellPosition > ui.value)\r
220                         angular.element(document.getElementById('SchedulerCtrl')).scope().moveBackSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
221                     else if (_schedulerCurrentCellPosition < ui.value)\r
222                         angular.element(document.getElementById('SchedulerCtrl')).scope().moveFrontSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
223                     _schedulerCurrentCellPosition = ui.value;\r
224                 }\r
225             });\r
226             //End init Slider\r
227 \r
228             //other stuff\r
229             $("#plugin-scheduler-loader").hide();\r
230             $("#plugin-scheduler").show();\r
231 \r
232             //fixOddEvenClasses();\r
233             //$("#" + schedulerTblId + " td:not([class])").addClass("free");\r
234             if (schedulerDebug) console.timeEnd("_initUI");\r
235         },\r
236 \r
237         _FixLeases  : function () {\r
238             for (var i = 0; i < tmpSchedulerLeases.length; i++) {\r
239                 var tmpLea = tmpSchedulerLeases[i];\r
240                 var tmpRes = schedulerFindResourceById(SchedulerData, tmpLea.resource);\r
241                 if (tmpRes != null) {\r
242                     //alert(tmpLea.id + '-' + tmpLea.start_time);\r
243                     tmpRes.leases[tmpLea.id] = tmpLea;\r
244                 }\r
245             }\r
246         },\r
247 \r
248         _FixTable: function () {\r
249             var colWidth = 50;\r
250             SchedulerTotalCells = SchedulerSlots.length;\r
251             $('#' + schedulerTblId + ' thead tr th:eq(0)').css("width", schedulerTblFirstColWidth);\r
252             //this get width might need fix depending on the template \r
253             var tblwidth = $('#scheduler-tab').parent().outerWidth();\r
254             SchedulerTotalVisibleCells = parseInt((tblwidth - schedulerTblFirstColWidth) / colWidth);\r
255 \r
256             //if (SchedulerData.length == 0) {\r
257             //    //puth some test data\r
258             //    SchedulerData.push({ name: 'xyz+aaa', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+aaa', type: 'node' });\r
259             //    SchedulerData.push({ name: 'xyz+bbb', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+bbb', type: 'node' });\r
260             //    SchedulerData.push({ name: 'xyz+ccc', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+ccc', type: 'node' });\r
261             //    SchedulerData.push({ name: 'nitos1', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'nitos1', type: 'node' });\r
262             //}\r
263             angular.element(document.getElementById('SchedulerCtrl')).scope().initSlots(0, SchedulerTotalVisibleCells);\r
264         },\r
265         _SetPeriodInPage: function (start, end) {\r
266         }\r
267     });\r
268 \r
269     //Sched2 = new Scheduler2();\r
270 \r
271     /* Plugin registration */\r
272     $.plugin('Scheduler2', scheduler2);\r
273 \r
274     // TODO Here use cases for instanciating plugins in different ways like in the pastie.\r
275 \r
276 \r
277 })(jQuery);\r
278 \r
279 \r
280 \r