From 76d93c23b7739140379cae725282e137521fdd5d Mon Sep 17 00:00:00 2001 From: Felipe Barriga Richards Date: Thu, 26 Feb 2015 22:52:38 -0300 Subject: [PATCH 1/3] webui (c++): feature: added labels support. #648 --- src/webui/btjson.cpp | 1 + src/webui/prefjson.cpp | 1 + src/webui/webapplication.cpp | 19 +++++++++++++++++++ src/webui/webapplication.h | 1 + 4 files changed, 22 insertions(+) diff --git a/src/webui/btjson.cpp b/src/webui/btjson.cpp index 44addf7f0..fdbb65661 100644 --- a/src/webui/btjson.cpp +++ b/src/webui/btjson.cpp @@ -255,6 +255,7 @@ private: * - "seq_dl": Torrent sequential download state * - "f_l_piece_prio": Torrent first last piece priority state * - "force_start": Torrent force start state + * - "label": Torrent label */ QByteArray btjson::getTorrents(QString filter, QString label, QString sortedColumn, bool reverse, int limit, int offset) diff --git a/src/webui/prefjson.cpp b/src/webui/prefjson.cpp index bf206df39..c82fa782d 100644 --- a/src/webui/prefjson.cpp +++ b/src/webui/prefjson.cpp @@ -396,3 +396,4 @@ void prefjson::setPreferences(const QString& json) // Save preferences pref->apply(); } + diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index 1b2dfee6f..16916b6eb 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -110,6 +110,7 @@ QMap > WebApplication::initialize ADD_ACTION(command, topPrio); ADD_ACTION(command, bottomPrio); ADD_ACTION(command, recheck); + ADD_ACTION(command, setLabel); ADD_ACTION(version, api); ADD_ACTION(version, api_min); ADD_ACTION(version, qbittorrent); @@ -664,6 +665,24 @@ void WebApplication::action_command_recheck() torrent->forceRecheck(); } +void WebApplication::action_command_setLabel() +{ + CHECK_URI(0); + CHECK_PARAMETERS("hash" << "label_obj"); + + QString hash = request().posts["hash"]; + QString label_obj = request().posts["label_obj"]; + + const QVariantMap m = json::fromJson(label_obj).toMap(); + if( m.contains("value") ) { + QString label = m["value"].toString(); + if (!hash.isEmpty()) { + QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); + QBtSession::instance()->setLabel(h, label); + } + } +} + bool WebApplication::isPublicScope() { return (scope_ == DEFAULT_SCOPE || scope_ == VERSION_INFO); diff --git a/src/webui/webapplication.h b/src/webui/webapplication.h index e4bef1ab1..5424ce2c5 100644 --- a/src/webui/webapplication.h +++ b/src/webui/webapplication.h @@ -86,6 +86,7 @@ private: void action_command_topPrio(); void action_command_bottomPrio(); void action_command_recheck(); + void action_command_setLabel(); void action_version_api(); void action_version_api_min(); void action_version_qbittorrent(); From 4ae2f6c33b92a8e66edebac851e082ddeaecd1f1 Mon Sep 17 00:00:00 2001 From: Felipe Barriga Richards Date: Sat, 14 Feb 2015 18:19:54 -0300 Subject: [PATCH 2/3] webui (js): feature: Added complete support for labels (add/set/reset/display/filter) #648 Changes: - added list of labels on the lower-left corner - added support to add/set/reset labels on context menu - added support to filter torrents by label --- src/webui/prefjson.cpp | 1 - src/webui/webapplication.cpp | 6 +- src/webui/webui.qrc | 1 + src/webui/www/private/index.html | 5 + src/webui/www/public/filters.html | 7 +- src/webui/www/public/newlabel.html | 64 ++++++++ src/webui/www/public/scripts/client.js | 149 ++++++++++++++++--- src/webui/www/public/scripts/dynamicTable.js | 8 +- src/webui/www/public/scripts/mocha-init.js | 54 +++++++ 9 files changed, 272 insertions(+), 23 deletions(-) create mode 100644 src/webui/www/public/newlabel.html diff --git a/src/webui/prefjson.cpp b/src/webui/prefjson.cpp index c82fa782d..bf206df39 100644 --- a/src/webui/prefjson.cpp +++ b/src/webui/prefjson.cpp @@ -396,4 +396,3 @@ void prefjson::setPreferences(const QString& json) // Save preferences pref->apply(); } - diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index 16916b6eb..3000c3464 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -39,6 +39,7 @@ #include "core/preferences.h" #include "btjson.h" #include "prefjson.h" +#include "jsonutils.h" #include "core/bittorrent/session.h" #include "core/bittorrent/trackerentry.h" #include "core/bittorrent/torrentinfo.h" @@ -677,8 +678,9 @@ void WebApplication::action_command_setLabel() if( m.contains("value") ) { QString label = m["value"].toString(); if (!hash.isEmpty()) { - QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); - QBtSession::instance()->setLabel(h, label); + BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash); + if (torrent) + torrent->setLabel(label); } } } diff --git a/src/webui/webui.qrc b/src/webui/webui.qrc index c77d04bdc..407b6b05f 100644 --- a/src/webui/webui.qrc +++ b/src/webui/webui.qrc @@ -27,6 +27,7 @@ www/public/download.html www/public/downloadlimit.html www/public/filters.html + www/public/newlabel.html www/public/preferences.html www/public/preferences_content.html www/public/properties.html diff --git a/src/webui/www/private/index.html b/src/webui/www/private/index.html index f7db8ab31..a0ee74397 100644 --- a/src/webui/www/private/index.html +++ b/src/webui/www/private/index.html @@ -105,6 +105,11 @@
  • QBT_TR(Pause)QBT_TR QBT_TR(Pause)QBT_TR
  • QBT_TR(Force Resume)QBT_TR QBT_TR(Force Resume)QBT_TR
  • QBT_TR(Delete)QBT_TR QBT_TR(Delete)QBT_TR
  • +
  • + QBT_TR(Label >)QBT_TR +
      +
    +
  • QBT_TR(Priority)QBT_TR +
    + QBT_TR(Labels)QBT_TR +
      +
    diff --git a/src/webui/www/public/newlabel.html b/src/webui/www/public/newlabel.html new file mode 100644 index 000000000..2b6ff512e --- /dev/null +++ b/src/webui/www/public/newlabel.html @@ -0,0 +1,64 @@ + + + + + QBT_TR(New Label)QBT_TR + + + + + + +
    +

    QBT_TR(Label)QBT_TR: + +

    + + +
    + + diff --git a/src/webui/www/public/scripts/client.js b/src/webui/www/public/scripts/client.js index b6b0a93ac..72610e33d 100644 --- a/src/webui/www/public/scripts/client.js +++ b/src/webui/www/public/scripts/client.js @@ -30,24 +30,29 @@ var alternativeSpeedLimits = false; var queueing_enabled = true; var syncMainDataTimerPeriod = 1500; -selected_filter = getLocalStorageItem('selected_filter', 'all'); -selected_label = null; +var LABELS_ALL = 1; +var LABELS_UNLABELLED = 2; + +var label_list = {}; + +var selected_label = LABELS_ALL; +var setLabelFilter = function(){}; + +var selected_filter = getLocalStorageItem('selected_filter', 'all'); +var setFilter = function(){}; var loadSelectedLabel = function () { - if (getLocalStorageItem('any_label', '1') == '0') - selected_label = getLocalStorageItem('selected_label', ''); - else - selected_label = null; -} + selected_label = getLocalStorageItem('selected_label', LABELS_ALL); +}; loadSelectedLabel(); -var saveSelectedLabel = function () { - if (selected_label == null) - localStorage.setItem('any_label', '1'); - else { - localStorage.setItem('any_label', '0'); - localStorage.setItem('selected_label', selected_label); +function genHash(string) { + var hash = 0; + for (var i = 0; i < string.length; i++) { + var c = string.charCodeAt(i); + hash = (c + hash * 31) | 0; } + return hash; } window.addEvent('load', function () { @@ -90,6 +95,14 @@ window.addEvent('load', function () { resizeLimit : [100, 300] }); + setLabelFilter = function( hash ) { + selected_label = hash; + localStorage.setItem('selected_label', selected_label); + updateLabelList(); + if (typeof myTable.table != 'undefined') + updateMainData(); + }; + setFilter = function (f) { // Visually Select the right filter $("all_filter").removeClass("selectedFilter"); @@ -148,6 +161,97 @@ window.addEvent('load', function () { var syncMainDataLastResponseId = 0; var serverState = {}; + var removeTorrentFromLabelList = function( hash ) + { + if( hash == null || hash == "" ) return false; + + var removed = false; + Object.each( label_list, function( label ) { + if( Object.contains( label.torrents, hash ) ) { + removed = true; + label.torrents.splice( label.torrents.indexOf( hash ), 1 ); + } + }); + return removed; + }; + + var addTorrentToLabelList = function( torrent ) { + var label = torrent['label']; + if( label == null || label.length === 0 ) { + removeTorrentFromLabelList( torrent['hash'] ); + return false; + } + + var labelHash = genHash( label ); + if( label_list[labelHash] == null ) { + console.log( "addTorrentToLabelList: warning, label not found. label=", label, " label_list=", label_list ); + label_list[labelHash] = { name: label, torrents: [] }; + } + if( !Object.contains(label_list[labelHash].torrents, torrent['hash'] ) ) { + removeTorrentFromLabelList( torrent['hash'] ); + label_list[labelHash].torrents = label_list[labelHash].torrents.combine( [ torrent['hash'] ] ); + return true; + } + return false; + }; + + var updateContextMenu = function () { + var labelList = $('contextLabelList'); + labelList.empty(); + labelList.appendChild(new Element('li', {html: 'QBT_TR(New...)QBT_TR'})); + labelList.appendChild(new Element('li', {html: 'QBT_TR(Reset)QBT_TR'})); + + var first = true; + Object.each(label_list, function (label) { + var labelHash = genHash( label.name ); + var el = new Element('li', {html: '' + label.name + ''}); + if (first) { + el.removeClass(); + el.addClass('separator'); + first = false; + } + labelList.appendChild(el); + }); + }; + + var updateLabelList = function() { + var labelList = $( 'filterLabelList' ); + if( !labelList ) { + return; + } + labelList.empty(); + + var create_link = function( hash, text, count ) + { + var html = '' + + '' + + text + '(' + count + ')' + ''; + + return new Element( 'li', { id: hash, html: html } ); + }; + + var allLabels = 0; + Object.each( label_list, function( label ) { + allLabels += label.torrents.length; + }); + + var unlabelled = myTable.getRowIds().length - allLabels; + labelList.appendChild( create_link( LABELS_ALL, 'QBT_TR(All Labels)QBT_TR', allLabels ) ); + labelList.appendChild( create_link( LABELS_UNLABELLED, 'QBT_TR(Unlabeled)QBT_TR', unlabelled ) ); + + Object.each( label_list, function( label ) { + var labelHash = genHash( label.name ); + labelList.appendChild( create_link( labelHash, label.name, label.torrents.length ) ); + } ); + + var childrens = labelList.childNodes; + for (var i in childrens) { + if( childrens[i].id == selected_label ) { + childrens[i].className = "selectedFilter"; + } + } + }; + var syncMainDataTimer; var syncMainData = function () { var url = new URI('sync/maindata'); @@ -165,15 +269,26 @@ window.addEvent('load', function () { $('error_div').set('html', ''); if (response) { var full_update = (response['full_update'] == true); - if (full_update) + if (full_update) { myTable.rows.erase(); - if (response['rid']) + label_list = {}; + Object.each( response['labels'], function( label ) { + var labelHash = genHash( label ); + label_list[ labelHash ] = { name: label, torrents: [] }; + } ); + } + if (response['rid']) { syncMainDataLastResponseId = response['rid']; - if (response['torrents']) + } + if (response['torrents']) { for (var key in response['torrents']) { response['torrents'][key]['hash'] = key; - myTable.updateRowData(response['torrents'][key]); + myTable.updateRowData( response['torrents'][key] ); + addTorrentToLabelList( response['torrents'][key] ); } + updateLabelList(); + updateContextMenu(); + } if (response['torrents_removed']) response['torrents_removed'].each(function (hash) { myTable.removeRow(hash); diff --git a/src/webui/www/public/scripts/dynamicTable.js b/src/webui/www/public/scripts/dynamicTable.js index ac1062801..c49de75c7 100644 --- a/src/webui/www/public/scripts/dynamicTable.js +++ b/src/webui/www/public/scripts/dynamicTable.js @@ -60,6 +60,7 @@ var dynamicTable = new Class({ this.newColumn('upspeed', 'width: 100px; cursor: pointer', 'QBT_TR(Up Speed)QBT_TR'); this.newColumn('eta', 'width: 100px; cursor: pointer', 'QBT_TR(ETA)QBT_TR'); this.newColumn('ratio', 'width: 100px; cursor: pointer', 'QBT_TR(Ratio)QBT_TR'); + this.newColumn('label', 'width: 100px; cursor: pointer', 'QBT_TR(Label)QBT_TR'); this.columns['state_icon'].onclick = ''; this.columns['state_icon'].dataProperties[0] = 'state'; @@ -279,10 +280,13 @@ var dynamicTable = new Class({ break; } - if (labelName == null) + if (labelName == LABELS_ALL && row['full_data'].label.length > 0) return true; - if (labelName != row['full_data'].label) + if (labelName == LABELS_UNLABELLED && row['full_data'].label.length === 0) + return true; + + if (labelName != genHash( row['full_data'].label) ) return false; return true; diff --git a/src/webui/www/public/scripts/mocha-init.js b/src/webui/www/public/scripts/mocha-init.js index 1fdd5e924..9ff9c36f5 100644 --- a/src/webui/www/public/scripts/mocha-init.js +++ b/src/webui/www/public/scripts/mocha-init.js @@ -309,6 +309,60 @@ initializeWindows = function() { } }; + newLabelFN = function () { + var h = myTable.selectedIds(); + if (h.length) { + new MochaUI.Window({ + id: 'newLabelPage', + title: "QBT_TR(Torrent Label)QBT_TR", + loadMethod: 'iframe', + contentURL: 'newlabel.html?hashes=' + h.join(','), + scrollbars: false, + resizable: false, + maximizable: false, + paddingVertical: 0, + paddingHorizontal: 0, + width: 424, + height: 150 + }); + } + }; + + resetLabelFN = function () { + var h = myTable.selectedIds(); + var label_json = JSON.stringify({value: ''}); + if (h.length) { + h.each(function (hash, index) { + new Request({ + url: 'command/setLabel', + method: 'post', + data: { + hash: hash, + label_obj: label_json + } + }).send(); + }); + } + }; + + updateLabelFN = function (label_hash) { + var label = label_list[label_hash].name; + var label_json = JSON.stringify({value: label}); + var h = myTable.selectedIds(); + if (h.length) { + h.each(function (hash, index) { + new Request({ + url: 'command/setLabel', + method: 'post', + data: { + hash: hash, + label_obj: label_json + } + }).send(); + }); + } + }; + ['pauseAll', 'resumeAll'].each(function(item) { addClickEvent(item, function(e) { new Event(e).stop(); From a9e9a9c835ca834d45dbe0b7e7f99edd6012e0bf Mon Sep 17 00:00:00 2001 From: ngosang Date: Thu, 16 Jul 2015 01:04:53 +0200 Subject: [PATCH 3/3] [Web UI] Changes in labels implementation --- src/webui/webapplication.cpp | 22 +-- src/webui/www/private/index.html | 5 +- src/webui/www/public/filters.html | 30 ++-- src/webui/www/public/newlabel.html | 71 ++++----- src/webui/www/public/scripts/client.js | 147 +++++++++++-------- src/webui/www/public/scripts/dynamicTable.js | 2 +- src/webui/www/public/scripts/mocha-init.js | 50 ++----- 7 files changed, 161 insertions(+), 166 deletions(-) diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index 3000c3464..2e3a8bfe6 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -669,19 +669,19 @@ void WebApplication::action_command_recheck() void WebApplication::action_command_setLabel() { CHECK_URI(0); - CHECK_PARAMETERS("hash" << "label_obj"); + CHECK_PARAMETERS("hashes" << "label"); - QString hash = request().posts["hash"]; - QString label_obj = request().posts["label_obj"]; + QStringList hashes = request().posts["hashes"].split("|"); + QString label = request().posts["label"].trimmed(); + if (!Utils::Fs::isValidFileSystemName(label)) { + status(400, "Labels must not contain special characters"); + return; + } - const QVariantMap m = json::fromJson(label_obj).toMap(); - if( m.contains("value") ) { - QString label = m["value"].toString(); - if (!hash.isEmpty()) { - BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash); - if (torrent) - torrent->setLabel(label); - } + foreach (const QString &hash, hashes) { + BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash); + if (torrent) + torrent->setLabel(label); } } diff --git a/src/webui/www/private/index.html b/src/webui/www/private/index.html index a0ee74397..d3a320afd 100644 --- a/src/webui/www/private/index.html +++ b/src/webui/www/private/index.html @@ -106,9 +106,8 @@
  • QBT_TR(Force Resume)QBT_TR QBT_TR(Force Resume)QBT_TR
  • QBT_TR(Delete)QBT_TR QBT_TR(Delete)QBT_TR
  • - QBT_TR(Label >)QBT_TR -
      -
    + QBT_TR(Label)QBT_TR QBT_TR(Label)QBT_TR +
    • QBT_TR(Priority)QBT_TR diff --git a/src/webui/www/public/filters.html b/src/webui/www/public/filters.html index ba1b1ce7f..48cce7baf 100644 --- a/src/webui/www/public/filters.html +++ b/src/webui/www/public/filters.html @@ -1,15 +1,15 @@ - QBT_TR(Torrents)QBT_TR - -
      - QBT_TR(Labels)QBT_TR -
        -
      +QBT_TR(Torrents)QBT_TR + +
      +QBT_TR(Labels)QBT_TR +
        +
      diff --git a/src/webui/www/public/newlabel.html b/src/webui/www/public/newlabel.html index 2b6ff512e..ccf226bc2 100644 --- a/src/webui/www/public/newlabel.html +++ b/src/webui/www/public/newlabel.html @@ -7,58 +7,41 @@ -
      -

      QBT_TR(Label)QBT_TR: - -

      -
      diff --git a/src/webui/www/public/scripts/client.js b/src/webui/www/public/scripts/client.js index 72610e33d..49374be4d 100644 --- a/src/webui/www/public/scripts/client.js +++ b/src/webui/www/public/scripts/client.js @@ -95,10 +95,10 @@ window.addEvent('load', function () { resizeLimit : [100, 300] }); - setLabelFilter = function( hash ) { + setLabelFilter = function(hash) { selected_label = hash; localStorage.setItem('selected_label', selected_label); - updateLabelList(); + highlightSelectedLabel(); if (typeof myTable.table != 'undefined') updateMainData(); }; @@ -161,35 +161,33 @@ window.addEvent('load', function () { var syncMainDataLastResponseId = 0; var serverState = {}; - var removeTorrentFromLabelList = function( hash ) - { - if( hash == null || hash == "" ) return false; - + var removeTorrentFromLabelList = function(hash) { + if (hash == null || hash == "") + return false; var removed = false; - Object.each( label_list, function( label ) { - if( Object.contains( label.torrents, hash ) ) { + Object.each(label_list, function(label) { + if (Object.contains(label.torrents, hash)) { removed = true; - label.torrents.splice( label.torrents.indexOf( hash ), 1 ); + label.torrents.splice(label.torrents.indexOf(hash), 1); } }); return removed; }; - var addTorrentToLabelList = function( torrent ) { + var addTorrentToLabelList = function(torrent) { var label = torrent['label']; - if( label == null || label.length === 0 ) { - removeTorrentFromLabelList( torrent['hash'] ); + if (label == null) return false; + if (label.length === 0) { // Empty label + removeTorrentFromLabelList(torrent['hash']); + return true; } - - var labelHash = genHash( label ); - if( label_list[labelHash] == null ) { - console.log( "addTorrentToLabelList: warning, label not found. label=", label, " label_list=", label_list ); - label_list[labelHash] = { name: label, torrents: [] }; - } - if( !Object.contains(label_list[labelHash].torrents, torrent['hash'] ) ) { - removeTorrentFromLabelList( torrent['hash'] ); - label_list[labelHash].torrents = label_list[labelHash].torrents.combine( [ torrent['hash'] ] ); + var labelHash = genHash(label); + if (label_list[labelHash] == null) // This should not happen + label_list[labelHash] = {name: label, torrents: []}; + if (!Object.contains(label_list[labelHash].torrents, torrent['hash'])) { + removeTorrentFromLabelList(torrent['hash']); + label_list[labelHash].torrents = label_list[labelHash].torrents.combine([torrent['hash']]); return true; } return false; @@ -198,15 +196,20 @@ window.addEvent('load', function () { var updateContextMenu = function () { var labelList = $('contextLabelList'); labelList.empty(); - labelList.appendChild(new Element('li', {html: 'QBT_TR(New...)QBT_TR'})); - labelList.appendChild(new Element('li', {html: 'QBT_TR(Reset)QBT_TR'})); + labelList.appendChild(new Element('li', {html: 'QBT_TR(New...)QBT_TR QBT_TR(New...)QBT_TR'})); + labelList.appendChild(new Element('li', {html: 'QBT_TR(Reset)QBT_TR QBT_TR(Reset)QBT_TR'})); + + var sortedLabels = [] + Object.each(label_list, function(label) { + sortedLabels.push(label.name); + }); + sortedLabels.sort(); var first = true; - Object.each(label_list, function (label) { - var labelHash = genHash( label.name ); - var el = new Element('li', {html: '' + label.name + ''}); + Object.each(sortedLabels, function(labelName) { + var labelHash = genHash(labelName); + var el = new Element('li', {html: ' ' + labelName + ''}); if (first) { - el.removeClass(); el.addClass('separator'); first = false; } @@ -215,42 +218,54 @@ window.addEvent('load', function () { }; var updateLabelList = function() { - var labelList = $( 'filterLabelList' ); - if( !labelList ) { + var labelList = $('filterLabelList'); + if (!labelList) return; - } labelList.empty(); - var create_link = function( hash, text, count ) - { + var create_link = function(hash, text, count) { var html = '' + - '' + - text + '(' + count + ')' + ''; - - return new Element( 'li', { id: hash, html: html } ); + '' + + text + ' (' + count + ')' + ''; + return new Element('li', {id: hash, html: html}); }; - var allLabels = 0; - Object.each( label_list, function( label ) { - allLabels += label.torrents.length; + var all = myTable.getRowIds().length; + var unlabelled = 0; + Object.each(myTable.rows, function(row) { + if (row['full_data'].label.length === 0) + unlabelled += 1; + }); + labelList.appendChild(create_link(LABELS_ALL, 'QBT_TR(All)QBT_TR', all)); + labelList.appendChild(create_link(LABELS_UNLABELLED, 'QBT_TR(Unlabeled)QBT_TR', unlabelled)); + + var sortedLabels = [] + Object.each(label_list, function(label) { + sortedLabels.push(label.name); + }); + sortedLabels.sort(); + + Object.each(sortedLabels, function(labelName) { + var labelHash = genHash(labelName); + var labelCount = label_list[labelHash].torrents.length; + labelList.appendChild(create_link(labelHash, labelName, labelCount)); }); - var unlabelled = myTable.getRowIds().length - allLabels; - labelList.appendChild( create_link( LABELS_ALL, 'QBT_TR(All Labels)QBT_TR', allLabels ) ); - labelList.appendChild( create_link( LABELS_UNLABELLED, 'QBT_TR(Unlabeled)QBT_TR', unlabelled ) ); - - Object.each( label_list, function( label ) { - var labelHash = genHash( label.name ); - labelList.appendChild( create_link( labelHash, label.name, label.torrents.length ) ); - } ); + highlightSelectedLabel(); + }; + var highlightSelectedLabel = function() { + var labelList = $('filterLabelList'); + if (!labelList) + return; var childrens = labelList.childNodes; for (var i in childrens) { - if( childrens[i].id == selected_label ) { + if (childrens[i].id == selected_label) childrens[i].className = "selectedFilter"; - } + else + childrens[i].className = ""; } - }; + } var syncMainDataTimer; var syncMainData = function () { @@ -268,30 +283,42 @@ window.addEvent('load', function () { onSuccess : function (response) { $('error_div').set('html', ''); if (response) { + var update_labels = false; var full_update = (response['full_update'] == true); if (full_update) { myTable.rows.erase(); label_list = {}; - Object.each( response['labels'], function( label ) { - var labelHash = genHash( label ); - label_list[ labelHash ] = { name: label, torrents: [] }; - } ); } if (response['rid']) { syncMainDataLastResponseId = response['rid']; } + if (response['labels']) { + response['labels'].each(function(label) { + var labelHash = genHash(label); + label_list[labelHash] = {name: label, torrents: []}; + }); + update_labels = true; + } + if (response['labels_removed']) { + response['labels_removed'].each(function(label) { + var labelHash = genHash(label); + delete label_list[labelHash]; + }); + update_labels = true; + } if (response['torrents']) { for (var key in response['torrents']) { response['torrents'][key]['hash'] = key; - myTable.updateRowData( response['torrents'][key] ); - addTorrentToLabelList( response['torrents'][key] ); + myTable.updateRowData(response['torrents'][key]); + if (addTorrentToLabelList(response['torrents'][key])) + update_labels = true; } - updateLabelList(); - updateContextMenu(); } if (response['torrents_removed']) response['torrents_removed'].each(function (hash) { myTable.removeRow(hash); + removeTorrentFromLabelList(hash); + update_labels = true; // Allways to update All label }); myTable.updateTable(full_update); myTable.altRow(); @@ -301,6 +328,10 @@ window.addEvent('load', function () { serverState[key] = tmp[key]; processServerState(); } + if (update_labels) { + updateLabelList(); + updateContextMenu(); + } } clearTimeout(syncMainDataTimer); syncMainDataTimer = syncMainData.delay(syncMainDataTimerPeriod); diff --git a/src/webui/www/public/scripts/dynamicTable.js b/src/webui/www/public/scripts/dynamicTable.js index c49de75c7..8b23bf29a 100644 --- a/src/webui/www/public/scripts/dynamicTable.js +++ b/src/webui/www/public/scripts/dynamicTable.js @@ -280,7 +280,7 @@ var dynamicTable = new Class({ break; } - if (labelName == LABELS_ALL && row['full_data'].label.length > 0) + if (labelName == LABELS_ALL) return true; if (labelName == LABELS_UNLABELLED && row['full_data'].label.length === 0) diff --git a/src/webui/www/public/scripts/mocha-init.js b/src/webui/www/public/scripts/mocha-init.js index 9ff9c36f5..35af44816 100644 --- a/src/webui/www/public/scripts/mocha-init.js +++ b/src/webui/www/public/scripts/mocha-init.js @@ -314,52 +314,34 @@ initializeWindows = function() { if (h.length) { new MochaUI.Window({ id: 'newLabelPage', - title: "QBT_TR(Torrent Label)QBT_TR", + title: "QBT_TR(New Label)QBT_TR", loadMethod: 'iframe', - contentURL: 'newlabel.html?hashes=' + h.join(','), + contentURL: 'newlabel.html?hashes=' + h.join('|'), scrollbars: false, resizable: false, maximizable: false, paddingVertical: 0, paddingHorizontal: 0, - width: 424, - height: 150 + width: 250, + height: 100 }); } }; - resetLabelFN = function () { - var h = myTable.selectedIds(); - var label_json = JSON.stringify({value: ''}); - if (h.length) { - h.each(function (hash, index) { - new Request({ - url: 'command/setLabel', - method: 'post', - data: { - hash: hash, - label_obj: label_json - } - }).send(); - }); - } - }; - - updateLabelFN = function (label_hash) { - var label = label_list[label_hash].name; - var label_json = JSON.stringify({value: label}); + updateLabelFN = function (labelHash) { + var labelName = ''; + if (labelHash != 0) + var labelName = label_list[labelHash].name; var h = myTable.selectedIds(); if (h.length) { - h.each(function (hash, index) { - new Request({ - url: 'command/setLabel', - method: 'post', - data: { - hash: hash, - label_obj: label_json - } - }).send(); - }); + new Request({ + url: 'command/setLabel', + method: 'post', + data: { + hashes: h.join("|"), + label: labelName + } + }).send(); } };