Chrysostomos for scheduler
[unfold.git] / plugins / scheduler2 / static / js / scheduler2.js
index dd0c8f8..8c4c49f 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 = 12;\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
+var _leasesDone = false;\r
+var _resourcesDone = false;\r
+//Enable Debug\r
+var schedulerDebug = true;\r
+//tmp to delete\r
+var tmpSchedulerLeases = [];\r
 \r
 (function ($) {\r
-    var Scheduler2 = Plugin.extend({\r
+    var scheduler2 = Plugin.extend({\r
 \r
         /** XXX to check\r
          * @brief Plugin constructor\r
@@ -48,112 +61,43 @@ var schdlr_PartsInOneHour = 6;
          *     applied, which allows to maintain chainability of calls\r
          */\r
         init: function (options, element) {\r
+            this.classname="scheduler2";\r
             // Call the parent constructor, see FAQ when forgotten\r
             this._super(options, element);\r
+            // We need to remember the active filter for datatables filtering
+            this.filters = Array();\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_uuid);
+            this.listen_query(options.query_all_uuid, 'all');\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
         },\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
-            \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
-                //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
-                \r
-                $.each(group.resources.slice(0,30), 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.hostname);\r
-                        //$(srt_body).html("<tr>" + totalCell + "</tr>");\r
-                    } else {\r
-                        $(groupTR).find("tr:last").after('<tr><td class="info fixed">' + resource.hostname + '</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
-            //$("#scheduler-reservation-table tbody").html(srt_body.join(""));\r
-            $("#scheduler-reservation-table tbody").html(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
@@ -181,22 +125,27 @@ var schdlr_PartsInOneHour = 6;
             //data is empty on load\r
         },\r
         on_all_resources_new_record: function (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
+            //alert(data.toSource());\r
+            if (data.exclusive == true){\r
+                SchedulerData.push({\r
+                    id: data.urn,\r
+                    index: SchedulerData.length,\r
+                    name: data.hrn,\r
+                    granularity: data.granularity,\r
+                    leases: schedulerGetLeases(60 / schedulerSlotsPerHour),\r
+                    type: data.type,\r
+                    org_resource: data\r
+                });\r
+                if (schedulerDebug && SchedulerData[SchedulerData.length - 1].org_resource.network_hrn == 'omf') {\r
+                    SchedulerData[SchedulerData.length - 1].granularity = 30;\r
+                }\r
             }\r
-            tmpGroup.resources.push(data);\r
-            //alert('new_record');\r
+            //alert(data.toSource());\r
+\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
+            _resourcesDone = true;\r
+            this._initScheduler();\r
         },\r
         //another plugin has modified something, that requires you to update your display. \r
         on_all_resources_field_state_changed: function (data) {\r
@@ -204,55 +153,113 @@ var schdlr_PartsInOneHour = 6;
         },\r
         /* all_resources QUERY HANDLERS End */\r
         /* lease QUERY HANDLERS Start */\r
-        on_lease_clear_records: function (data) { alert('clear_records'); },\r
-        on_lease_query_in_progress: function (data) { alert('query_in_progress'); },\r
-        on_lease_new_record: function (data) { alert('new_record'); },\r
-        on_lease_query_done: function (data) { alert('query_done'); },\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) {\r
+            tmpSchedulerLeases.push({\r
+                id: schedulerGetSlotId(data.start_time, data.duration, data.granularity),\r
+                slice: data.slice,\r
+                status: 'reserved',\r
+                resource: data.resource,\r
+                network: data.network,\r
+                start_time: new Date(data.start_time * 1000),\r
+                start_time_unixtimestamp: data.start_time,\r
+                lease_type: data.lease_type,\r
+                granularity: data.granularity,\r
+                duration: data.duration\r
+            });\r
+            //console.log(data.toSource()); console.log('lease_new_record');\r
+        },\r
+        on_lease_query_done: function (data) {\r
+            _leasesDone = true;\r
+            this._initScheduler();\r
+            // console.log('lease_query_done');\r
+        },\r
         //another plugin has modified something, that requires you to update your display. \r
-        on_lease_field_state_changed: function (data) { alert('query_done'); },\r
+        on_lease_field_state_changed: function (data) { console.log('lease_field_state_changed'); },\r
         /* lease QUERY HANDLERS End */\r
 \r
 \r
         // no prefix\r
-\r
         on_filter_added: function (filter) {\r
-\r
+            this.filters.push(filter);\r
+            this._SetFiletredResources(this.filters);\r
+            //angular and UI\r
+            var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
+            if (SchedulerDataViewData.length == 0) {\r
+                $("#plugin-scheduler").hide();\r
+                $("#plugin-scheduler-empty").show();\r
+                tmpScope.clearStuff();\r
+            } else {\r
+                $("#plugin-scheduler-empty").hide();\r
+                $("#plugin-scheduler").show();\r
+                tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length);\r
+            }\r
+        },
+
+        on_filter_removed: function (filter) {
+            // Remove corresponding filters
+            this.filters = $.grep(this.filters, function (x) {\r
+                return x == filter;\r
+            });\r
+            this._SetFiletredResources(this.filters);\r
+            //angular and UI\r
+            var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
+            if (SchedulerDataViewData.length == 0) {\r
+                $("#plugin-scheduler").hide();\r
+                $("#plugin-scheduler-empty").show();\r
+                tmpScope.clearStuff();\r
+            } else {\r
+                $("#plugin-scheduler-empty").hide();\r
+                $("#plugin-scheduler").show();\r
+                tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length);\r
+            }\r
+        },
+
+        on_filter_clear: function () {\r
+            this.filters = [];\r
+            this._SetFiletredResources(this.filters);\r
+            //angular and UI\r
+            var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
+            if (SchedulerDataViewData.length == 0) {\r
+                $("#plugin-scheduler").hide();\r
+                $("#plugin-scheduler-empty").show();\r
+                tmpScope.clearStuff();\r
+            } else {\r
+                $("#plugin-scheduler-empty").hide();\r
+                $("#plugin-scheduler").show();\r
+                tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length);\r
+            }\r
         },\r
 \r
         // ... be sure to list all events here\r
 \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
+        _initScheduler: function () {\r
+            if (_resourcesDone && _leasesDone)\r
+            {\r
+                SchedulerDataViewData = SchedulerData;\r
+                /* GUI setup and event binding */\r
+                this._FixLeases();\r
+                this._initUI();\r
+            }\r
+        },\r
+\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
+            //alert(1);\r
+            if (schedulerDebug) console.time("_initUI");\r
+            //init DatePicker Start\r
             $("#DateToRes").datepicker({\r
                 dateFormat: "yy-mm-dd",\r
                 minDate: 0,\r
@@ -262,48 +269,126 @@ var schdlr_PartsInOneHour = 6;
             }).click(function () {\r
                 $("#ui-datepicker-div").css("z-index", 5);\r
             });\r
+            //End init DatePicker\r
+            \r
+            //init Table\r
+            this._FixTable();\r
+            //End init Table\r
+\r
+            //init Slider\r
+            $('#tblSlider').slider({\r
+                min: 0,\r
+                max: SchedulerTotalCells - SchedulerTotalVisibleCells,\r
+                value: 0,\r
+                slide: function (event, ui) {\r
+                    //$("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);\r
+                    //console.log(ui.value);\r
+                    var angScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
+                    if (_schedulerCurrentCellPosition > ui.value) {\r
+                        angScope.moveBackSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
+                    }\r
+                    else if (_schedulerCurrentCellPosition < ui.value) {\r
+                        angScope.moveFrontSlot(ui.value, ui.value + SchedulerTotalVisibleCells);\r
+                    }\r
+                    _schedulerCurrentCellPosition = ui.value;\r
+                }\r
+            });\r
+            //End init Slider\r
+\r
             //other stuff\r
-            fixOddEvenClasses();\r
-            $("#" + rsvrTblNm + " td:not([class])").addClass("free");\r
-            if (Debug) console.timeEnd("_initUI");\r
+            $("#plugin-scheduler-loader").hide();\r
+            $("#plugin-scheduler").show();\r
+            //fixOddEvenClasses();\r
+            //$("#" + schedulerTblId + " td:not([class])").addClass("free");\r
+            if (schedulerDebug) 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
-            \r
-            var start_visible_index = (start * schdlr_PartsInOneHour) + 1;\r
-            var end_visible_index = (end * schdlr_PartsInOneHour);\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
+        _FixLeases  : function () {\r
+            for (var i = 0; i < tmpSchedulerLeases.length; i++) {\r
+                var tmpLea = tmpSchedulerLeases[i];\r
+                var tmpRes = schedulerFindResourceById(SchedulerData, tmpLea.resource);\r
+                if (tmpRes != null) {\r
+                    //alert(tmpLea.id + '-' + tmpLea.start_time);\r
+                    tmpRes.leases[tmpLea.id] = tmpLea;\r
+                }\r
             }\r
+        },\r
+\r
+        _FixTable: function () {\r
+            var colWidth = 50;\r
+            SchedulerTotalCells = SchedulerSlots.length;\r
+            $('#' + schedulerTblId + ' thead tr th:eq(0)').css("width", schedulerTblFirstColWidth); //.css("display", "block");\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
+            var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope();\r
+            tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length);\r
+\r
+        },\r
 \r
-            if ($("#" + rsvrTblNm + " th:visible:first").width() > 105) {\r
-                $("#" + rsvrTblNm + " th span").css("display", "inline")\r
+        _SetFiletredResources : function (filters) {\r
+            if (filters.length > 0) {\r
+                SchedulerDataViewData = new Array();\r
+                var tmpAddIt = true;\r
+                for (var i = 0; i < SchedulerData.length; i++) {\r
+                    loopfilters:\r
+                    for (var f = 0; f < filters.length; f++) {\r
+                        tmpAddIt = this._FilterResource(SchedulerData[i], filters[f]);\r
+                        if (tmpAddIt == false) break loopfilters;\r
+                    }\r
+                    if (tmpAddIt) {\r
+                        SchedulerDataViewData.push(SchedulerData[i]);\r
+                    }\r
+                }\r
             } else {\r
-                $("#" + rsvrTblNm + " th span").css("display", "block");\r
+                SchedulerDataViewData = SchedulerData;\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
+        },\r
+\r
+        _FilterResource: function (resource, filter) {\r
+            var key = filter[0];\r
+            var op = filter[1];\r
+            var value = filter[2];\r
+            var colValue = resource.org_resource[key];\r
+            var ret = true;\r
+            if (schedulerDebug &&  colValue == 'omf') colValue = 'nitos';\r
+\r
+            if (op == '=' || op == '==') {\r
+                if (colValue != value || colValue == null || colValue == "" || colValue == "n/a")
+                    ret = false;\r
+            } else if (op == 'included') {\r
+                $.each(value, function (i, x) {\r
+                    if (x == colValue) {\r
+                        ret = true;\r
+                        return false;\r
+                    } else {\r
+                        ret = false;\r
+                    }\r
+                });\r
+            } else if (op == '!=') {\r
+                if (colValue == value || colValue == null || colValue == "" || colValue == "n/a")
+                    ret = false;\r
+            }\r
+\r
+            return ret;\r
+        },\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