Fix for covers generation.
[redakcja.git] / src / redakcja / static / js / wiki / view_history.js
index 8744995..9ce518c 100644 (file)
 (function($){
 
-    function HistoryPerspective(options) {
-               var old_callback = options.callback || function() {};
-
-               options.callback = function() {
-                       var self = this;
-            if (CurrentDocument.diff) {
-                rev_from = CurrentDocument.diff[0];
-                rev_to = CurrentDocument.diff[1];
-                this.doc.fetchDiff({
-                    from: rev_from,
-                    to: rev_to,
-                    success: function(doc, data){
-                        var result = $.wiki.newTab(doc, ''+rev_from +' -> ' + rev_to, 'DiffPerspective');
-
-                        $(result.view).html(data);
-                        $.wiki.switchToTab(result.tab);
-                    }
-                });
-            }
-
-                       // first time page is rendered
-               $('#make-diff-button').click(function() {
-                               self.makeDiff();
-                       });
-
-                       $('#pubmark-changeset-button').click(function() {
-                               self.showPubmarkForm();
-                       });
+    class HistoryPerspective extends $.wiki.Perspective {
+        constructor(options) {
+            super(options);
+            var self = this;
+
+            // first time page is rendered
+            $('#make-diff-button').click(function() {
+                self.makeDiff();
+            });
 
-               $('#doc-revert-button').click(function() {
-                   self.revertDialog();
-               });
+            $('#pubmark-changeset-button').click(function() {
+                self.showPubmarkForm();
+            });
 
-                       $('#open-preview-button').click(function(event) {
-                               var selected = $('#changes-list .entry.selected');
+            $('#doc-revert-button').click(function() {
+                self.revertDialog();
+            });
 
-                               if (selected.length != 1) {
-                           window.alert("Wybierz dokładnie *jedną* wersję.");
-                       return;
-                       }
+            $('#open-preview-button').click(function(event) {
+                var selected = $('#changes-list .entry.selected');
 
-                               var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
-                               window.open($(this).attr('data-basehref') + "?revision=" + version);
+                if (selected.length != 1) {
+                    window.alert("Wybierz dokładnie *jedną* wersję.");
+                    return;
+                }
 
-                               event.preventDefault();
-                       });
+                var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
+                window.open($(this).attr('data-basehref') + "?revision=" + version);
 
-                    $(document).on('click', '#changes-list .entry', function(){
-               var $this = $(this);
+                event.preventDefault();
+            });
 
-               var selected_count = $("#changes-list .entry.selected").length;
+            $(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;
+                }
+                else {
+                    if (selected_count  < 2) {
+                        $this.addClass('selected');
+                        selected_count += 1;
+                    };
+                };
+
+                $('#history-view-editor .toolbar button').attr('disabled', 'disabled').
+                    filter('*[data-enabled-when~="' + selected_count + '"]').
+                    attr('disabled', null);
+            });
 
-               if ($this.hasClass('selected')) {
-                       $this.removeClass('selected');
-                       selected_count -= 1;
-               }
-               else {
-                   if (selected_count  < 2) {
-                       $this.addClass('selected');
-                       selected_count += 1;
-                   };
-               };
+            $(document).on('click', '#changes-list .entry .approved', function(){
+                $("#changes-list .entry.selected").removeClass('selected');
+                $(this).closest('.entry').click();
+                self.showPubmarkForm();
+                return false;
+            })
+            $(document).on('click', '#changes-list span.tag', function(event){
+                return false;
+            });
 
-               $('#history-view-editor .toolbar button').attr('disabled', 'disabled').
-                            filter('*[data-enabled-when~="' + selected_count + '"]').
-                   attr('disabled', null);
-               });
+            $('#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();
+                }
+            });
+        }
 
-                    $(document).on('click', '#changes-list span.tag', function(event){
-                   return false;
-               });
+        onEnter(success, failure) {
+            super.onEnter();
+            this.startFetching();
+            success && success();
+        }
 
-               old_callback.call(this);
-               }
+        startFetching() {
+            $('#history-view .message-box').html('Wczytywanie historii…').show();
+            $('#changes-list').html('');
+            this.finished = false;
+            this.before = '';
+            this.triggerFetch();
+        }
+        stopFetching() {
+            self.finished = true;
+            $('#history-view .message-box').hide()
+        }
 
-               $.wiki.Perspective.call(this, options);
-    };
+        triggerFetch() {
+            var self = this;
+            self.fetching = true;
 
-    HistoryPerspective.prototype = new $.wiki.Perspective();
+            function _finalize() {
+                self.fetching = false;
+            }
 
-    HistoryPerspective.prototype.freezeState = function(){
-        // must
-    };
+            function _failure(doc, message){
+                $('#history-view .message-box').html('Nie udało się odświeżyć historii:' + message).show();
+                _finalize();
+            };
+
+            function _success(doc, data){
+                //$('#history-view .message-box').hide(); ONLY AFTER LAST!
+                var changes_list = $('#changes-list');
+                var $stub = $('#history-view .row-stub');
+
+                if (!data.length) {
+                    self.stopFetching();
+                }
+
+                $.each(data, function(){
+                    $.wiki.renderStub({
+                        container: changes_list,
+                        stub: $stub,
+                        data: this,
+                    });
+                    self.before = this.version;
+                    if (this.version == 1) {
+                        self.stopFetching();
+                    }
+                });
 
-    HistoryPerspective.prototype.onEnter = function(success, failure){
-        $.wiki.Perspective.prototype.onEnter.call(this);
+                _finalize();
+            };
 
-        $.blockUI({
-            message: 'Odświeżanie historii...'
-        });
+            this.doc.fetchHistory({
+                success: _success,
+                failure: _failure,
+                before: this.before,
+            });
+        }
 
-        function _finalize(s){
-            $.unblockUI();
+        showPubmarkForm() {
+            var selected = $('#changes-list .entry.selected');
 
-            if (s) {
-                if (success)
-                    success();
-            }
-            else {
-                if (failure)
-                    failure();
+            if (selected.length != 1) {
+                window.alert("Musisz zaznaczyć dokładnie jedną wersję.");
+                return;
             }
-        }
 
-        function _failure(doc, message){
-            $('#history-view .message-box').html('Nie udało się odświeżyć historii:' + message).show();
-            _finalize(false);
-        };
-
-        function _success(doc, data){
-            $('#history-view .message-box').hide();
-            var changes_list = $('#changes-list');
-            var $stub = $('#history-view .row-stub');
-            changes_list.html('');
-
-                       var tags = $('select#id_addtag-tag option');
-
-            $.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', ');
-//                                             }
-                                       }
-                               });
-            });
+            var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
+            var approved = selected.attr('data-approved') == 'true';
+            $.wiki.showDialog('#pubmark_dialog', {'revision': version, 'approved': !approved});
+        }
 
-            _finalize(true);
-        };
+        makeDiff() {
+            var changelist = $('#changes-list');
+            var selected = $('.entry.selected', changelist);
 
-        return this.doc.fetchHistory({
-            success: _success,
-            failure: _failure
-        });
-    };
+            if (selected.length != 2) {
+                window.alert("Musisz zaznaczyć dokładnie dwie wersje do porównania.");
+                return;
+            }
 
-       HistoryPerspective.prototype.showPubmarkForm = function(){
-               var selected = $('#changes-list .entry.selected');
+            var rev_from = $("*[data-stub-value='version']", selected[1]).text();
+            var rev_to =  $("*[data-stub-value='version']", selected[0]).text();
 
-               if (selected.length != 1) {
-            window.alert("Musisz zaznaczyć dokładnie jedną wersję.");
-            return;
+            $.wiki.DiffPerspective.open(rev_from, rev_to);
         }
 
-               var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
-               $.wiki.showDialog('#pubmark_dialog', {'revision': version});
-       };
-
-       HistoryPerspective.prototype.makeDiff = function() {
-        var changelist = $('#changes-list');
-        var selected = $('.entry.selected', changelist);
-
-        if (selected.length != 2) {
-            window.alert("Musisz zaznaczyć dokładnie dwie wersje do porównania.");
-            return;
-        }
+        revertDialog() {
+            var self = this;
+            var selected = $('#changes-list .entry.selected');
 
-        $.blockUI({
-            message: 'Wczytywanie porównania...'
-        });
-
-               var rev_from = $("*[data-stub-value='version']", selected[1]).text();
-               var rev_to =  $("*[data-stub-value='version']", selected[0]).text();
-
-        return this.doc.fetchDiff({
-            from: rev_from,
-                       to: rev_to,
-            success: function(doc, data){
-                var result = $.wiki.newTab(doc, ''+rev_from +' -> ' + rev_to, 'DiffPerspective');
-
-                               $(result.view).html(data);
-                               $.wiki.switchToTab(result.tab);
-                               $.unblockUI();
-            },
-            failure: function(doc){
-                $.unblockUI();
+            if (selected.length != 1) {
+                window.alert("Musisz zaznaczyć dokładnie jedną wersję.");
+                return;
             }
-        });
-    };
-
-    HistoryPerspective.prototype.revertDialog = function(){
-        var self = this;
-        var selected = $('#changes-list .entry.selected');
 
-        if (selected.length != 1) {
-            window.alert("Musisz zaznaczyć dokładnie jedną wersję.");
-            return;
+            var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
+            $.wiki.showDialog('#revert_dialog', {revision: version});
         }
-
-        var version = parseInt($("*[data-stub-value='version']", selected[0]).text());
-        $.wiki.showDialog('#revert_dialog', {revision: version});
-    };
-
+    }
     $.wiki.HistoryPerspective = HistoryPerspective;
 
 })(jQuery);