Allow assigning woblink authors in catalogue.
[redakcja.git] / src / redakcja / static / js / wiki / view_editor_wysiwyg.js
index 82920fd..bd67db6 100644 (file)
@@ -1,229 +1,4 @@
 (function($) {
-    class Caret {
-        constructor(view) {
-            self = this;
-            self.view = view;
-            self.singleClick = false;
-            
-            let caret = this.element = $('<span id="caret"><textarea></textarea></span>');
-
-            // When user writes into caret, add it to the document.
-            $('textarea', caret).on('input', function() {
-                let v = $(this).val();
-                $(this).val('');
-                self.insertChar(v);
-
-            });
-
-            // On click on x-node element, set caret position.
-            self.view.on('click', '*[x-node]', function(e) {
-                if (e.redakcja_caret_inserted) return;
-                e.redakcja_caret_inserted = true;
-
-                if (self.singleClick) {
-                    self.singleClick = false;
-                    return;
-                }
-                
-                self.detach();
-
-                var selection = window.getSelection();
-                if (!selection.isCollapsed) return;
-
-                self.singleClick = true;
-                setTimeout(function() {
-                    if (self.singleClick) {
-                        self.element.insertBefore(
-                            selection.anchorNode.splitText(
-                                selection.anchorOffset
-                            )
-                        )
-                        self.focus();
-                    }
-                    self.singleClick = false;
-                }, 250);
-
-            });
-
-            self.view.on('keydown', function(e) {
-                // TODO:
-                // Enter (split block)
-                // delete selection?
-
-                // cases:
-                // we are in <akap> (no going up)
-                // we are in <wyroznienie> (can go up)
-                // we are next to <wyroznienie> (can go inside)
-
-                switch (e.key) {
-                case "ArrowRight":
-                    if (e.shiftKey) {
-                        self.detach();
-                        return;
-                    }
-
-                    self.moveRight();
-                    break;
-                case "ArrowLeft":
-                    if (e.shiftKey) {
-                        self.detach();
-                        return;
-                    }
-
-                    self.moveLeft();
-                    break;
-                case "ArrowUp":
-                    if (e.shiftKey) {
-                        self.detach();
-                        return;
-                    }
-                    break;
-                case "ArrowDown":
-                    if (e.shiftKey) {
-                        self.detach();
-                        return;
-                    }
-                    break;
-                case "Backspace":
-                    self.deleteBefore();
-                    break;
-                case "Delete":
-                    self.deleteAfter();
-                    break;
-//                default:
-//                    console.log('key', e.key, e.code);
-                }
-            })
-        }
-
-        get attached() {
-            return this.element.parent().length;
-        }
-
-        detach() {
-            let p;
-            if (this.attached) {
-                p = this.element.parent()[0]
-                this.element.detach();
-                p.normalize()
-            }
-        }
-
-        focus() {
-            $("textarea", self.element).focus();
-        }
-
-        normalize() {
-            this.element.parent()[0].normalize();
-        }
-
-        insertChar(ch) {
-            $(document.createTextNode(ch)).insertBefore(this.element);
-            this.normalize();
-        }
-
-        deleteBefore() {
-            let contents = this.element.parent().contents();
-            // Find the text before caret.
-            let textBefore = contents[contents.index(this.element) - 1];
-
-            // Should be text, but what if not?
-            textBefore.textContent = textBefore.textContent.substr(0, textBefore.textContent.length - 1);
-            this.normalize();
-
-        }
-
-        deleteAfter() {
-            let contents = this.element.parent().contents();
-            // Find the text after caret.
-            let textAfter = contents[contents.index(this.element) + 1];
-            textAfter.textContent = textAfter.textContent.substr(1);
-        }
-
-        moveLeft() {
-            this.move({
-                move: -1,
-                edge: (i, l) => {return !i;},
-                enter: (l) => {return l - 1;},
-                splitTarget: (t) => {return t.splitText(t.length - 1);},
-                noSplitTarget: (t) => {return t.splitText(t.length);},
-            })
-        }
-
-        moveRight() {
-            this.move({
-                move: 1,
-                edge: (i, l) => {return i == l - 1;},
-                enter: (l) => {return 0;},
-                splitTarget: (t) => {return t.splitText(1);},
-                noSplitTarget: (t) => {return t;},
-            })
-        }
-
-        move(opts) {
-            if (!this.attached) return;
-            this.normalize();
-
-            let contents = this.element.parent().contents();
-            let index = contents.index(this.element);
-            let target, moved, oldparent;
-
-            let parent = this.element.parent()[0];
-
-            if (opts.edge(index, contents.length)) {
-                // We're at the end -- what to do?
-                // can we go up?
-
-                if (parent.nodeName == 'EM') {
-                    oldparent = parent;
-                    parent = parent.parentNode;
-                    contents = $(parent).contents();
-                    index = contents.index(oldparent);
-                }
-            }
-
-            index += opts.move;
-            target = contents[index];
-            moved = false;
-
-            while (target.nodeType == 1) {
-                // we've encountered a node.
-                // can we go inside?
-
-                if (target.nodeName == 'EM') {
-                    // enter
-                    parent = $(target);
-                    contents = parent.contents();
-                    index = opts.enter(contents.length);
-                    target = contents[index];
-
-                    // what if it has no elements?
-                } else {
-                    // skip
-                    index += opts.move; // again, what if end?
-                    target = contents[index];
-                    moved = true;
-                }
-
-                // if editable?
-                // what if editable but empty?
-
-            }
-
-            if (target.nodeType == 3) {
-                if (!moved) {
-                    target = opts.splitTarget(target);
-                } else {
-                    target = opts.noSplitTarget(target);
-                }
-
-                this.element.insertBefore(target);
-            }
-            this.normalize();
-            this.focus();
-        }
-    }
-
 
     /* Show theme to the user */
     function selectTheme(themeId){
@@ -231,8 +6,8 @@
         selection.removeAllRanges();
 
         var range = document.createRange();
-        var s = $(".motyw[theme-class='" + themeId + "']")[0];
-        var e = $(".end[theme-class='" + themeId + "']")[0];
+        var s = $("[x-node='motyw'][theme-class='" + themeId + "']")[0];
+        var e = $("[x-node='end'][theme-class='" + themeId + "']")[0];
 
         if (s && e) {
             range.setStartAfter(s);
 
     /* Verify insertion port for annotation or theme */
     function verifyTagInsertPoint(node){
-        if (node.nodeType == 3) { // Text Node
+        if (node.nodeType == Node.TEXT_NODE) {
             node = node.parentNode;
         }
 
-        if (node.nodeType != 1) {
+        if (node.nodeType != Node.ELEMENT_NODE) {
             return false;
         }
 
         node = $(node);
+        if (node.attr('id') == 'caret') {
+            node = node.parent();
+        }
+        while (node.attr('x-pass-thru')) {
+            node = node.parent();
+        }
         var xtype = node.attr('x-node');
 
         if (!xtype || (xtype.search(':') >= 0) ||
             return false;
         }
 
+        return true;
+    }
+
+    function verifyThemeBoundaryPoint(node) {
+        if (!verifyTagInsertPoint(node)) return false;
+        node = $(node);
         // don't allow themes inside annotations
         if (node.closest('[x-node="pe"]').length > 0)
             return false;
         var text = "";
 
         $(fragment.childNodes).each(function(){
-            if (this.nodeType == 3) // textNode
+            if (this.nodeType == Node.TEXT_NODE)
                 text += this.nodeValue;
             else {
-                if (this.nodeType == 1 &&
+                if (this.nodeType == Node.ELEMENT_NODE &&
                         $.inArray($(this).attr('x-node'), ANNOT_FORBIDDEN) == -1) {
                     text += html2plainText(this);
                 }
         var selection = window.getSelection();
         var n = selection.rangeCount;
 
-        if (n == 0) {
+        if (selection.isCollapsed) {
             window.alert("Nie zaznaczono żadnego obszaru");
             return false;
         }
 
-        // for now allow only 1 range
-        if (n > 1) {
-            window.alert("Zaznacz jeden obszar");
-            return false;
-        }
-
-        // remember the selected range
-        var range = selection.getRangeAt(0);
+        var range = selection.getRangeAt(n - 1);
 
         if (!verifyTagInsertPoint(range.endContainer)) {
             window.alert("Nie można wstawić w to miejsce przypisu.");
             return false;
         }
 
-        // BUG #273 - selected text can contain themes, which should be omitted from
-        // defining term
-        var text = html2plainText(range.cloneContents());
+        text = '';
+        for (let i = 0; i < n; ++ i) {
+            let rangeI = selection.getRangeAt(i);
+            if (verifyTagInsertPoint(rangeI.startContainer) &&
+                verifyTagInsertPoint(rangeI.endContainer)) {
+                text += html2plainText(rangeI.cloneContents());
+            }
+        }
         var tag = $('<span></span>');
         range.collapse(false);
         range.insertNode(tag[0]);
     }
 
 
-    function addReference(){
-        var selection = window.getSelection();
-        var n = selection.rangeCount;
-
-        if (n == 0) {
-            window.alert("Nie zaznaczono żadnego obszaru");
-            return false;
-        }
-
-        // for now allow only 1 range
-        if (n > 1) {
-            window.alert("Zaznacz jeden obszar");
-            return false;
-        }
-
-        // remember the selected range
-        var range = selection.getRangeAt(0);
-
-        if (!verifyTagInsertPoint(range.endContainer)) {
-            window.alert("Nie można wstawić w to miejsce przypisu.");
-            return false;
-        }
-
-        var tag = $('<span></span>');
-        range.collapse(false);
-        range.insertNode(tag[0]);
-
-        xml2html({
-            xml: '<ref href=""/>',
-            success: function(text){
-                var t = $(text);
-                tag.replaceWith(t);
-                openForEdit(t);
-            },
-            error: function(){
-                tag.remove();
-                alert('Błąd przy dodawaniu referncji:' + errors);
-            }
-        })
-    }
 
 
 
         // verify if the start/end points make even sense -
         // they must be inside a x-node (otherwise they will be discarded)
         // and the x-node must be a main text
-        if (!verifyTagInsertPoint(range.startContainer)) {
+        if (!verifyThemeBoundaryPoint(range.startContainer)) {
             window.alert("Motyw nie może się zaczynać w tym miejscu.");
             return false;
         }
 
-        if (!verifyTagInsertPoint(range.endContainer)) {
+        if (!verifyThemeBoundaryPoint(range.endContainer)) {
             window.alert("Motyw nie może się kończyć w tym miejscu.");
             return false;
         }
                                 spoint.insertNode(btag[0])
                                 btag.replaceWith(text);
                                 selection.removeAllRanges();
-                                openForEdit($('.motyw[theme-class="' + id + '"]'));
+                                openForEdit($('[x-node="motyw"][theme-class="' + id + '"]'));
                             }
                         });
                     }
            $('.akap-edit-button').remove();
         }
 
-        if ($origin.is('.motyw')) {
+        if ($origin.is('[x-node="motyw"]')) {
             $.themes.autocomplete($('textarea', $overlay));
         }
 
-        if ($origin.is('.motyw')){
+        if ($origin.is('[x-node="motyw"]')){
             $('.delete-button', $overlay).click(function(){
                 if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten motyw?")) {
                     $('[theme-class="' + $origin.attr('theme-class') + '"]').remove();
                 };
             });
         }
-        else if($box.is('*[x-annotation-box]') || $origin.is('*[x-edit-attribute]')) {
+        else if($box.is('*[x-annotation-box]') || $origin.is('*[x-edit-attribute]') || $origin.is('*[x-node="uwaga"]')) {
+            let q;
+            switch ($origin.attr('x-node')) {
+            case 'uwaga':
+                q = 'tę uwagę';
+                break;
+            case 'ref':
+                q = 'tę referencję';
+                break
+            default:
+                q = 'ten przypis';
+            }
             $('.delete-button', $overlay).click(function(){
-                if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten przypis?")) {
+                if (window.confirm("Czy jesteś pewien, że chcesz usunąć " + q + "?")) {
                     $origin.remove();
                     $overlay.remove();
                     $(document).unbind('click.blur-overlay');
 
         if($box.attr("x-edit-attribute")) {
             source = $('<span x-pass-thru="true"/>');
-            source.text($box.attr("data-wlf-" + $box.attr("x-edit-attribute")));
+            source.text($box.attr("x-a-wl-" + $box.attr("x-edit-attribute")));
             source = source[0];
         } else {
             source = $box[0];
                     var nodeName = $box.attr('x-node') || 'pe';
                     var insertedText = $('textarea', $overlay).val();
 
-                    if ($origin.is('.motyw')) {
+                    if ($origin.is('[x-node="motyw"]')) {
                         insertedText = insertedText.replace(/,\s*$/, '');
                     }
 
         });
     }
 
+    function createUwagaBefore(before) {
+        xml2html({
+            xml: '<uwaga/>',
+            success: function(element){
+                let $element = $(element);
+                $element.insertBefore(before);
+                openForEdit($element);
+            }
+        });
+    }
 
     function VisualPerspective(options){
-        perspective = this;
+        perspective = self = this;
 
         var old_callback = options.callback;
 
         options.callback = function(){
             var element = $("#html-view");
-            var button = $('<button class="edit-button">Edytuj</button>');
+            var button = $('<button class="edit-button active-block-button">Edytuj</button>');
+            var uwagaButton = $('<button class="uwaga-button active-block-button">Uwaga</button>');
 
             if (!CurrentDocument.readonly) {
 
                 $('#html-view').bind('mousemove', function(event){
                     var editable = $(event.target).closest('*[x-editable]');
-                    $('.active', element).not(editable).removeClass('active').children('.edit-button').remove();
+                    $('.active', element).not(editable).removeClass('active').children('.active-block-button').remove();
 
                     if (!editable.hasClass('active')) {
                         editable.addClass('active').append(button);
+                        if (!editable.is('[x-edit-attribute]') &&
+                            !editable.is('.annotation-inline-box') &&
+                            !editable.is('[x-edit-no-format]')
+                           ) {
+                            editable.append(uwagaButton);
+                        }
                     }
                     if (editable.is('.annotation-inline-box')) {
                         $('*[x-annotation-box]', editable).css({
-                            position: 'absolute',
-                            left: event.clientX - editable.offset().left + 5,
-                            top: event.clientY - editable.offset().top + 5
                         }).show();
                     }
-                    else {
-                        $('*[x-annotation-box]').hide();
-                    }
                 });
 
+                perspective.caret = new Caret(element);
+                
                 $('#insert-reference-button').click(function(){
-                    addReference();
+                    self.addReference();
                     return false;
                 });
 
                 });
 
                 $(".insert-char").click(function() {
-                    console.log('perspective', perspective);
                     addSymbol(caret=perspective.caret);
                     return false;
                 });
                     openForEdit($(this).parent());
                 });
 
+                $(document).on('click', '.uwaga-button', function(event){
+                    event.preventDefault();
+                    createUwagaBefore($(this).parent());
+                });
             }
 
-            $(document).on('click', '.motyw', function(){
+            $(document).on('click', '[x-node="motyw"]', function(){
                 selectTheme($(this).attr('theme-class'));
             });
 
+            element.on('click', '.annotation', function(event) {
+                event.preventDefault();
+                event.redakcja_caret_ignore = true;
+                $('[x-annotation-box]', $(this).parent()).toggleClass('editing');
+                perspective.caret.detach();
+            });
+
             old_callback.call(this);
         };
 
 
     VisualPerspective.prototype = new $.wiki.Perspective();
 
-    VisualPerspective.prototype.freezeState = function(){
-
-    };
-
     VisualPerspective.prototype.onEnter = function(success, failure){
         $.wiki.Perspective.prototype.onEnter.call(this);
 
                 var htmlView = $('#html-view');
                 htmlView.html(element);
 
-                perspective.caret = new Caret(htmlView);
-
-
-
-                htmlView.find('*[x-node]').dblclick(function(e) {
-                    if($(e.target).is('textarea'))
-                        return;
-                    var selection = window.getSelection();
-                    selection.collapseToStart();
-                    selection.modify('extend', 'forward', 'word');
-                    e.stopPropagation();
-                });
                 _finalize(success);
             },
             error: function(text, source){
     VisualPerspective.prototype.onExit = function(success, failure){
         var self = this;
 
+        self.caret.detach();
+
+        $.wiki.exitTab('#PropertiesPerspective');
+        
         $.blockUI({
             message: 'Zapisywanie widoku...'
         });
 
         let selection = window.getSelection();
         var n = selection.rangeCount;
-        if (n != 1) {
+        if (n != 1 || selection.isCollapsed) {
             window.alert("Nie zaznaczono obszaru");
             return false
         }
         });
     };
 
+    VisualPerspective.prototype.insertAtRange = function(range, elem) {
+        let self = this;
+        let $end = $(range.endContainer);
+        if ($end.attr('id') == 'caret') {
+            self.caret.insert(elem);
+        } else {
+            range.insertNode(elem[0]);
+        }
+    }
+
+    VisualPerspective.prototype.addReference = function() {
+        let self = this;
+        var selection = window.getSelection();
+        var n = selection.rangeCount;
+
+        // TODO: if no selection, take caret position..
+        if (n == 0) {
+            window.alert("Nie zaznaczono żadnego obszaru");
+            return false;
+        }
+
+        var range = selection.getRangeAt(n - 1);
+        if (!verifyTagInsertPoint(range.endContainer)) {
+            window.alert("Nie można wstawić w to miejsce referencji.");
+            return false;
+        }
+
+        var tag = $('<span></span>');
+
+        range.collapse(false);
+        self.insertAtRange(range, tag);
+
+        xml2html({
+            xml: '<ref href=""/>',
+            success: function(text){
+                var t = $(text);
+                tag.replaceWith(t);
+                openForEdit(t);
+            },
+            error: function(){
+                tag.remove();
+                alert('Błąd przy dodawaniu referncji:' + errors);
+            }
+        })
+    }
 
-    
     $.wiki.VisualPerspective = VisualPerspective;
 
 })(jQuery);