diff --git a/src/sunstone/public/app/utils/schedule_action.js b/src/sunstone/public/app/utils/schedule_action.js index 018b1739b0..cf0db9461b 100644 --- a/src/sunstone/public/app/utils/schedule_action.js +++ b/src/sunstone/public/app/utils/schedule_action.js @@ -15,90 +15,90 @@ /* -------------------------------------------------------------------------- */ define(function (require) { - var Config = require("sunstone-config"); - var Locale = require("utils/locale"); - var Humanize = require("utils/humanize"); - var TemplateUtils = require("utils/template-utils"); - var Tips = require("utils/tips"); - var Notifier = require("utils/notifier"); + var Config = require("sunstone-config"); + var Locale = require("utils/locale"); + var Humanize = require("utils/humanize"); + var TemplateUtils = require("utils/template-utils"); + var Tips = require("utils/tips"); + var Notifier = require("utils/notifier"); - var TemplateHTML = require("hbs!./schedule_action/html"); - var TemplateTableHTML = require("hbs!./schedule_action/table"); + var TemplateHTML = require("hbs!./schedule_action/html"); + var TemplateTableHTML = require("hbs!./schedule_action/table"); - function _html(resource) { - this.res = resource; - return TemplateTableHTML({ - res: resource - }); - } + function _html(resource) { + this.res = resource; + return TemplateTableHTML({ + res: resource + }); + } - function _htmlNewAction(actions, context, res) { - var options = ""; - var that = this; - $.each(actions, function (key, action) { - var actionAux = action.replace("-", "_"); - if (Config.isTabActionEnabled("vms-tab", "VM." + actionAux)) { - options += ""; - } - }); - $("#scheduling_" + res + "_actions_table tbody", context).append(TemplateHTML({ - "actions": options, - "res": that.res - })).find("#relative_time", context).on("click", function(e){ - $("#schedule_type",context).prop("checked",false); - if($(this).is(":checked")){ - $("#no_relative_time_form, .periodic",context).addClass("hide"); - $("#schedule_time",context).prop("",false); - $("#relative_time_form",context).removeClass("hide"); - }else{ - $("#relative_time_form",context).addClass("hide"); - $("#no_relative_time_form",context).removeClass("hide"); + function _htmlNewAction(actions, context, res) { + var options = ""; + var that = this; + $.each(actions, function (key, action) { + var actionAux = action.replace("-", "_"); + if (Config.isTabActionEnabled("vms-tab", "VM." + actionAux)) { + options += ""; } }); - if (res === "vms"){ - $("#title", context).prop("colspan", "2"); - $("#td_days", context).prop("colspan", "5"); - } - } + $("#scheduling_" + res + "_actions_table tbody", context).append(TemplateHTML({ + "actions": options, + "res": that.res + })).find("#relative_time", context).on("click", function (e) { + $("#schedule_type", context).prop("checked", false); + if ($(this).is(":checked")) { + $("#no_relative_time_form, .periodic", context).addClass("hide"); + $("#schedule_time", context).prop("", false); + $("#relative_time_form", context).removeClass("hide"); + } else { + $("#relative_time_form", context).addClass("hide"); + $("#no_relative_time_form", context).removeClass("hide"); + } + }); + if (res === "vms") { + $("#title", context).prop("colspan", "2"); + $("#td_days", context).prop("colspan", "5"); + } + } - function _setup(context) { - var today = new Date(); - var dd = today.getDate(); - var mm = today.getMonth() + 1; - var yyyy = today.getFullYear(); - if (dd < 10) { - dd = "0" + dd; - } - if (mm < 10) { - mm = "0" + mm; - } - today = yyyy + "-" + mm + "-" + dd; - $("#date_input", context).attr("min", today); - $("#date_input", context).attr("value", today); + function _setup(context) { + var today = new Date(); + var dd = today.getDate(); + var mm = today.getMonth() + 1; + var yyyy = today.getFullYear(); + if (dd < 10) { + dd = "0" + dd; + } + if (mm < 10) { + mm = "0" + mm; + } + today = yyyy + "-" + mm + "-" + dd; + $("#date_input", context).attr("min", today); + $("#date_input", context).attr("value", today); - $(".periodic", context).hide(); + $(".periodic", context).hide(); - $("input#schedule_type", context).on("change", function () { - var periodic = $(this).prop("checked"); + $("input#schedule_type", context).on("change", function () { + var periodic = $(this).prop("checked"); - if (periodic) { - $(".periodic", context).show(); - $(".non-periodic", context).hide(); - } else { - $(".periodic", context).hide(); - $(".non-periodic", context).show(); - } - }); - var that = this; - this.repeat = "week"; - this.end_type = "never"; - $("select[name='repeat']", context).change(function () { - var value = $(this).val(); - that.repeat = value; - var input_html = ""; - switch (value) { - case "week": - input_html = "
\ + if (periodic) { + $(".periodic", context).show(); + $(".non-periodic", context).hide(); + } else { + $(".periodic", context).hide(); + $(".non-periodic", context).show(); + } + }); + var that = this; + this.repeat = "week"; + this.end_type = "never"; + $("select[name='repeat']", context).change(function () { + var value = $(this).val(); + that.repeat = value; + var input_html = ""; + switch (value) { + case "week": + input_html = "
\ \ \ \ @@ -107,326 +107,329 @@ define(function (require) { \ \
"; - break; - case "month": - input_html = "
\ - "+Locale.tr("Comma separated list of days of the month to repeat the action on. Ex: 1,15,25 repeats the action every first, 15th and 25th day of the month")+"
"; - break; - case "year": - input_html = "
\ - "+Locale.tr("Comma separated list of days of the year to repeat the action on. Ex: 1,30,330 repeats the action every first, 30th and 330th day of the year")+"
"; - break; - case "hour": - input_html = "
\ - \ + break; + case "month": + input_html = "
\ + "+ Locale.tr("Comma separated list of days of the month to repeat the action on. Ex: 1,15,25 repeats the action every first, 15th and 25th day of the month") + "
"; + break; + case "year": + input_html = "
\ + "+ Locale.tr("Comma separated list of days of the year to repeat the action on. Ex: 1,30,330 repeats the action every first, 30th and 330th day of the year") + "
"; + break; + case "hour": + input_html = "
\ + \ \ - \ + \
"; - break; - } - $("#td_days").html(input_html); - Tips.setup(context); - }); + break; + } + $("#td_days").html(input_html); + Tips.setup(context); + }); - $("input[name='end_type']", context).change(function () { - var value = $(this).val(); - that.end_type = value; - var input_html = ""; - var min; - $(".end_input", context).prop("disabled", true); - switch (value) { - case "n_rep": - min = 1; - break; - case "date": - var today = new Date(); - var dd = today.getDate(); - var mm = today.getMonth() + 1; - var yyyy = today.getFullYear(); - if (dd < 10) { - dd = "0" + dd; - } - if (mm < 10) { - mm = "0" + mm; - } - min = yyyy + "-" + mm + "-" + dd; - break; - } - $("#end_value_" + value, context).attr("min", min); - $("#end_value_" + value, context).prop("disabled", false); - }); + $("input[name='end_type']", context).change(function () { + var value = $(this).val(); + that.end_type = value; + var input_html = ""; + var min; + $(".end_input", context).prop("disabled", true); + switch (value) { + case "n_rep": + min = 1; + break; + case "date": + var today = new Date(); + var dd = today.getDate(); + var mm = today.getMonth() + 1; + var yyyy = today.getFullYear(); + if (dd < 10) { + dd = "0" + dd; + } + if (mm < 10) { + mm = "0" + mm; + } + min = yyyy + "-" + mm + "-" + dd; + break; + } + $("#end_value_" + value, context).attr("min", min); + $("#end_value_" + value, context).prop("disabled", false); + }); - context.on("focusout", "#time_input", function () { - $("#time_input").removeAttr("data-invalid"); - $("#time_input").removeAttr("class"); - }); + context.on("focusout", "#time_input", function () { + $("#time_input").removeAttr("data-invalid"); + $("#time_input").removeAttr("class"); + }); - } + } - function _retrieve(context) { - $("#scheduling_" + this.res + "_actions_table .create", context).remove(); - var actionsJSON = []; - $("#scheduling_" + this.res + "_actions_table tbody tr").each(function (index) { - var first = $(this).children("td")[0]; - if (!$("select", first).html()) { - var actionJSON = {}; - if ($(this).attr("data")){ - actionJSON = JSON.parse($(this).attr("data")); - actionJSON.ID = index; - } - } - if (!$.isEmptyObject(actionJSON)) { actionsJSON.push(actionJSON); }; - }); - return actionsJSON; - } + function _retrieve(context) { + $("#scheduling_" + this.res + "_actions_table .create", context).remove(); + var actionsJSON = []; + $("#scheduling_" + this.res + "_actions_table tbody tr").each(function (index) { + var first = $(this).children("td")[0]; + if (!$("select", first).html()) { + var actionJSON = {}; + if ($(this).attr("data")) { + actionJSON = JSON.parse($(this).attr("data")); + actionJSON.ID = index; + } + } + if (!$.isEmptyObject(actionJSON)) { actionsJSON.push(actionJSON); }; + }); + return actionsJSON; + } - function _retrieveNewAction(context) { - var relative_time = $("#relative_time",context).prop("checked"); + function _retrieveNewAction(context) { + var relative_time = $("#relative_time", context).prop("checked"); var new_action = $("#select_new_action", context).val(); var sched_action = {}; - if(relative_time){ + if (relative_time) { var time_number = $("#time_number", context).val(); var time_unit = $("#time_unit", context).val(); var send_time = 0; - if (time_number === "" || time_number <= 0 ) { - Notifier.notifyError("Time Number not defined."); - return false; - } + if (time_number === "" || time_number <= 0) { + Notifier.notifyError("Time Number not defined."); + return false; + } switch (time_unit) { case "years": send_time = time_number * 365 * 24 * 3600; - break; + break; case "months": send_time = time_number * 30 * 24 * 3600; - break; + break; case "weeks": send_time = time_number * 7 * 24 * 3600; - break; + break; case "days": send_time = time_number * 24 * 3600; - break; + break; case "hours": send_time = time_number * 3600; - break; + break; case "minutes": send_time = time_number * 60; - break; + break; default: Notifier.notifyError("Error in unit time"); - return false; - break; + return false; + break; } sched_action.TIME = "+" + send_time; - }else{ + } else { var periodic = $("input#schedule_type", context).prop("checked"); - var time_input_value = $("#time_input", context).val(); - var date_input_value = $("#date_input", context).val(); - var rep = 0; - var end_type = 1; - var days = ""; - var end_value = 1; - if (date_input_value === "") { - Notifier.notifyError("Date not defined."); - return false; - } - if (time_input_value === ""){ - Notifier.notifyError("Time not defined."); - return false; + var time_input_value = $("#time_input", context).val(); + var date_input_value = $("#date_input", context).val(); + var rep = 0; + var end_type = 1; + var days = ""; + var end_value = 1; + if (date_input_value === "") { + Notifier.notifyError("Date not defined."); + return false; } + if (time_input_value === "") { + Notifier.notifyError("Time not defined."); + return false; + } + end_type = 2; var timeCal = date_input_value + " " + time_input_value; - epochStr = new Date(timeCal); + epochStr = new Date(timeCal); var time = parseInt(epochStr.getTime()) / 1000; sched_action.END_TYPE = end_type; sched_action.END_VALUE = time; - sched_action.TIME = time; - if (periodic) { - if (!this.repeat || !this.end_type) { - return false; - } - if (this.repeat === "week") { - $("input[name='days']:checked").each(function () { - days = days + (this).value + ","; - }); - days = days.slice(0, -1); - } else if (this.repeat === "month") { - rep = 1; - days = $("#days_month_value", context).val(); - } else if (this.repeat === "year"){ - rep = 2; - days = $("#days_year_value", context).val(); - } else { - rep = 3; - days = $("#days_hour_value", context).val(); - } - if (days === "") { - Notifier.notifyError("Hours or days not defined."); - return false; - } - if (this.end_type === "never") { - end_type = 0; - } else if (this.end_type === "n_rep") { - end_value = $("#end_value_n_rep", context).val(); - if (end_value === "") { - Notifier.notifyError("Repetition number not defined."); - return false; - } - } else if (this.end_type === "date") { - end_type = 2; - end_date = $("#end_value_date", context).val(); - if (end_date === "") { - Notifier.notifyError("End date not defined."); - return false; - } - var time_value = end_date + " " + time_input_value; - var epoch_str = new Date(time_value); - end_value = parseInt(epoch_str.getTime()) / 1000; - } - sched_action.DAYS = days; - sched_action.REPEAT = rep; - sched_action.END_VALUE = end_value; - } + sched_action.TIME = time; + if (periodic) { + end_type = 1; + if (!this.repeat || !this.end_type) { + return false; + } + if (this.repeat === "week") { + $("input[name='days']:checked").each(function () { + days = days + (this).value + ","; + }); + days = days.slice(0, -1); + } else if (this.repeat === "month") { + rep = 1; + days = $("#days_month_value", context).val(); + } else if (this.repeat === "year") { + rep = 2; + days = $("#days_year_value", context).val(); + } else { + rep = 3; + days = $("#days_hour_value", context).val(); + } + if (days === "") { + Notifier.notifyError("Hours or days not defined."); + return false; + } + if (this.end_type === "never") { + end_type = 0; + } else if (this.end_type === "n_rep") { + end_value = $("#end_value_n_rep", context).val(); + if (end_value === "") { + Notifier.notifyError("Repetition number not defined."); + return false; + } + } else if (this.end_type === "date") { + end_type = 2; + end_date = $("#end_value_date", context).val(); + if (end_date === "") { + Notifier.notifyError("End date not defined."); + return false; + } + var time_value = end_date + " " + time_input_value; + var epoch_str = new Date(time_value); + end_value = parseInt(epoch_str.getTime()) / 1000; + } + sched_action.DAYS = days; + sched_action.REPEAT = rep; + sched_action.END_VALUE = end_value; + } + sched_action.END_TYPE = end_type; } sched_action.ACTION = new_action; $("#scheduling_" + this.res + "_actions_table .create", context).remove(); $("#scheduling_" + this.res + "_actions_table #relative_time_form", context).remove(); $("#scheduling_" + this.res + "_actions_table #no_relative_time_form", context).remove(); - $("#no_relative_time_form",context).addClass("hide"); + $("#no_relative_time_form", context).addClass("hide"); $("#add_scheduling_" + this.res + "_action", context).removeAttr("disabled"); return sched_action; - } + } - function _fromJSONtoActionsTable(actions_array, action_id, minus) { - var str = ""; + function _fromJSONtoActionsTable(actions_array, action_id, minus) { + var str = ""; - if (!actions_array) { - return ""; - } + if (!actions_array) { + return ""; + } - if (!$.isArray(actions_array)) { - var tmp_array = new Array(); - tmp_array[0] = actions_array; - actions_array = tmp_array; - } + if (!$.isArray(actions_array)) { + var tmp_array = new Array(); + tmp_array[0] = actions_array; + actions_array = tmp_array; + } - if (!actions_array.length) { - return ""; - } + if (!actions_array.length) { + return ""; + } - $.each(actions_array, function (index, scheduling_action) { - str += _fromJSONtoActionRow(scheduling_action, action_id, minus); - }); + $.each(actions_array, function (index, scheduling_action) { + str += _fromJSONtoActionRow(scheduling_action, action_id, minus); + }); - return str; - } + return str; + } - function _parseTime(time=0){ + function _parseTime(time = 0) { r = time; - if(Math.round(time) !== time) { - r = time.toFixed(2); + if (Math.round(time) !== time) { + r = time.toFixed(2); } return r; } - function _time(unit=undefined){ - if(unit && unit>0){ + function _time(unit = undefined) { + if (unit && unit > 0) { years = unit / 365 / 24 / 3600; months = unit / 30 / 24 / 3600; weeks = unit / 7 / 24 / 3600; days = unit / 24 / 3600; hours = unit / 3600; minutes = unit / 60; - if(years >= 1){ + if (years >= 1) { return _parseTime(years) + " " + Locale.tr("Years"); } - if(months >= 1){ + if (months >= 1) { return _parseTime(months) + " " + Locale.tr("Months"); } - if(weeks >= 1){ + if (weeks >= 1) { return _parseTime(weeks) + " " + Locale.tr("Weeks"); } - if(days >= 1){ + if (days >= 1) { return _parseTime(days) + " " + Locale.tr("Days"); } - if(hours >= 1){ + if (hours >= 1) { return _parseTime(hours) + " " + Locale.tr("Hours"); } - if(minutes >= 1){ + if (minutes >= 1) { return _parseTime(minutes) + " " + Locale.tr("Minutes"); } } } - function _fromJSONtoActionRow(scheduling_action, action_id, minus) { - var time_str = Humanize.prettyTime(scheduling_action.TIME); - var rep_str = ""; - var end_str = ""; + function _fromJSONtoActionRow(scheduling_action, action_id, minus) { + var time_str = Humanize.prettyTime(scheduling_action.TIME); + var rep_str = ""; + var end_str = ""; - if (scheduling_action.REPEAT !== undefined) { - if (scheduling_action.REPEAT == 0) { - rep_str = "Weekly "; - } else if (scheduling_action.REPEAT == 1) { - rep_str = "Monthly "; - } else if (scheduling_action.REPEAT == 2) { - rep_str = "Yearly "; - } else if (scheduling_action.REPEAT == 3) { - rep_str = "Each " + scheduling_action.DAYS + " hours"; - } + if (scheduling_action.REPEAT !== undefined) { + if (scheduling_action.REPEAT == 0) { + rep_str = "Weekly "; + } else if (scheduling_action.REPEAT == 1) { + rep_str = "Monthly "; + } else if (scheduling_action.REPEAT == 2) { + rep_str = "Yearly "; + } else if (scheduling_action.REPEAT == 3) { + rep_str = "Each " + scheduling_action.DAYS + " hours"; + } - if (scheduling_action.REPEAT != 3) { - if (scheduling_action.REPEAT != 0) { - rep_str += scheduling_action.DAYS; - } else { - rep_str += Humanize.week_days(scheduling_action.DAYS); - } - } - } + if (scheduling_action.REPEAT != 3) { + if (scheduling_action.REPEAT != 0) { + rep_str += scheduling_action.DAYS; + } else { + rep_str += Humanize.week_days(scheduling_action.DAYS); + } + } + } - if (scheduling_action.END_TYPE !== undefined) { - if (scheduling_action.END_TYPE == 0) { - end_str = "None"; - } else if (scheduling_action.END_TYPE == 1) { - end_str = "After " + scheduling_action.END_VALUE + " times"; - } else if (scheduling_action.END_TYPE == 2) { - end_str = "on " + Humanize.prettyTime(scheduling_action.END_VALUE); - } - } + if (scheduling_action.END_TYPE !== undefined) { + if (scheduling_action.END_TYPE == 0) { + end_str = "None"; + } else if (scheduling_action.END_TYPE == 1) { + end_str = "After " + scheduling_action.END_VALUE + " times"; + } else if (scheduling_action.END_TYPE == 2) { + end_str = "on " + Humanize.prettyTime(scheduling_action.END_VALUE); + } + } - var str = ""; - if (action_id === undefined) { - str += ""; + var str = ""; + if (action_id === undefined) { + str += ""; } var time = scheduling_action.TIME.toString(); - time = isNaN(time)? time_str : (time && time.match(/^\+(.*)/gi)? _time(time) : time_str); + time = isNaN(time) ? time_str : (time && time.match(/^\+(.*)/gi) ? _time(time) : time_str); - str += "" + TemplateUtils.htmlEncode(scheduling_action.ACTION) + "\ + str += "" + TemplateUtils.htmlEncode(scheduling_action.ACTION) + "\ " + time + "\ " + rep_str + "\ " + end_str + ""; - if (minus === undefined) { - str += "\ + if (minus === undefined) { + str += "\
\ \
\ \ "; - } + } - return str; - } + return str; + } - function convertDate(date_string) { - date_string = date_string.split("/"); - return date_string[2] + "-" + date_string[1] + "-" + date_string[0]; - } + function convertDate(date_string) { + date_string = date_string.split("/"); + return date_string[2] + "-" + date_string[1] + "-" + date_string[0]; + } - return { - "fromJSONtoActionRow": _fromJSONtoActionRow, - "fromJSONtoActionsTable": _fromJSONtoActionsTable, - "htmlNewAction": _htmlNewAction, - "setup": _setup, - "htmlTable": _html, - "retrieveNewAction": _retrieveNewAction, - "retrieve": _retrieve - }; + return { + "fromJSONtoActionRow": _fromJSONtoActionRow, + "fromJSONtoActionsTable": _fromJSONtoActionsTable, + "htmlNewAction": _htmlNewAction, + "setup": _setup, + "htmlTable": _html, + "retrieveNewAction": _retrieveNewAction, + "retrieve": _retrieve + }; }); \ No newline at end of file