scheduler new version with angular
[myslice.git] / plugins / scheduler2 / static / js / scheduler2.js
index f883002..07c81aa 100755 (executable)
 */\r
 \r
 /* some params */\r
-var init_start_visible_index = 10;\r
-var init_end_visible_index = 21;\r
-var rsvrTblNm = "scheduler-reservation-table";\r
-var SchedulerResources = [];\r
-var schdlr_totalColums = 0;\r
-var SetPerFun = null;\r
-var Sched2 = null;\r
-var Debug = true;\r
-var schdlr_PartsInOneHour = 6;\r
+//is ctrl keyboard button pressed\r
+var schedulerCtrlPressed = false;\r
+//table Id\r
+var schedulerTblId = "scheduler-reservation-table";\r
+var schedulerTblFirstColWidth = 150;\r
+//Some Data\r
+var schedulerSlotsPerHour = 6;\r
+var schedulerMaxRows = 50;\r
+var SchedulerData = [];\r
+var SchedulerSlots = [];\r
+var SchedulerDataViewData = [];\r
+var SchedulerSlotsViewData = [];\r
+var SchedulerTotalCells;\r
+var SchedulerTotalVisibleCells;\r
+//Help Variables\r
+var _schedulerCurrentCellPosition = 0;\r
+//Enable Debug\r
+var schedulerDebug = true;\r
 \r
 (function ($) {\r
-    var Scheduler2 = Plugin.extend({\r
+    var scheduler2 = Plugin.extend({\r
 \r
         /** XXX to check\r
          * @brief Plugin constructor\r
@@ -52,115 +61,34 @@ var schdlr_PartsInOneHour = 6;
             // Call the parent constructor, see FAQ when forgotten\r
             this._super(options, element);\r
 \r
-            schdlr_totalColums = $("#scheduler-reservation-table th").length;\r
-\r
+            SchedulerSlots = schedulerGetSlots(60/schedulerSlotsPerHour);\r
             //selection from table \r
             $(window).keydown(function (evt) {\r
                 if (evt.which == 17) { // ctrl\r
-                    ctrlPressed = true;\r
+                    schedulerCtrlPressed = true;\r
                 }\r
             }).keyup(function (evt) {\r
                 if (evt.which == 17) { // ctrl\r
-                    ctrlPressed = false;\r
+                    schedulerCtrlPressed = false;\r
                 }\r
             });\r
-            $("#" + rsvrTblNm).on('mousedown', 'td', rangeMouseDown).on('mouseup', 'td', rangeMouseUp).on('mousemove', 'td', rangeMouseMove);\r
+            //$("#" + schedulerTblId).on('mousedown', 'td', rangeMouseDown).on('mouseup', 'td', rangeMouseUp).on('mousemove', 'td', rangeMouseMove);\r
 \r
             // Explain this will allow query events to be handled\r
             // What happens when we don't define some events ?\r
             // Some can be less efficient\r
 \r
-            if (Debug) console.time("Listening_to_queries");\r
+            if (schedulerDebug) console.time("Listening_to_queries");\r
             /* Listening to queries */\r
             this.listen_query(options.query_uuid, 'all_ev');\r
             this.listen_query(options.query_all_resources_uuid, 'all_resources');\r
             this.listen_query(options.query_lease_uuid, 'lease');\r
             //this.listen_query(options.query_lease_uuid, 'lease');\r
-            if (Debug) console.timeEnd("Listening_to_queries");\r
+            if (schedulerDebug) console.timeEnd("Listening_to_queries");\r
 \r
-            $("#ShedulerNodes tbody").html('<tr><td id="schdlr_frstTD" style="background:transparent; height:45px; border:none;"></td></tr>');\r
         },\r
 \r
-        /* PLUGIN EVENTS */\r
-        // on_show like in querytable\r
-\r
-\r
-        /* GUI EVENTS */\r
-\r
-        // a function to bind events here: click change\r
-        // how to raise manifold events\r
-\r
-\r
-        /* GUI MANIPULATION */\r
-\r
-        // We advise you to write function to change behaviour of the GUI\r
-        // Will use naming helpers to access content _inside_ the plugin\r
-        // always refer to these functions in the remaining of the code\r
-\r
-        show_hide_button: function () {\r
-            // this.id, this.el, this.cl, this.elts\r
-            // same output as a jquery selector with some guarantees\r
-        },\r
-\r
-        //drawResources: function () {\r
-        drawLeases: function () {\r
-            \r
-            //if (Debug) this.debug('foo');\r
-            if (Debug) console.time("each:SchedulerResources");\r
-\r
-            //scheduler-reservation-table main table columns\r
-            totalColums = $("#scheduler-reservation-table thead tr th").length;\r
-            //var totalCell = [];\r
-            //for (var i = 0; i < totalColums; i++) { totalCell.push("<td></td>"); }\r
-            //var srt_body = [];\r
-            var totalCell = "";\r
-            for (var i = 0; i < totalColums; i++) totalCell +="<td></td>"; \r
-            var srt_body = "";\r
-            /*\r
-            $.each(SchedulerResources, function (i, group) {\r
-                console.log(group.groupName);\r
-                //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
-                //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
-                var groupTR = $("#ShedulerNodes tbody").html('<tr><td id="schdlr_frstTD" class="info fixed"></td></tr>');\r
-                \r
-                //$.each(group.resources.slice(0,30), function (i, resource) {\r
-                $.each(group.resources, function (i, resource) {\r
-                    if (i == 0) {\r
-                        //$("#ShedulerNodes tbody tr:first").append('<td class="info fixed">' + resource.hostname + '</td>');\r
-                        $(groupTR).find("#schdlr_frstTD").html(resource.urn);\r
-                        //$(srt_body).html("<tr>" + totalCell + "</tr>");\r
-                    } else {\r
-                        $(groupTR).find("tr:last").after('<tr><td class="info fixed">' + resource.urn + '</td></tr>');\r
-                        //$(srt_body).find("tr:last").after("<tr>" + totalCell + "</tr>");\r
-                    }\r
-                    srt_body += "<tr>" + totalCell + "</tr>";\r
-                    //srt_body.push('<tr>'); srt_body = srt_body.concat(totalCell.concat()); srt_body.push('/<tr>');\r
-                });\r
-            });\r
-            */\r
-            srt_body += "<tr>" + totalCell + "</tr>";\r
-            //$("#scheduler-reservation-table tbody").html(srt_body.join(""));\r
-            $("#scheduler-reservation-table tbody").append(srt_body);\r
-\r
-            if (Debug) console.timeEnd("each:SchedulerResources");\r
-            \r
-\r
-            $("#" + rsvrTblNm + " tbody tr").each(function (index) { $(this).attr("data-trindex", index); });\r
-\r
-        },\r
-\r
-        /* TEMPLATES */\r
-\r
-        // see in the html template\r
-        // How to load a template, use of mustache\r
-\r
-        /* QUERY HANDLERS */\r
-        loadWithDate: function () {\r
-            // only convention, not strictly enforced at the moment\r
-        },\r
-        // How to make sure the plugin is not desynchronized\r
-        // He should manifest its interest in filters, fields or records\r
-        // functions triggered only if the proper listen is done\r
+        /* Handlers */\r
 \r
         /* all_ev QUERY HANDLERS Start */\r
         on_all_ev_clear_records: function (data) {\r
@@ -188,25 +116,14 @@ var schdlr_PartsInOneHour = 6;
             //data is empty on load\r
         },\r
         on_all_resources_new_record: function (data) {\r
-            $("#ShedulerNodes tbody").find("tr:last").after('<tr><td class="info fixed">' + data.urn + '</td></tr>');\r
-            this.drawLeases();\r
-            //console.log(data);\r
-            var tmpGroup = lookup(SchedulerResources, 'groupName', data.type);\r
-            if (tmpGroup == null) {\r
-                tmpGroup = { groupName: data.type, resources: [] };\r
-                SchedulerResources.push(tmpGroup);\r
-                //if (data.type != "node")  alert('not all node');\r
-            }\r
-            tmpGroup.resources.push(data);\r
-            //alert('new_record');\r
+            if (SchedulerData.length < schedulerMaxRows)\r
+                SchedulerData.push({ name: data.hrn, leases: schedulerGetLeases(schedulerSlotsPerHour), urn: data.urn, type: data.type });\r
+            //alert(data.toSource());\r
         },\r
         on_all_resources_query_done: function (data) {\r
-            //this.drawResources();\r
-            //data is empty on load\r
             /* GUI setup and event binding */\r
             this._initUI();\r
-            this._SetPeriodInPage(init_start_visible_index, init_end_visible_index);\r
-            this.loadWithDate();\r
+            //this.loadWithDate();\r
         },\r
         //another plugin has modified something, that requires you to update your display. \r
         on_all_resources_field_state_changed: function (data) {\r
@@ -216,7 +133,7 @@ var schdlr_PartsInOneHour = 6;
         /* lease QUERY HANDLERS Start */\r
         on_lease_clear_records: function (data) { console.log('clear_records'); },\r
         on_lease_query_in_progress: function (data) { console.log('lease_query_in_progress'); },\r
-        on_lease_new_record: function (data) { console.log('lease_new_record'); },\r
+        on_lease_new_record: function (data) { alert(data.toSource()); console.log('lease_new_record'); },\r
         on_lease_query_done: function (data) { console.log('lease_query_done'); },\r
         //another plugin has modified something, that requires you to update your display. \r
         on_lease_field_state_changed: function (data) { console.log('lease_field_state_changed'); },\r
@@ -233,36 +150,19 @@ var schdlr_PartsInOneHour = 6;
 \r
         /* RECORD HANDLERS */\r
         on_all_new_record: function (record) {\r
-            //\r
-            alert('on_all_new_record');\r
+            //alert('on_all_new_record');\r
         },\r
 \r
-        debug : function (log_txt) {\r
+        debug : function (logTxt) {\r
             if (typeof window.console != 'undefined') {\r
-                console.debug(log_txt);\r
+                console.debug(logTxt);\r
             }\r
         },\r
 \r
         /* INTERNAL FUNCTIONS */\r
         _initUI: function () {\r
-            if (Debug) console.time("_initUI");\r
-            //fix margins in tables\r
-            mtNodesTbl = $("#" + rsvrTblNm + " tr:first").outerHeight() + 6;\r
-            mtSchrollCon = $("#nodes").outerWidth();\r
-            $("#nodes").css("margin-top", mtNodesTbl);\r
-            $("#reservation-table-scroll-container").css("margin-left", mtSchrollCon);\r
-            SetPerFun = this._SetPeriodInPage;\r
-            //slider\r
-            $("#time-range").slider({\r
-                range: true,\r
-                min: 0,\r
-                max: 24,\r
-                step: 0.5,\r
-                values: [init_start_visible_index, init_end_visible_index],\r
-                slide: function (event, ui) {\r
-                    SetPerFun(ui.values[0], ui.values[1]);\r
-                }\r
-            });\r
+            if (schedulerDebug) console.time("_initUI");\r
+            //init DatePicker Start\r
             $("#DateToRes").datepicker({\r
                 dateFormat: "yy-mm-dd",\r
                 minDate: 0,\r
@@ -272,48 +172,59 @@ var schdlr_PartsInOneHour = 6;
             }).click(function () {\r
                 $("#ui-datepicker-div").css("z-index", 5);\r
             });\r
-            //other stuff\r
-            fixOddEvenClasses();\r
-            $("#" + rsvrTblNm + " td:not([class])").addClass("free");\r
-            if (Debug) console.timeEnd("_initUI");\r
-        },\r
-        _SetPeriodInPage: function (start, end) {\r
-            if (Debug) console.time("_SetPeriodInPage");\r
-            ClearTableSelection();\r
-            $("#lbltime").html(GetTimeFromInt(start) + " - " + GetTimeFromInt(end));\r
+            //End init DatePicker\r
             \r
-            var start_visible_index = (start * schdlr_PartsInOneHour) + 1;\r
-            var end_visible_index = (end * schdlr_PartsInOneHour);\r
+            //init Table\r
+            this._FixTable();\r
+            //End init Table\r
 \r
-            //hide - show\r
-            for (i = 0; i < start_visible_index; i++) {\r
-                $("#" + rsvrTblNm + " td:nth-child(" + i + "), #" + rsvrTblNm + " th:nth-child(" + i + ")").hide();\r
-            }\r
-            for (i = end_visible_index + 1; i <= schdlr_totalColums; i++) {\r
-                $("#" + rsvrTblNm + " td:nth-child(" + i + "), #" + rsvrTblNm + " th:nth-child(" + i + ")").hide();\r
-            }\r
-            /*$("#" + rsvrTblNm + " td:not([class*='info']), #" + rsvrTblNm + " th:not([class*='fixed'])").hide();*/\r
-            for (i = start_visible_index; i <= end_visible_index; i++) {\r
-                $("#" + rsvrTblNm + " td:nth-child(" + i + "), #" + rsvrTblNm + " th:nth-child(" + i + ")").show();\r
-            }\r
+            //init Slider\r
+            $('#tblSlider').slider({\r
+                min: 0,\r
+                max: SchedulerTotalCells - SchedulerTotalVisibleCells - 1,\r
+                value: 0,\r
+                slide: function (event, ui) {\r
+                    //$("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);\r
+                    //console.log(ui.value);\r
+                    if (_schedulerCurrentCellPosition > ui.value)\r
+                        angular.element(document.getElementById('SchedulerCtrl')).scope().moveBackSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
+                    else if (_schedulerCurrentCellPosition < ui.value)\r
+                        angular.element(document.getElementById('SchedulerCtrl')).scope().moveFrontSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
+                    _schedulerCurrentCellPosition = ui.value;\r
+                }\r
+            });\r
+            //End init Slider\r
 \r
-            if ($("#" + rsvrTblNm + " th:visible:first").width() > 105) {\r
-                $("#" + rsvrTblNm + " th span").css("display", "inline")\r
-            } else {\r
-                $("#" + rsvrTblNm + " th span").css("display", "block");\r
+            //other stuff\r
+            //fixOddEvenClasses();\r
+            //$("#" + schedulerTblId + " td:not([class])").addClass("free");\r
+            if (schedulerDebug) console.timeEnd("_initUI");\r
+        },\r
+        _FixTable: function () {\r
+            var colWidth = 50;\r
+            SchedulerTotalCells = SchedulerSlots.length;\r
+            $('#' + schedulerTblId + ' thead tr th:eq(0)').css("width", schedulerTblFirstColWidth);\r
+            //this get width might need fix depending on the template \r
+            var tblwidth = $('#scheduler-tab').parent().outerWidth();\r
+            SchedulerTotalVisibleCells = parseInt((tblwidth - schedulerTblFirstColWidth) / colWidth);\r
+\r
+            if (SchedulerData.length == 0) {\r
+                //puth some test data\r
+                SchedulerData.push({ name: 'xyz+aaa', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+aaa', type: 'node' });\r
+                SchedulerData.push({ name: 'xyz+bbb', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+bbb', type: 'node' });\r
+                SchedulerData.push({ name: 'xyz+ccc', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'xyz+ccc', type: 'node' });\r
+                SchedulerData.push({ name: 'nitos1', leases: schedulerGetLeases(60 / schedulerSlotsPerHour), urn: 'nitos1', type: 'node' });\r
             }\r
-            mtNodesTbl = $("#" + rsvrTblNm + " tr:first").outerHeight() + 6;\r
-            $("#nodes").css("margin-top", mtNodesTbl);\r
-            //$("#scroll_container").width($("#Search").width() - $("#nodes").width());\r
-            //$("#nodes th").height($("#tblReservation th:visible:first").height() - 2);\r
-            if (Debug) console.timeEnd("_SetPeriodInPage");\r
+            angular.element(document.getElementById('SchedulerCtrl')).scope().initSlots(0, SchedulerTotalVisibleCells);\r
+        },\r
+        _SetPeriodInPage: function (start, end) {\r
         }\r
     });\r
 \r
     //Sched2 = new Scheduler2();\r
 \r
     /* Plugin registration */\r
-    $.plugin('Scheduler2', Scheduler2);\r
+    $.plugin('Scheduler2', scheduler2);\r
 \r
     // TODO Here use cases for instanciating plugins in different ways like in the pastie.\r
 \r