jellyfin-web/dashboard-ui/scripts/scheduledtaskpage.js

334 lines
9.0 KiB
JavaScript
Raw Normal View History

2015-12-14 08:43:03 -07:00
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function (from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
var ScheduledTaskPage = {
2013-02-20 18:33:05 -07:00
refreshScheduledTask: function () {
Dashboard.showLoadingMsg();
var id = getParameterByName('id');
2015-12-14 08:43:03 -07:00
ApiClient.getScheduledTask(id).then(ScheduledTaskPage.loadScheduledTask);
2013-02-20 18:33:05 -07:00
},
loadScheduledTask: function (task) {
2016-02-07 12:47:09 -07:00
var page = $($.mobile.activePage)[0];
2013-02-20 18:33:05 -07:00
2014-07-21 18:29:06 -07:00
$('.taskName', page).html(task.Name);
$('#pTaskDescription', page).html(task.Description);
2013-02-20 18:33:05 -07:00
2016-02-07 12:47:09 -07:00
require(['paper-fab', 'paper-item-body', 'paper-icon-item'], function () {
ScheduledTaskPage.loadTaskTriggers(page, task);
});
2013-02-20 18:33:05 -07:00
Dashboard.hideLoadingMsg();
},
2016-02-07 12:47:09 -07:00
loadTaskTriggers: function (context, task) {
2013-02-20 18:33:05 -07:00
var html = '';
2016-02-07 12:47:09 -07:00
html += '<div class="paperList">';
2013-02-20 18:33:05 -07:00
for (var i = 0, length = task.Triggers.length; i < length; i++) {
var trigger = task.Triggers[i];
2016-02-07 12:47:09 -07:00
html += '<paper-icon-item>';
2013-02-20 18:33:05 -07:00
2016-02-08 11:24:01 -07:00
html += '<paper-fab mini icon="schedule" class="blue" item-icon></paper-fab>';
2016-02-07 12:47:09 -07:00
html += '<paper-item-body two-line>';
html += "<div>" + ScheduledTaskPage.getTriggerFriendlyName(trigger) + "</div>";
if (trigger.MaxRuntimeMs) {
2016-02-07 12:47:09 -07:00
html += '<div secondary>';
var hours = trigger.MaxRuntimeMs / 3600000;
if (hours == 1) {
html += Globalize.translate('ValueTimeLimitSingleHour');
} else {
html += Globalize.translate('ValueTimeLimitMultiHour', hours);
}
2016-02-07 12:47:09 -07:00
html += '</div>';
}
2016-02-07 12:47:09 -07:00
html += '</paper-item-body>';
2013-02-20 18:33:05 -07:00
2016-05-07 23:31:08 -07:00
html += '<button type="button" is="paper-icon-button-light" title="' + Globalize.translate('ButtonDelete') + '" onclick="ScheduledTaskPage.confirmDeleteTrigger(' + i + ');"><iron-icon icon="delete"></iron-icon></button>';
2013-02-20 18:33:05 -07:00
2016-02-07 12:47:09 -07:00
html += '</paper-icon-item>';
2013-02-20 18:33:05 -07:00
}
2016-02-07 12:47:09 -07:00
html += '</div>';
context.querySelector('.taskTriggers').innerHTML = html;
2013-02-20 18:33:05 -07:00
},
getTriggerFriendlyName: function (trigger) {
if (trigger.Type == 'DailyTrigger') {
return 'Daily at ' + ScheduledTaskPage.getDisplayTime(trigger.TimeOfDayTicks);
}
if (trigger.Type == 'WeeklyTrigger') {
return trigger.DayOfWeek + 's at ' + ScheduledTaskPage.getDisplayTime(trigger.TimeOfDayTicks);
}
if (trigger.Type == 'SystemEventTrigger') {
if (trigger.SystemEvent == 'WakeFromSleep') {
return 'On wake from sleep';
}
}
if (trigger.Type == 'IntervalTrigger') {
var hours = trigger.IntervalTicks / 36000000000;
if (hours == .25) {
return "Every 15 minutes";
}
if (hours == .5) {
return "Every 30 minutes";
}
if (hours == .75) {
return "Every 45 minutes";
}
if (hours == 1) {
return "Every hour";
}
return 'Every ' + hours + ' hours';
}
if (trigger.Type == 'StartupTrigger') {
return 'On application startup';
}
return trigger.Type;
},
getDisplayTime: function (ticks) {
var hours = ticks / 36000000000;
if (hours < 1) {
hours = 0;
}
2014-03-19 10:44:55 -07:00
hours = Math.floor(hours);
2013-02-20 18:33:05 -07:00
ticks -= (hours * 36000000000);
2014-03-19 10:44:55 -07:00
var minutes = Math.floor((ticks / 600000000));
2013-02-20 18:33:05 -07:00
var suffix = "am";
if (hours > 11) {
suffix = "pm";
}
hours = hours % 12;
if (hours == 0) {
hours = 12;
}
if (minutes < 10) {
minutes = '0' + minutes;
}
return hours + ':' + minutes + ' ' + suffix;
},
showAddTriggerPopup: function () {
var page = $.mobile.activePage;
2015-12-23 10:46:01 -07:00
$('#selectTriggerType', page).val('DailyTrigger').trigger('change');
2013-02-20 18:33:05 -07:00
2015-06-07 18:23:56 -07:00
$('#popupAddTrigger', page).on("popupafteropen", function () {
2013-03-28 20:50:46 -07:00
$('#addTriggerForm input:first', this).focus();
}).popup("open").on("popupafterclose", function () {
2013-02-20 18:33:05 -07:00
$('#addTriggerForm', page).off("submit");
$(this).off("popupafterclose");
});
2014-03-26 08:06:48 -07:00
},
2013-02-20 18:33:05 -07:00
confirmDeleteTrigger: function (index) {
2016-02-22 11:47:56 -07:00
require(['confirm'], function (confirm) {
confirm(Globalize.translate('MessageDeleteTaskTrigger'), Globalize.translate('HeaderDeleteTaskTrigger')).then(function () {
2013-02-20 18:33:05 -07:00
ScheduledTaskPage.deleteTrigger(index);
2016-02-22 11:47:56 -07:00
});
2013-02-20 18:33:05 -07:00
});
},
deleteTrigger: function (index) {
Dashboard.showLoadingMsg();
var id = getParameterByName('id');
2015-12-14 08:43:03 -07:00
ApiClient.getScheduledTask(id).then(function (task) {
2013-02-20 18:33:05 -07:00
task.Triggers.remove(index);
2015-12-14 08:43:03 -07:00
ApiClient.updateScheduledTaskTriggers(task.Id, task.Triggers).then(function () {
2013-02-20 18:33:05 -07:00
ScheduledTaskPage.refreshScheduledTask();
});
});
},
refreshTriggerFields: function (triggerType) {
var page = $.mobile.activePage;
if (triggerType == 'DailyTrigger') {
$('#fldTimeOfDay', page).show();
$('#fldDayOfWeek', page).hide();
$('#fldSelectSystemEvent', page).hide();
$('#fldSelectInterval', page).hide();
$('#txtTimeOfDay', page).attr('required', 'required');
}
else if (triggerType == 'WeeklyTrigger') {
$('#fldTimeOfDay', page).show();
$('#fldDayOfWeek', page).show();
$('#fldSelectSystemEvent', page).hide();
$('#fldSelectInterval', page).hide();
$('#txtTimeOfDay', page).attr('required', 'required');
}
else if (triggerType == 'SystemEventTrigger') {
$('#fldTimeOfDay', page).hide();
$('#fldDayOfWeek', page).hide();
$('#fldSelectSystemEvent', page).show();
$('#fldSelectInterval', page).hide();
$('#txtTimeOfDay', page).removeAttr('required');
}
else if (triggerType == 'IntervalTrigger') {
$('#fldTimeOfDay', page).hide();
$('#fldDayOfWeek', page).hide();
$('#fldSelectSystemEvent', page).hide();
$('#fldSelectInterval', page).show();
$('#txtTimeOfDay', page).removeAttr('required');
}
else if (triggerType == 'StartupTrigger') {
$('#fldTimeOfDay', page).hide();
$('#fldDayOfWeek', page).hide();
$('#fldSelectSystemEvent', page).hide();
$('#fldSelectInterval', page).hide();
$('#txtTimeOfDay', page).removeAttr('required');
}
},
getTriggerToAdd: function () {
var page = $.mobile.activePage;
var trigger = {
Type: $('#selectTriggerType', page).val()
};
if (trigger.Type == 'DailyTrigger') {
trigger.TimeOfDayTicks = ScheduledTaskPage.getTimeOfDayTicks($('#txtTimeOfDay', page).val());
}
else if (trigger.Type == 'WeeklyTrigger') {
trigger.DayOfWeek = $('#selectDayOfWeek', page).val();
trigger.TimeOfDayTicks = ScheduledTaskPage.getTimeOfDayTicks($('#txtTimeOfDay', page).val());
}
else if (trigger.Type == 'SystemEventTrigger') {
trigger.SystemEvent = $('#selectSystemEvent', page).val();
}
else if (trigger.Type == 'IntervalTrigger') {
trigger.IntervalTicks = $('#selectInterval', page).val();
}
var timeLimit = $('#txtTimeLimit', page).val() || '0';
timeLimit = parseFloat(timeLimit) * 3600000;
trigger.MaxRuntimeMs = timeLimit || null;
2013-02-20 18:33:05 -07:00
return trigger;
},
getTimeOfDayTicks: function (val) {
var vals = val.split(':');
var hours = vals[0];
2015-12-14 08:43:03 -07:00
var minutes = vals[1].split(' ')[0];
2013-02-20 18:33:05 -07:00
// Add hours
var ticks = hours * 60 * 60 * 1000 * 10000;
ticks += minutes * 60 * 1000 * 10000;
return ticks;
}
};
2015-06-07 18:23:56 -07:00
(function () {
function onSubmit() {
Dashboard.showLoadingMsg();
var id = getParameterByName('id');
2015-12-14 08:43:03 -07:00
ApiClient.getScheduledTask(id).then(function (task) {
2015-06-07 18:23:56 -07:00
task.Triggers.push(ScheduledTaskPage.getTriggerToAdd());
2015-12-14 08:43:03 -07:00
ApiClient.updateScheduledTaskTriggers(task.Id, task.Triggers).then(function () {
2015-06-07 18:23:56 -07:00
$('#popupAddTrigger').popup('close');
ScheduledTaskPage.refreshScheduledTask();
});
});
return false;
}
2015-09-01 07:01:59 -07:00
$(document).on('pageinit', "#scheduledTaskPage", function () {
2015-06-07 18:23:56 -07:00
2016-02-26 10:15:06 -07:00
var page = this;
2015-06-07 18:23:56 -07:00
$('.addTriggerForm').off('submit', onSubmit).on('submit', onSubmit);
2016-02-26 10:15:06 -07:00
page.querySelector('.timeFieldExample').innerHTML = Globalize.translate('ValueExample', '1:00 PM');
2015-09-24 10:08:10 -07:00
}).on('pageshow', "#scheduledTaskPage", function () {
2015-06-07 18:23:56 -07:00
ScheduledTaskPage.refreshScheduledTask();
});
})();