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

340 lines
10 KiB
JavaScript
Raw Normal View History

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