Chrysostomos for scheduler
[myslice.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 var init_start_visible_index = 10;\r
31 var init_end_visible_index = 21;\r
32 var rsvrTblNm = "scheduler-reservation-table";\r
33 var SchedulerResources = [];\r
34 var schdlr_totalColums = 0;\r
35 var SetPerFun = null;\r
36 var Sched2 = null;\r
37 var Debug = true;\r
38 var schdlr_PartsInOneHour = 6;\r
39 \r
40 (function ($) {\r
41     var Scheduler2 = Plugin.extend({\r
42 \r
43         /** XXX to check\r
44          * @brief Plugin constructor\r
45          * @param options : an associative array of setting values\r
46          * @param element : \r
47          * @return : a jQuery collection of objects on which the plugin is\r
48          *     applied, which allows to maintain chainability of calls\r
49          */\r
50         init: function (options, element) {\r
51             // Call the parent constructor, see FAQ when forgotten\r
52             this._super(options, element);\r
53 \r
54             schdlr_totalColums = $("#scheduler-reservation-table th").length;\r
55 \r
56             //selection from table \r
57             $(window).keydown(function (evt) {\r
58                 if (evt.which == 17) { // ctrl\r
59                     ctrlPressed = true;\r
60                 }\r
61             }).keyup(function (evt) {\r
62                 if (evt.which == 17) { // ctrl\r
63                     ctrlPressed = false;\r
64                 }\r
65             });\r
66             $("#" + rsvrTblNm).on('mousedown', 'td', rangeMouseDown).on('mouseup', 'td', rangeMouseUp).on('mousemove', 'td', rangeMouseMove);\r
67 \r
68             // Explain this will allow query events to be handled\r
69             // What happens when we don't define some events ?\r
70             // Some can be less efficient\r
71 \r
72             if (Debug) console.time("Listening_to_queries");\r
73             /* Listening to queries */\r
74             this.listen_query(options.query_uuid, 'all_ev');\r
75             this.listen_query(options.query_all_resources_uuid, 'all_resources');\r
76             this.listen_query(options.query_lease_uuid, 'lease');\r
77             //this.listen_query(options.query_lease_uuid, 'lease');\r
78             if (Debug) console.timeEnd("Listening_to_queries");\r
79 \r
80         },\r
81 \r
82         /* PLUGIN EVENTS */\r
83         // on_show like in querytable\r
84 \r
85 \r
86         /* GUI EVENTS */\r
87 \r
88         // a function to bind events here: click change\r
89         // how to raise manifold events\r
90 \r
91 \r
92         /* GUI MANIPULATION */\r
93 \r
94         // We advise you to write function to change behaviour of the GUI\r
95         // Will use naming helpers to access content _inside_ the plugin\r
96         // always refer to these functions in the remaining of the code\r
97 \r
98         show_hide_button: function () {\r
99             // this.id, this.el, this.cl, this.elts\r
100             // same output as a jquery selector with some guarantees\r
101         },\r
102 \r
103         drawResources: function () {\r
104             \r
105             //if (Debug) this.debug('foo');\r
106             if (Debug) console.time("each:SchedulerResources");\r
107 \r
108             //scheduler-reservation-table main table columns\r
109             totalColums = $("#scheduler-reservation-table thead tr th").length;\r
110             //var totalCell = [];\r
111             //for (var i = 0; i < totalColums; i++) { totalCell.push("<td></td>"); }\r
112             //var srt_body = [];\r
113             var totalCell = "";\r
114             for (var i = 0; i < totalColums; i++) totalCell +="<td></td>"; \r
115             var srt_body = "";\r
116 \r
117             $.each(SchedulerResources, function (i, group) {\r
118                 //var groupTR = $("#ShedulerNodes tbody").html('<tr><td class="no-image verticalIndex" rowspan="' + group.resources.length + '"><div class="verticalText">' + group.groupName + '</div></td><td id="schdlr_frstTD" class="info fixed"></td></tr>');\r
119                 var groupTR = $("#ShedulerNodes tbody").html('<tr><td class="no-image verticalIndex" rowspan="' + 30 + '"><div class="verticalText">' + group.groupName + '</div></td><td id="schdlr_frstTD" class="info fixed"></td></tr>');\r
120                 \r
121                 $.each(group.resources.slice(0,30), function (i, resource) {\r
122                     if (i == 0) {\r
123                         //$("#ShedulerNodes tbody tr:first").append('<td class="info fixed">' + resource.hostname + '</td>');\r
124                         $(groupTR).find("#schdlr_frstTD").html(resource.hostname);\r
125                         //$(srt_body).html("<tr>" + totalCell + "</tr>");\r
126                     } else {\r
127                         $(groupTR).find("tr:last").after('<tr><td class="info fixed">' + resource.hostname + '</td></tr>');\r
128                         //$(srt_body).find("tr:last").after("<tr>" + totalCell + "</tr>");\r
129                     }\r
130                     srt_body += "<tr>" + totalCell + "</tr>";\r
131                     //srt_body.push('<tr>'); srt_body = srt_body.concat(totalCell.concat()); srt_body.push('/<tr>');\r
132                 });\r
133             });\r
134 \r
135             //$("#scheduler-reservation-table tbody").html(srt_body.join(""));\r
136             $("#scheduler-reservation-table tbody").html(srt_body);\r
137 \r
138             if (Debug) console.timeEnd("each:SchedulerResources");\r
139             \r
140 \r
141             $("#" + rsvrTblNm + " tbody tr").each(function (index) { $(this).attr("data-trindex", index); });\r
142 \r
143         },\r
144 \r
145         /* TEMPLATES */\r
146 \r
147         // see in the html template\r
148         // How to load a template, use of mustache\r
149 \r
150         /* QUERY HANDLERS */\r
151         loadWithDate: function () {\r
152             // only convention, not strictly enforced at the moment\r
153         },\r
154         // How to make sure the plugin is not desynchronized\r
155         // He should manifest its interest in filters, fields or records\r
156         // functions triggered only if the proper listen is done\r
157 \r
158         /* all_ev QUERY HANDLERS Start */\r
159         on_all_ev_clear_records: function (data) {\r
160             //alert('all_ev clear_records');\r
161         },\r
162         on_all_ev_query_in_progress: function (data) {\r
163            // alert('all_ev query_in_progress');\r
164         },\r
165         on_all_ev_new_record: function (data) {\r
166             //alert('all_ev new_record');\r
167         },\r
168         on_all_ev_query_done: function (data) {\r
169             //alert('all_ev query_done');\r
170         },\r
171         //another plugin has modified something, that requires you to update your display. \r
172         on_all_ev_field_state_changed: function (data) {\r
173             //alert('all_ev query_done');\r
174         },\r
175         /* all_ev QUERY HANDLERS End */\r
176         /* all_resources QUERY HANDLERS Start */\r
177         on_all_resources_clear_records: function (data) {\r
178             //data is empty on load\r
179         },\r
180         on_all_resources_query_in_progress: function (data) {\r
181             //data is empty on load\r
182         },\r
183         on_all_resources_new_record: function (data) {\r
184             var tmpGroup = lookup(SchedulerResources, 'groupName', data.type);\r
185             if (tmpGroup == null) {\r
186                 tmpGroup = { groupName: data.type, resources: [] };\r
187                 SchedulerResources.push(tmpGroup);\r
188                 //if (data.type != "node")  alert('not all node');\r
189             }\r
190             tmpGroup.resources.push(data);\r
191             //alert('new_record');\r
192         },\r
193         on_all_resources_query_done: function (data) {\r
194             this.drawResources();\r
195             //data is empty on load\r
196             /* GUI setup and event binding */\r
197             this._initUI();\r
198             this._SetPeriodInPage(init_start_visible_index, init_end_visible_index);\r
199             this.loadWithDate();\r
200         },\r
201         //another plugin has modified something, that requires you to update your display. \r
202         on_all_resources_field_state_changed: function (data) {\r
203             //alert('all_resources query_done');\r
204         },\r
205         /* all_resources QUERY HANDLERS End */\r
206         /* lease QUERY HANDLERS Start */\r
207         on_lease_clear_records: function (data) { alert('clear_records'); },\r
208         on_lease_query_in_progress: function (data) { alert('query_in_progress'); },\r
209         on_lease_new_record: function (data) { alert('new_record'); },\r
210         on_lease_query_done: function (data) { alert('query_done'); },\r
211         //another plugin has modified something, that requires you to update your display. \r
212         on_lease_field_state_changed: function (data) { alert('query_done'); },\r
213         /* lease QUERY HANDLERS End */\r
214 \r
215 \r
216         // no prefix\r
217 \r
218         on_filter_added: function (filter) {\r
219 \r
220         },\r
221 \r
222         // ... be sure to list all events here\r
223 \r
224         /* RECORD HANDLERS */\r
225         on_all_new_record: function (record) {\r
226             //\r
227             alert('on_all_new_record');\r
228         },\r
229 \r
230         debug : function (log_txt) {\r
231             if (typeof window.console != 'undefined') {\r
232                 console.debug(log_txt);\r
233             }\r
234         },\r
235 \r
236         /* INTERNAL FUNCTIONS */\r
237         _initUI: function () {\r
238             if (Debug) console.time("_initUI");\r
239             //fix margins in tables\r
240             mtNodesTbl = $("#" + rsvrTblNm + " tr:first").outerHeight() + 6;\r
241             mtSchrollCon = $("#nodes").outerWidth();\r
242             $("#nodes").css("margin-top", mtNodesTbl);\r
243             $("#reservation-table-scroll-container").css("margin-left", mtSchrollCon);\r
244             SetPerFun = this._SetPeriodInPage;\r
245             //slider\r
246             $("#time-range").slider({\r
247                 range: true,\r
248                 min: 0,\r
249                 max: 24,\r
250                 step: 0.5,\r
251                 values: [init_start_visible_index, init_end_visible_index],\r
252                 slide: function (event, ui) {\r
253                     SetPerFun(ui.values[0], ui.values[1]);\r
254                 }\r
255             });\r
256             $("#DateToRes").datepicker({\r
257                 dateFormat: "yy-mm-dd",\r
258                 minDate: 0,\r
259                 numberOfMonths: 3\r
260             }).change(function () {\r
261                 //Scheduler2.loadWithDate();\r
262             }).click(function () {\r
263                 $("#ui-datepicker-div").css("z-index", 5);\r
264             });\r
265             //other stuff\r
266             fixOddEvenClasses();\r
267             $("#" + rsvrTblNm + " td:not([class])").addClass("free");\r
268             if (Debug) console.timeEnd("_initUI");\r
269         },\r
270         _SetPeriodInPage: function (start, end) {\r
271             if (Debug) console.time("_SetPeriodInPage");\r
272             ClearTableSelection();\r
273             $("#lbltime").html(GetTimeFromInt(start) + " - " + GetTimeFromInt(end));\r
274             \r
275             var start_visible_index = (start * schdlr_PartsInOneHour) + 1;\r
276             var end_visible_index = (end * schdlr_PartsInOneHour);\r
277 \r
278             //hide - show\r
279             for (i = 0; i < start_visible_index; i++) {\r
280                 $("#" + rsvrTblNm + " td:nth-child(" + i + "), #" + rsvrTblNm + " th:nth-child(" + i + ")").hide();\r
281             }\r
282             for (i = end_visible_index + 1; i <= schdlr_totalColums; i++) {\r
283                 $("#" + rsvrTblNm + " td:nth-child(" + i + "), #" + rsvrTblNm + " th:nth-child(" + i + ")").hide();\r
284             }\r
285             /*$("#" + rsvrTblNm + " td:not([class*='info']), #" + rsvrTblNm + " th:not([class*='fixed'])").hide();*/\r
286             for (i = start_visible_index; i <= end_visible_index; i++) {\r
287                 $("#" + rsvrTblNm + " td:nth-child(" + i + "), #" + rsvrTblNm + " th:nth-child(" + i + ")").show();\r
288             }\r
289 \r
290             if ($("#" + rsvrTblNm + " th:visible:first").width() > 105) {\r
291                 $("#" + rsvrTblNm + " th span").css("display", "inline")\r
292             } else {\r
293                 $("#" + rsvrTblNm + " th span").css("display", "block");\r
294             }\r
295             mtNodesTbl = $("#" + rsvrTblNm + " tr:first").outerHeight() + 6;\r
296             $("#nodes").css("margin-top", mtNodesTbl);\r
297             //$("#scroll_container").width($("#Search").width() - $("#nodes").width());\r
298             //$("#nodes th").height($("#tblReservation th:visible:first").height() - 2);\r
299             if (Debug) console.timeEnd("_SetPeriodInPage");\r
300         }\r
301     });\r
302 \r
303     //Sched2 = new Scheduler2();\r
304 \r
305     /* Plugin registration */\r
306     $.plugin('Scheduler2', Scheduler2);\r
307 \r
308     // TODO Here use cases for instanciating plugins in different ways like in the pastie.\r
309 \r
310 \r
311 })(jQuery);\r
312 \r
313 \r
314 \r