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