X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=plugins%2Fscheduler2%2Fstatic%2Fjs%2Fscheduler2.js;h=27300e33d09b5f85cee8306b6e0a94f233c2bbd6;hb=436b2227713f8cd5e0b7da6e9d1216eb17c2a941;hp=8f786de5af96e28d947b467bced23e5354bbf3e2;hpb=0e79f5650ff7bd234d756214d1e3fdbf1302a0eb;p=unfold.git diff --git a/plugins/scheduler2/static/js/scheduler2.js b/plugins/scheduler2/static/js/scheduler2.js index 8f786de5..27300e33 100755 --- a/plugins/scheduler2/static/js/scheduler2.js +++ b/plugins/scheduler2/static/js/scheduler2.js @@ -53,277 +53,347 @@ var schedulerDebug = true; //tmp to delete var tmpSchedulerLeases = []; -(function ($) { - scheduler2 = Plugin.extend({ +(function($) { + scheduler2 = Plugin.extend({ - /** XXX to check + /** XXX to check * @brief Plugin constructor * @param options : an associative array of setting values * @param element : * @return : a jQuery collection of objects on which the plugin is * applied, which allows to maintain chainability of calls */ - init: function (options, element) { - this.classname="scheduler2"; - // Call the parent constructor, see FAQ when forgotten - this._super(options, element); - scheduler2Instance = this; - // We need to remember the active filter for datatables filtering - this.filters = Array(); - - - SchedulerSlots = schedulerGetSlots(60/schedulerSlotsPerHour); - //selection from table - $(window).keydown(function (evt) { - if (evt.which == 17) { // ctrl - schedulerCtrlPressed = true; + init: function(options, element) { + this.classname = "scheduler2"; + // Call the parent constructor, see FAQ when forgotten + this._super(options, element); + scheduler2Instance = this; + // We need to remember the active filter for datatables filtering + this.filters = Array(); + + + SchedulerSlots = schedulerGetSlots(60 / schedulerSlotsPerHour); + //selection from table + $(window).keydown(function(evt) { + if (evt.which == 17) { // ctrl + schedulerCtrlPressed = true; + } + }).keyup(function(evt) { + if (evt.which == 17) { // ctrl + schedulerCtrlPressed = false; + } + }); + $("#" + schedulerTblId).on('mousedown', 'td', rangeMouseDown).on('mouseup', 'td', rangeMouseUp).on('mousemove', 'td', rangeMouseMove); + + // Explain this will allow query events to be handled + // What happens when we don't define some events ? + // Some can be less efficient + + if (schedulerDebug) console.time("Listening_to_queries"); + /* Listening to queries */ + + this.listen_query(options.query_uuid); + //this.listen_query(options.query_all_uuid, 'all'); + this.listen_query(options.query_all_resources_uuid, 'all_resources'); + this.listen_query(options.query_lease_uuid, 'lease'); + this.listen_query(options.query_all_leases_uuid, 'all_leases'); + if (schedulerDebug) console.timeEnd("Listening_to_queries"); + + }, + + /* Handlers */ + + /* all_ev QUERY HANDLERS Start */ + on_all_ev_clear_records: function(data) { + //alert('all_ev clear_records'); + }, + on_all_ev_query_in_progress: function(data) { + // alert('all_ev query_in_progress'); + }, + on_all_ev_new_record: function(data) { + //alert('all_ev new_record'); + }, + on_all_ev_query_done: function(data) { + //alert('all_ev query_done'); + }, + //another plugin has modified something, that requires you to update your display. + on_all_ev_field_state_changed: function(data) { + //alert('all_ev query_done'); + }, + /* all_ev QUERY HANDLERS End */ + /* all_resources QUERY HANDLERS Start */ + on_all_resources_clear_records: function(data) { + //data is empty on load + }, + on_all_resources_query_in_progress: function(data) { + //data is empty on load + }, + on_all_resources_new_record: function(data) { + //alert(data.toSource()); + if (data.exclusive == true) { + var tmpGran = schedulerDebug && data.granularity == null ? 1800 : data.granularity; + SchedulerData.push({ + id: data.urn, + index: SchedulerData.length, + name: data.hrn, + granularity: tmpGran, + leases: schedulerGetLeases(60 / schedulerSlotsPerHour, tmpGran), + type: data.type, + org_resource: data + }); + /*if (schedulerDebug && SchedulerData[SchedulerData.length - 1].org_resource.network_hrn == 'omf') { + SchedulerData[SchedulerData.length - 1].granularity = 1800; + }*/ } - }).keyup(function (evt) { - if (evt.which == 17) { // ctrl - schedulerCtrlPressed = false; + //alert(data.toSource()); + + }, + on_all_resources_query_done: function(data) { + _resourcesDone = true; + this._initScheduler(); + }, + //another plugin has modified something, that requires you to update your display. + on_all_resources_field_state_changed: function(data) { + //alert('all_resources query_done'); + }, + /* all_resources QUERY HANDLERS End */ + /* lease QUERY HANDLERS Start */ + on_lease_clear_records: function(data) { console.log('clear_records'); }, + on_lease_query_in_progress: function(data) { console.log('lease_query_in_progress'); }, + on_all_leases_new_record: function(data) { + if (data.resource.indexOf("nitos") > -1) { + tmpSchedulerLeases.push({ + id: schedulerGetSlotId(data.start_time, data.duration, data.granularity), + end_id: schedulerGetSlotId(data.end_time, data.duration, data.granularity), + slice: data.slice, + status: 'reserved', + resource: data.resource, + network: data.network, + start_time: new Date(data.start_time * 1000), + start_time_unixtimestamp: data.start_time, + end_time: new Date(data.end_time * 1000), + end_time_unixtimestamp: data.end_time, + lease_type: data.lease_type, + granularity: data.granularity, + duration: data.duration + }); } - }); - $("#" + schedulerTblId).on('mousedown', 'td', rangeMouseDown).on('mouseup', 'td', rangeMouseUp).on('mousemove', 'td', rangeMouseMove); - - // Explain this will allow query events to be handled - // What happens when we don't define some events ? - // Some can be less efficient - - if (schedulerDebug) console.time("Listening_to_queries"); - /* Listening to queries */ - - this.listen_query(options.query_uuid); - this.listen_query(options.query_all_uuid, 'all'); - this.listen_query(options.query_all_resources_uuid, 'all_resources'); - this.listen_query(options.query_lease_uuid, 'lease'); - //this.listen_query(options.query_lease_uuid, 'lease'); - if (schedulerDebug) console.timeEnd("Listening_to_queries"); - - }, - - /* Handlers */ - - /* all_ev QUERY HANDLERS Start */ - on_all_ev_clear_records: function (data) { - //alert('all_ev clear_records'); - }, - on_all_ev_query_in_progress: function (data) { - // alert('all_ev query_in_progress'); - }, - on_all_ev_new_record: function (data) { - //alert('all_ev new_record'); - }, - on_all_ev_query_done: function (data) { - //alert('all_ev query_done'); - }, - //another plugin has modified something, that requires you to update your display. - on_all_ev_field_state_changed: function (data) { - //alert('all_ev query_done'); - }, - /* all_ev QUERY HANDLERS End */ - /* all_resources QUERY HANDLERS Start */ - on_all_resources_clear_records: function (data) { - //data is empty on load - }, - on_all_resources_query_in_progress: function (data) { - //data is empty on load - }, - on_all_resources_new_record: function (data) { - //alert(data.toSource()); - if (data.exclusive == true){ - SchedulerData.push({ - id: data.urn, - index: SchedulerData.length, - name: data.hrn, - granularity: data.granularity, - leases: schedulerGetLeases(60 / schedulerSlotsPerHour, data.granularity), - type: data.type, - org_resource: data - }); - if (schedulerDebug && SchedulerData[SchedulerData.length - 1].org_resource.network_hrn == 'omf') { - SchedulerData[SchedulerData.length - 1].granularity = 1800; + //console.log(data.toSource()); console.log('lease_new_record'); + }, + on_all_leases_query_done: function(data) { + _leasesDone = true; + this._initScheduler(); + // console.log('lease_query_done'); + }, + //another plugin has modified something, that requires you to update your display. + on_lease_field_state_changed: function(data) { console.log('lease_field_state_changed'); }, + /* lease QUERY HANDLERS End */ + + + // no prefix + on_filter_added: function(filter) { + this.filters.push(filter); + this._SetFiletredResources(this.filters); + //angular and UI + var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope(); + if (SchedulerDataViewData.length == 0) { + $("#plugin-scheduler").hide(); + $("#plugin-scheduler-empty").show(); + tmpScope.clearStuff(); + } else { + $("#plugin-scheduler-empty").hide(); + $("#plugin-scheduler").show(); + tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length); } - } - //alert(data.toSource()); + }, - }, - on_all_resources_query_done: function (data) { - _resourcesDone = true; - this._initScheduler(); - }, - //another plugin has modified something, that requires you to update your display. - on_all_resources_field_state_changed: function (data) { - //alert('all_resources query_done'); - }, - /* all_resources QUERY HANDLERS End */ - /* lease QUERY HANDLERS Start */ - on_lease_clear_records: function (data) { console.log('clear_records'); }, - on_lease_query_in_progress: function (data) { console.log('lease_query_in_progress'); }, - on_lease_new_record: function (data) { - if (data.resource.indexOf("nitos")>-1){ - tmpSchedulerLeases.push({ - id: schedulerGetSlotId(data.start_time, data.duration, data.granularity), - end_id: schedulerGetSlotId(data.end_time, data.duration, data.granularity), - slice: data.slice, - status: 'reserved', - resource: data.resource, - network: data.network, - start_time: new Date(data.start_time * 1000), - start_time_unixtimestamp: data.start_time, - end_time: new Date(data.end_time * 1000), - end_time_unixtimestamp: data.end_time, - lease_type: data.lease_type, - granularity: data.granularity, - duration: data.duration + on_filter_removed: function(filter) { + // Remove corresponding filters + this.filters = $.grep(this.filters, function(x) { + return x == filter; }); - } - //console.log(data.toSource()); console.log('lease_new_record'); - }, - on_lease_query_done: function (data) { - _leasesDone = true; - this._initScheduler(); - // console.log('lease_query_done'); - }, - //another plugin has modified something, that requires you to update your display. - on_lease_field_state_changed: function (data) { console.log('lease_field_state_changed'); }, - /* lease QUERY HANDLERS End */ - - - // no prefix - on_filter_added: function (filter) { - this.filters.push(filter); - this._SetFiletredResources(this.filters); - //angular and UI - var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope(); - if (SchedulerDataViewData.length == 0) { - $("#plugin-scheduler").hide(); - $("#plugin-scheduler-empty").show(); - tmpScope.clearStuff(); - } else { - $("#plugin-scheduler-empty").hide(); - $("#plugin-scheduler").show(); - tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length); - } - }, - - on_filter_removed: function (filter) { - // Remove corresponding filters - this.filters = $.grep(this.filters, function (x) { - return x == filter; - }); - this._SetFiletredResources(this.filters); - //angular and UI - var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope(); - if (SchedulerDataViewData.length == 0) { - $("#plugin-scheduler").hide(); - $("#plugin-scheduler-empty").show(); - tmpScope.clearStuff(); - } else { - $("#plugin-scheduler-empty").hide(); - $("#plugin-scheduler").show(); - tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length); - } - }, - - on_filter_clear: function () { - this.filters = []; - this._SetFiletredResources(this.filters); - //angular and UI - var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope(); - if (SchedulerDataViewData.length == 0) { - $("#plugin-scheduler").hide(); - $("#plugin-scheduler-empty").show(); - tmpScope.clearStuff(); - } else { - $("#plugin-scheduler-empty").hide(); - $("#plugin-scheduler").show(); - tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length); - } - }, - - // ... be sure to list all events here + this._SetFiletredResources(this.filters); + //angular and UI + var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope(); + if (SchedulerDataViewData.length == 0) { + $("#plugin-scheduler").hide(); + $("#plugin-scheduler-empty").show(); + tmpScope.clearStuff(); + } else { + $("#plugin-scheduler-empty").hide(); + $("#plugin-scheduler").show(); + tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length); + } + }, + + on_filter_clear: function() { + this.filters = []; + this._SetFiletredResources(this.filters); + //angular and UI + var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope(); + if (SchedulerDataViewData.length == 0) { + $("#plugin-scheduler").hide(); + $("#plugin-scheduler-empty").show(); + tmpScope.clearStuff(); + } else { + $("#plugin-scheduler-empty").hide(); + $("#plugin-scheduler").show(); + tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length); + } + }, - /* RECORD HANDLERS */ - on_all_new_record: function (record) { - //alert('on_all_new_record'); - }, + on_all_leases_filter_added: function(filter) { + console.log("Filter on Leases added !"); + }, - debug : function (logTxt) { - if (typeof window.console != 'undefined') { - console.debug(logTxt); - } - }, + // ... be sure to list all events here - /* INTERNAL FUNCTIONS */ - _initScheduler: function () { - if (_resourcesDone && _leasesDone) - { - SchedulerDataViewData = SchedulerData; - /* GUI setup and event binding */ - this._FixLeases(); - this._initUI(); - } - }, + /* RECORD HANDLERS */ + on_all_new_record: function(record) { + //alert('on_all_new_record'); + }, - _initUI: function () { - //alert(1); - if (schedulerDebug) console.time("_initUI"); - //init DatePicker Start - $("#DateToRes").datepicker({ - dateFormat: "yy-mm-dd", - minDate: 0, - numberOfMonths: 3 - }).change(function () { - //Scheduler2.loadWithDate(); - SchedulerDateSelected = $("#DateToRes").datepicker("getDate"); - if (SchedulerDateSelected != null && SchedulerDateSelected != '') { - for (var i=0; i < SchedulerData.length; i++) { - SchedulerData[i].leases = schedulerGetLeases(60 / schedulerSlotsPerHour, SchedulerData[i].granularity); - } - scheduler2Instance._FixLeases(); - $('#tblSlider').slider('value', 0); - var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope(); - tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length); - } else { - alert("Please select a date, so the scheduler can reserve leases."); + debug: function(logTxt) { + if (typeof window.console != 'undefined') { + console.debug(logTxt); + } + }, + + /* INTERNAL FUNCTIONS */ + _initScheduler: function() { + if (_resourcesDone && _leasesDone) { + SchedulerDataViewData = SchedulerData; + /* GUI setup and event binding */ + this._FixLeases(); + this._initUI(); } - }).datepicker('setDate', SchedulerDateSelected); - /*.click(function () { + }, + + _initUI: function() { + //alert(1); + if (schedulerDebug) console.time("_initUI"); + //init DatePicker Start + $("#DateToRes").datepicker({ + dateFormat: "yy-mm-dd", + minDate: 0, + numberOfMonths: 3 + }).change(function() { + //Scheduler2.loadWithDate(); + SchedulerDateSelected = $("#DateToRes").datepicker("getDate"); + if (SchedulerDateSelected != null && SchedulerDateSelected != '') { + for (var i = 0; i < SchedulerData.length; i++) { + SchedulerData[i].leases = schedulerGetLeases(60 / schedulerSlotsPerHour, SchedulerData[i].granularity); + } + scheduler2Instance._FixLeases(); + $('#tblSlider').slider('value', 0); + var tmpScope = angular.element(document.getElementById('SchedulerCtrl')).scope(); + tmpScope.initSchedulerResources(schedulerMaxRows < SchedulerDataViewData.length ? schedulerMaxRows : SchedulerDataViewData.length); + + console.log(SchedulerDateSelected); + console.log(SchedulerDateSelected.getTime()/1000); + var tomorrow = new Date(SchedulerDateSelected); + tomorrow.setDate(SchedulerDateSelected.getDate()+1); + console.log(tomorrow); + console.log(tomorrow.getTime()/1000); + + // Remove previous date interval + manifold.raise_event(scheduler2Instance.options.query_all_leases_uuid, FILTER_REMOVED, ['start_time', '>']); + manifold.raise_event(scheduler2Instance.options.query_all_leases_uuid, FILTER_REMOVED, ['start_time', '<']); + + // Add new date interval + manifold.raise_event(scheduler2Instance.options.query_all_leases_uuid, FILTER_ADDED, ['start_time', '>', SchedulerDateSelected.getTime()/1000]); + manifold.raise_event(scheduler2Instance.options.query_all_leases_uuid, FILTER_ADDED, ['start_time', '<', tomorrow.getTime()/1000]); + } else { + alert("Please select a date, so the scheduler can reserve leases."); + } + }).datepicker('setDate', SchedulerDateSelected); + /*.click(function () { $("#ui-datepicker-div").css("z-index", 5); })*/ - //End init DatePicker - - //init Table - this._FixTable(); - //End init Table - - //init Slider - $('#tblSlider').slider({ - min: 0, - max: SchedulerTotalCells - SchedulerTotalVisibleCells, - value: 0, - slide: function (event, ui) { - //$("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]); - //console.log(ui.value); - var angScope = angular.element(document.getElementById('SchedulerCtrl')).scope(); - if (_schedulerCurrentCellPosition > ui.value) { - angScope.moveBackSlot(ui.value, ui.value + SchedulerTotalVisibleCells); + //End init DatePicker + + //init Table + this._FixTable(); + //End init Table + + //init Slider + $('#tblSlider').slider({ + min: 0, + max: SchedulerTotalCells - SchedulerTotalVisibleCells, + value: 0, + slide: function(event, ui) { + //$("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]); + //console.log(ui.value); + var angScope = angular.element(document.getElementById('SchedulerCtrl')).scope(); + if (_schedulerCurrentCellPosition > ui.value) { + angScope.moveBackSlot(ui.value, ui.value + SchedulerTotalVisibleCells); + } else if (_schedulerCurrentCellPosition < ui.value) { + angScope.moveFrontSlot(ui.value, ui.value + SchedulerTotalVisibleCells); + } + _schedulerCurrentCellPosition = ui.value; + } + }); + //End init Slider + + + //btn Submit leases + $('#btnSchedulerSubmit').click(function () { + console.log("click btnSchedulerSubmit"); + var leasesForCommit = new Array(); + var tmpDateTime = SchedulerDateSelected; + console.log(SchedulerData); + for (var i = 0; i < SchedulerData.length; i++) + { + var tpmR = SchedulerData[i]; + //for capturing start and end of the lease + var newLeaseStarted = false; + for (var j = 0; j < tpmR.leases.length; j++) { + var tpmL = tpmR.leases[j]; + if (newLeaseStarted == false && tpmL.status == 'selected') { + //get date of the slot + tmpDateTime = schedulerGetDateTimeFromSlotId(tpmL.id, tmpDateTime); + var unixStartTime = tmpDateTime.getTime() / 1000; + //add lease object + leasesForCommit.push({ + resource: tpmR.id, + //granularity: tpmR.granularity, + //lease_type: null, + //slice: null, + start_time: unixStartTime, + end_time: null, + //duration: null + }); + console.log(tpmR.id); + newLeaseStarted = true; + } else if (newLeaseStarted == true && tpmL.status != 'selected') { + //get date of the slot + tmpDateTime = schedulerGetDateTimeFromSlotId(tpmL.id, tmpDateTime); + var unixEndTime = tmpDateTime.getTime() / 1000; + //upate end_time + var tmpCL = leasesForCommit[leasesForCommit.length - 1]; + tmpCL.end_time = unixEndTime; + //tmpCL.duration = schedulerFindDuration(tmpCL.start_time, tmpCL.end_time, tmpCL.granularity); + newLeaseStarted = false; + } + } } - else if (_schedulerCurrentCellPosition < ui.value) { - angScope.moveFrontSlot(ui.value, ui.value + SchedulerTotalVisibleCells); + console.log(leasesForCommit); + for (var i = 0; i < leasesForCommit.length; i++) { + manifold.raise_event(scheduler2Instance.options.query_lease_uuid, SET_ADD, leasesForCommit[i]); } - _schedulerCurrentCellPosition = ui.value; - } - }); - //End init Slider - - //other stuff - $("#plugin-scheduler-loader").hide(); - $("#plugin-scheduler").show(); - //fixOddEvenClasses(); - //$("#" + schedulerTblId + " td:not([class])").addClass("free"); - if (schedulerDebug) console.timeEnd("_initUI"); - }, + }); + // + + + //End btn Submit leases + + //other stuff + $("#plugin-scheduler-loader").hide(); + $("#plugin-scheduler").show(); + //fixOddEvenClasses(); + //$("#" + schedulerTblId + " td:not([class])").addClass("free"); + if (schedulerDebug) console.timeEnd("_initUI"); + }, _FixLeases : function () { for (var i = 0; i < tmpSchedulerLeases.length; i++) { @@ -427,7 +497,7 @@ var tmpSchedulerLeases = []; if (schedulerDebug && colValue == 'omf') colValue = 'nitos'; if (op == '=' || op == '==') { - if (colValue != value || colValue == null || colValue == "" || colValue == "n/a") + if (colValue != value || colValue == null || colValue == "" || colValue == "n/a") ret = false; } else if (op == 'included') { $.each(value, function (i, x) { @@ -439,7 +509,7 @@ var tmpSchedulerLeases = []; } }); } else if (op == '!=') { - if (colValue == value || colValue == null || colValue == "" || colValue == "n/a") + if (colValue == value || colValue == null || colValue == "" || colValue == "n/a") ret = false; }