From 8e8b2c44fc1345b2b4ae2806f2ef5f2187d760c0 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Tue, 27 Jun 2023 13:13:30 +0200 Subject: [PATCH] Paged history loading. --- src/redakcja/static/css/history.css | 5 + src/redakcja/static/js/wiki/view_history.js | 149 ++++++++++-------- src/redakcja/static/js/wiki/wikiapi.js | 47 +++--- .../templates/wiki/tabs/history_view.html | 47 +++--- src/wiki/views.py | 9 +- 5 files changed, 138 insertions(+), 119 deletions(-) diff --git a/src/redakcja/static/css/history.css b/src/redakcja/static/css/history.css index fd3ad030..18c606c8 100644 --- a/src/redakcja/static/css/history.css +++ b/src/redakcja/static/css/history.css @@ -6,6 +6,11 @@ #history-view { flex: 1; overflow: auto; + +} + +#history-view .message-box { + text-align: center; } /* diff --git a/src/redakcja/static/js/wiki/view_history.js b/src/redakcja/static/js/wiki/view_history.js index 8744995e..6e306391 100644 --- a/src/redakcja/static/js/wiki/view_history.js +++ b/src/redakcja/static/js/wiki/view_history.js @@ -1,10 +1,10 @@ (function($){ function HistoryPerspective(options) { - var old_callback = options.callback || function() {}; + var old_callback = options.callback || function() {}; - options.callback = function() { - var self = this; + options.callback = function() { + var self = this; if (CurrentDocument.diff) { rev_from = CurrentDocument.diff[0]; rev_to = CurrentDocument.diff[1]; @@ -20,41 +20,41 @@ }); } - // first time page is rendered - $('#make-diff-button').click(function() { - self.makeDiff(); - }); + // first time page is rendered + $('#make-diff-button').click(function() { + self.makeDiff(); + }); - $('#pubmark-changeset-button').click(function() { - self.showPubmarkForm(); - }); + $('#pubmark-changeset-button').click(function() { + self.showPubmarkForm(); + }); - $('#doc-revert-button').click(function() { - self.revertDialog(); - }); + $('#doc-revert-button').click(function() { + self.revertDialog(); + }); - $('#open-preview-button').click(function(event) { - var selected = $('#changes-list .entry.selected'); + $('#open-preview-button').click(function(event) { + var selected = $('#changes-list .entry.selected'); - if (selected.length != 1) { - window.alert("Wybierz dokładnie *jedną* wersję."); - return; - } + if (selected.length != 1) { + window.alert("Wybierz dokładnie *jedną* wersję."); + return; + } - var version = parseInt($("*[data-stub-value='version']", selected[0]).text()); - window.open($(this).attr('data-basehref') + "?revision=" + version); + var version = parseInt($("*[data-stub-value='version']", selected[0]).text()); + window.open($(this).attr('data-basehref') + "?revision=" + version); - event.preventDefault(); - }); + event.preventDefault(); + }); - $(document).on('click', '#changes-list .entry', function(){ + $(document).on('click', '#changes-list .entry', function(){ var $this = $(this); var selected_count = $("#changes-list .entry.selected").length; if ($this.hasClass('selected')) { - $this.removeClass('selected'); - selected_count -= 1; + $this.removeClass('selected'); + selected_count -= 1; } else { if (selected_count < 2) { @@ -64,18 +64,27 @@ }; $('#history-view-editor .toolbar button').attr('disabled', 'disabled'). - filter('*[data-enabled-when~="' + selected_count + '"]'). + filter('*[data-enabled-when~="' + selected_count + '"]'). attr('disabled', null); - }); + }); - $(document).on('click', '#changes-list span.tag', function(event){ - return false; - }); + $(document).on('click', '#changes-list span.tag', function(event){ + return false; + }); - old_callback.call(this); - } + $('#history-view').on('scroll', function() { + if (self.finished || self.fetching) return; + var elemTop = $('#history-view .message-box').offset().top; + var windowH = $(window).innerHeight(); + if (elemTop - 20 < windowH) { + self.triggerFetch(); + } + }); + + old_callback.call(this); + } - $.wiki.Perspective.call(this, options); + $.wiki.Perspective.call(this, options); }; HistoryPerspective.prototype = new $.wiki.Perspective(); @@ -86,61 +95,67 @@ HistoryPerspective.prototype.onEnter = function(success, failure){ $.wiki.Perspective.prototype.onEnter.call(this); + this.startFetching(); + success && success(); + }; - $.blockUI({ - message: 'Odświeżanie historii...' - }); + HistoryPerspective.prototype.startFetching = function() { + $('#history-view .message-box').html('Wczytywanie historii…').show(); + $('#changes-list').html(''); + this.finished = false; + this.befored = ''; + this.triggerFetch(); + }; + HistoryPerspective.prototype.stopFetching = function() { + self.finished = true; + $('#history-view .message-box').hide() + }; - function _finalize(s){ - $.unblockUI(); - if (s) { - if (success) - success(); - } - else { - if (failure) - failure(); - } + HistoryPerspective.prototype.triggerFetch = function() { + var self = this; + self.fetching = true; + + function _finalize() { + self.fetching = false; } function _failure(doc, message){ $('#history-view .message-box').html('Nie udało się odświeżyć historii:' + message).show(); - _finalize(false); + _finalize(); }; function _success(doc, data){ - $('#history-view .message-box').hide(); + //$('#history-view .message-box').hide(); ONLY AFTER LAST! var changes_list = $('#changes-list'); var $stub = $('#history-view .row-stub'); - changes_list.html(''); - var tags = $('select#id_addtag-tag option'); + if (!data.length) { + self.stopFetching(); + } $.each(data, function(){ $.wiki.renderStub({ - container: changes_list, - stub: $stub, - data: this, - filters: { -// tag: function(value) { -// return tags.filter("*[value='"+value+"']").text(); -// } -// description: function(value) { -// return value.replace('\n', '); -// } - } - }); + container: changes_list, + stub: $stub, + data: this, + }); + self.before = this.version; + if (this.version == 1) { + self.stopFetching(); + } }); - _finalize(true); + _finalize(); }; - return this.doc.fetchHistory({ + this.doc.fetchHistory({ success: _success, - failure: _failure + failure: _failure, + before: this.before, }); - }; + } + HistoryPerspective.prototype.showPubmarkForm = function(){ var selected = $('#changes-list .entry.selected'); diff --git a/src/redakcja/static/js/wiki/wikiapi.js b/src/redakcja/static/js/wiki/wikiapi.js index b53bf06d..c05c501e 100644 --- a/src/redakcja/static/js/wiki/wikiapi.js +++ b/src/redakcja/static/js/wiki/wikiapi.js @@ -125,33 +125,26 @@ } }); }; - /* - * Fetch history of this document. - * - * from - First revision to fetch (default = 0) upto - Last revision to - * fetch (default = tip) - * - */ - WikiDocument.prototype.fetchHistory = function(params) { - /* this doesn't modify anything, so no locks */ - params = $.extend({}, noops, params); - var self = this; - $.ajax({ - method: "GET", - url: reverse("ajax_document_history", self.id), - dataType: 'json', - data: { - "from": params['from'], - "upto": params['upto'] - }, - success: function(data) { - params['success'](self, data); - }, - error: function() { - params['failure'](self, "Nie udało się wczytać historii dokumentu."); - } - }); - }; + /* + * Fetch history of this document. + * + */ + WikiDocument.prototype.fetchHistory = function(params) { + /* this doesn't modify anything, so no locks */ + params = $.extend({}, noops, params); + var self = this; + $.ajax({ + method: "GET", + url: reverse("ajax_document_history", self.id) + "?before=" + params.before, + dataType: 'json', + success: function(data) { + params['success'](self, data); + }, + error: function() { + params['failure'](self, "Nie udało się wczytać historii dokumentu."); + } + }); + }; WikiDocument.prototype.fetchDiff = function(params) { /* this doesn't modify anything, so no locks */ var self = this; diff --git a/src/wiki/templates/wiki/tabs/history_view.html b/src/wiki/templates/wiki/tabs/history_view.html index d590f328..1384fb6b 100644 --- a/src/wiki/templates/wiki/tabs/history_view.html +++ b/src/wiki/templates/wiki/tabs/history_view.html @@ -16,28 +16,27 @@ -
- - - - - - - - - - - - -
- -
-
- -
-
-
-
-
-
+
+ + + + + + + + + + +
+ +
+
+ +
+
+
+
+
+ +
diff --git a/src/wiki/views.py b/src/wiki/views.py index 0b9cbc16..29c389a7 100644 --- a/src/wiki/views.py +++ b/src/wiki/views.py @@ -293,8 +293,15 @@ def history(request, chunk_id): if not doc.book.accessible(request): return HttpResponseForbidden("Not authorized.") + history = doc.history() + try: + before = int(request.GET.get('before')) + except: + pass + else: + history = history.filter(revision__lt=before) changes = [] - for change in doc.history().reverse(): + for change in history.reverse()[:20]: changes.append({ "version": change.revision, "description": change.description, -- 2.20.1