/** Model for the whole Queue with all it's items **/ function QueueListModel(parent) { // Internal var's var self = this; self.parent = parent; self.dragging = false; self.rawCatList = []; self.rawScriptList = []; // Because SABNZB returns the name // But when you want to set Priority you need the number.. self.priorityName = []; self.priorityName["Force"] = 2; self.priorityName["High"] = 1; self.priorityName["Normal"] = 0; self.priorityName["Low"] = -1; self.priorityName["Stop"] = -4; self.priorityOptions = ko.observableArray([ { value: 2, name: glitterTranslate.priority["Force"] }, { value: 1, name: glitterTranslate.priority["High"] }, { value: 0, name: glitterTranslate.priority["Normal"] }, { value: -1, name: glitterTranslate.priority["Low"] }, { value: -4, name: glitterTranslate.priority["Stop"] } ]); self.processingOptions = ko.observableArray([ { value: 0, name: glitterTranslate.pp["Download"] }, { value: 1, name: glitterTranslate.pp["+Repair"] }, { value: 2, name: glitterTranslate.pp["+Unpack"] }, { value: 3, name: glitterTranslate.pp["+Delete"] } ]); // External var's self.queueItems = ko.observableArray([]); self.totalItems = ko.observable(0); self.isMultiEditing = ko.observable(false); self.isLoading = ko.observable(false).extend({ rateLimit: 100 }); self.multiEditItems = ko.observableArray([]); self.categoriesList = ko.observableArray([]); self.scriptsList = ko.observableArray([]); self.searchTerm = ko.observable('').extend({ rateLimit: { timeout: 200, method: "notifyWhenChangesStop" } }); self.paginationLimit = ko.observable(20).extend({ persist: 'queuePaginationLimit' }); self.pagination = new paginationModel(self); // Don't update while dragging self.shouldUpdate = function() { return !self.dragging; } self.dragStart = function() { self.dragging = true; } self.dragStop = function(event) { // Remove that extra label $(event.target).parent().removeClass('table-active-sorting') // Wait a little before refreshing again (prevents jumping) setTimeout(function() { self.dragging = false; }, 500) } // Update slots from API data self.updateFromData = function(data) { // Get all ID's var itemIds = $.map(self.queueItems(), function(i) { return i.id; }); // Did the category-list change? // Otherwise KO will send updates to all in the dropdown are a hugggeeee slowdown on initial load! // Only loading on click cuts half the speed (especially on large queues) self.toggleDropdown = function(item, event) { self.hasDropdown(true) // Keep it open! keepOpen(event.target) } // Change of settings self.changeCat = function(item, event) { callAPI({ mode: 'change_cat', value: item.id, value2: item.category() }).then(function() { // Hide all tooltips before we refresh $('.queue-item-settings li').filter('[data-tooltip="true"]').tooltip('hide') self.parent.parent.refresh() }) } self.changeScript = function(item) { // Not on empty handlers if(!item.script() || parent.scriptsList().length <= 1) return; callAPI({ mode: 'change_script', value: item.id, value2: item.script() }) } self.changeProcessing = function(item) { callAPI({ mode: 'change_opts', value: item.id, value2: item.unpackopts() }) } self.changePriority = function(item, event) { // Not if we are fetching extra blocks for repair! if(item.isFetchingBlocks) return callAPI({ mode: 'queue', name: 'priority', value: item.id, value2: item.priority() }).then(function() { // Hide all tooltips before we refresh $('.queue-item-settings li').filter('[data-tooltip="true"]').tooltip('hide') self.parent.parent.refresh() }) } // Remove 1 download from queue self.removeDownload = function(item, event) { // Confirm and remove if(!self.parent.parent.confirmDeleteQueue() || confirm(glitterTranslate.deleteMsg + ":\n" + item.name() + "\n\n" + glitterTranslate.removeDow1)) { var itemToDelete = this; // Show notification showNotification('.main-notification-box-removing') callAPI({ mode: 'queue', name: 'delete', del_files: 1, value: item.id }).then(function(response) { // Make sure no flickering (if there are more items left) and then remove self.parent.isLoading(self.parent.totalItems() > 1) parent.queueItems.remove(itemToDelete); parent.multiEditItems.remove(function(inList) { return inList.id == itemToDelete.id; }) self.parent.parent.refresh(); // Hide notifcation hideNotification(true) }); } }; }