scheduler new version with angular
[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 //Enable Debug\r
47 var schedulerDebug = true;\r
48 \r
49 (function ($) {\r
50     var scheduler2 = Plugin.extend({\r
51 \r
52         /** XXX to check\r
53          * @brief Plugin constructor\r
54          * @param options : an associative array of setting values\r
55          * @param element : \r
56          * @return : a jQuery collection of objects on which the plugin is\r
57          *     applied, which allows to maintain chainability of calls\r
58          */\r
59         init: function (options, element) {\r
60             this.classname="scheduler2";\r
61             // Call the parent constructor, see FAQ when forgotten\r
62             this._super(options, element);\r
63 \r
64             SchedulerSlots = schedulerGetSlots(60/schedulerSlotsPerHour);\r
65             //selection from table \r
66             $(window).keydown(function (evt) {\r
67                 if (evt.which == 17) { // ctrl\r
68                     schedulerCtrlPressed = true;\r
69                 }\r
70             }).keyup(function (evt) {\r
71                 if (evt.which == 17) { // ctrl\r
72                     schedulerCtrlPressed = false;\r
73                 }\r
74             });\r
75             //$("#" + schedulerTblId).on('mousedown', 'td', rangeMouseDown).on('mouseup', 'td', rangeMouseUp).on('mousemove', 'td', rangeMouseMove);\r
76 \r
77             // Explain this will allow query events to be handled\r
78             // What happens when we don't define some events ?\r
79             // Some can be less efficient\r
80 \r
81             if (schedulerDebug) console.time("Listening_to_queries");\r
82             /* Listening to queries */\r
83             this.listen_query(options.query_uuid, 'all_ev');\r
84             this.listen_query(options.query_all_resources_uuid, 'all_resources');\r
85             this.listen_query(options.query_lease_uuid, 'lease');\r
86             //this.listen_query(options.query_lease_uuid, 'lease');\r
87             if (schedulerDebug) console.timeEnd("Listening_to_queries");\r
88 \r
89         },\r
90 \r
91         /* Handlers */\r
92 \r
93         /* all_ev QUERY HANDLERS Start */\r
94         on_all_ev_clear_records: function (data) {\r
95             //alert('all_ev clear_records');\r
96         },\r
97         on_all_ev_query_in_progress: function (data) {\r
98            // alert('all_ev query_in_progress');\r
99         },\r
100         on_all_ev_new_record: function (data) {\r
101             //alert('all_ev new_record');\r
102         },\r
103         on_all_ev_query_done: function (data) {\r
104             //alert('all_ev query_done');\r
105         },\r
106         //another plugin has modified something, that requires you to update your display. \r
107         on_all_ev_field_state_changed: function (data) {\r
108             //alert('all_ev query_done');\r
109         },\r
110         /* all_ev QUERY HANDLERS End */\r
111         /* all_resources QUERY HANDLERS Start */\r
112         on_all_resources_clear_records: function (data) {\r
113             //data is empty on load\r
114         },\r
115         on_all_resources_query_in_progress: function (data) {\r
116             //data is empty on load\r
117         },\r
118         on_all_resources_new_record: function (data) {\r
119             if (SchedulerData.length < schedulerMaxRows)\r
120                 SchedulerData.push({ name: data.hrn, leases: schedulerGetLeases(schedulerSlotsPerHour), urn: data.urn, type: data.type });\r
121             //alert(data.toSource());\r
122         },\r
123         on_all_resources_query_done: function (data) {\r
124             /* GUI setup and event binding */\r
125             this._initUI();\r
126             //this.loadWithDate();\r
127         },\r
128         //another plugin has modified something, that requires you to update your display. \r
129         on_all_resources_field_state_changed: function (data) {\r
130             //alert('all_resources query_done');\r
131         },\r
132         /* all_resources QUERY HANDLERS End */\r
133         /* lease QUERY HANDLERS Start */\r
134         on_lease_clear_records: function (data) { console.log('clear_records'); },\r
135         on_lease_query_in_progress: function (data) { console.log('lease_query_in_progress'); },\r
136         on_lease_new_record: function (data) { alert(data.toSource()); console.log('lease_new_record'); },\r
137         on_lease_query_done: function (data) { console.log('lease_query_done'); },\r
138         //another plugin has modified something, that requires you to update your display. \r
139         on_lease_field_state_changed: function (data) { console.log('lease_field_state_changed'); },\r
140         /* lease QUERY HANDLERS End */\r
141 \r
142 \r
143         // no prefix\r
144 \r
145         on_filter_added: function (filter) {\r
146 \r
147         },\r
148 \r
149         // ... be sure to list all events here\r
150 \r
151         /* RECORD HANDLERS */\r
152         on_all_new_record: function (record) {\r
153             //alert('on_all_new_record');\r
154         },\r
155 \r
156         debug : function (logTxt) {\r
157             if (typeof window.console != 'undefined') {\r
158                 console.debug(logTxt);\r
159             }\r
160         },\r
161 \r
162         /* INTERNAL FUNCTIONS */\r
163         _initUI: function () {\r
164             if (schedulerDebug) console.time("_initUI");\r
165             //init DatePicker Start\r
166             $("#DateToRes").datepicker({\r
167                 dateFormat: "yy-mm-dd",\r
168                 minDate: 0,\r
169                 numberOfMonths: 3\r
170             }).change(function () {\r
171                 //Scheduler2.loadWithDate();\r
172             }).click(function () {\r
173                 $("#ui-datepicker-div").css("z-index", 5);\r
174             });\r
175             //End init DatePicker\r
176             \r
177             //init Table\r
178             this._FixTable();\r
179             //End init Table\r
180 \r
181             //init Slider\r
182             $('#tblSlider').slider({\r
183                 min: 0,\r
184                 max: SchedulerTotalCells - SchedulerTotalVisibleCells - 1,\r
185                 value: 0,\r
186                 slide: function (event, ui) {\r
187                     //$("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);\r
188                     //console.log(ui.value);\r
189                     if (_schedulerCurrentCellPosition > ui.value)\r
190                         angular.element(document.getElementById('SchedulerCtrl')).scope().moveBackSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
191                     else if (_schedulerCurrentCellPosition < ui.value)\r
192                         angular.element(document.getElementById('SchedulerCtrl')).scope().moveFrontSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
193                     _schedulerCurrentCellPosition = ui.value;\r
194                 }\r
195             });\r
196             //End init Slider\r
197 \r
198             //other stuff\r
199             //fixOddEvenClasses();\r
200             //$("#" + schedulerTblId + " td:not([class])").addClass("free");\r
201             if (schedulerDebug) console.timeEnd("_initUI");\r
202         },\r
203         _FixTable: function () {\r
204             var colWidth = 50;\r
205             SchedulerTotalCells = SchedulerSlots.length;\r
206             $('#' + schedulerTblId + ' thead tr th:eq(0)').css("width", schedulerTblFirstColWidth);\r
207             //this get width might need fix depending on the template \r
208             var tblwidth = $('#scheduler-tab').parent().outerWidth();\r
209             SchedulerTotalVisibleCells = parseInt((tblwidth - schedulerTblFirstColWidth) / colWidth);\r
210 \r
211             if (SchedulerData.length == 0) {\r
212                 //puth some test data\r
213                 SchedulerData.push({ name: 'xyz+aaa', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+aaa', type: 'node' });\r
214                 SchedulerData.push({ name: 'xyz+bbb', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+bbb', type: 'node' });\r
215                 SchedulerData.push({ name: 'xyz+ccc', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+ccc', type: 'node' });\r
216                 SchedulerData.push({ name: 'nitos1', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'nitos1', type: 'node' });\r
217             }\r
218             angular.element(document.getElementById('SchedulerCtrl')).scope().initSlots(0, SchedulerTotalVisibleCells);\r
219         },\r
220         _SetPeriodInPage: function (start, end) {\r
221         }\r
222     });\r
223 \r
224     //Sched2 = new Scheduler2();\r
225 \r
226     /* Plugin registration */\r
227     $.plugin('Scheduler2', scheduler2);\r
228 \r
229     // TODO Here use cases for instanciating plugins in different ways like in the pastie.\r
230 \r
231 \r
232 })(jQuery);\r
233 \r
234 \r
235 \r