Added auto-removal of commas in theme edit box, if there was only 1 theme netered...
[redakcja.git] / platforma / static / js / main.js
index 2c20b9c..26c935e 100644 (file)
@@ -94,7 +94,7 @@ function gallery(element, url) {
     element.data('images', []);
     
     function changePage(pageNumber) {        
-        $('img', element).attr('src', element.data('images')[pageNumber - 1]);
+        $('.gallery-image img', element).attr('src', element.data('images')[pageNumber - 1]);
     }
     
     function normalizeNumber(pageNumber) {
@@ -130,7 +130,7 @@ function gallery(element, url) {
     });
     
     
-    var image = $('img', element).attr('unselectable', 'on');
+    var image = $('.gallery-image img', element).attr('unselectable', 'on');
     var origin = {};
     var imageOrigin = {};
     var zoomFactor = 1;
@@ -145,7 +145,7 @@ function gallery(element, url) {
     });
     $('.change-gallery', element).click(function() {
         $('.chosen-gallery').val($('#document-meta .gallery').html() || '/platforma/gallery/');
-        $('.gallery-image').animate({top: 53}, 200);
+        $('.gallery-image').animate({top: 60}, 200);
         $('.chosen-gallery').focus();
     });
     $('.change-gallery-ok', element).click(function() {
@@ -154,13 +154,13 @@ function gallery(element, url) {
         }
         $('#document-meta .gallery').html($('.chosen-gallery').val());
         updateGallery($('.chosen-gallery').val());
-        $('.gallery-image').animate({top: 27}, 200);
+        $('.gallery-image').animate({top: 30}, 200);
     });
     $('.change-gallery-cancel', element).click(function() {
-        $('.gallery-image').animate({top: 27}, 200);
+        $('.gallery-image').animate({top: 30}, 200);
     });
     
-    $('img', element).load(function() {
+    $('.gallery-image img', element).load(function() {
         image.css({width: null, height: null});
         imageDimensions = {
             width: $(this).width() * zoomFactor,
@@ -172,7 +172,7 @@ function gallery(element, url) {
         };
         
         if (!(imageDimensions.width && imageDimensions.height)) {
-            setTimeout(function() { $('img', element).load(); }, 100);
+            setTimeout(function() { $('.gallery-image img', element).load(); }, 100);
         }        
         var position = normalizePosition(
             image.position().left,
@@ -269,14 +269,14 @@ function gallery(element, url) {
                 element.data('images', data);
                 pn.val(1);
                 pn.change();
-                $('img', element).show();
+                $('.gallery-image img', element).show();
             },
             
             error: function(data) {
                 element.data('images', []);
                 pn.val(1);
                 pn.change();
-                $('img', element).hide();
+                $('.gallery-image img', element).hide();
             }
         });
     }
@@ -287,41 +287,65 @@ function gallery(element, url) {
 }
 
 
-function transform(editor) {
-    $.blockUI({message: 'Ładowanie...'});
+function transform(editor, callback) {
+    if (!callback) {
+        $.blockUI({message: 'Ładowanie...'});
+    }
     setTimeout(function() {
         xml2html({
             xml: editor.getCode(),
             success: function(element) {
                 $('#html-view').html(element);
                 $.unblockUI();
+                if (callback) {
+                    callback();
+                }
             }, error: function(text) {
-                $('#html-view').html('<p class="error">Wystąpił błąd:</p><pre>' + text + '</pre>');
+                               var message = $('<pre></pre>');
+                               message.text(text);
+                $('#html-view').html('<p class="error">Wystąpił błąd:</p><pre>' + 
+                                   message.html() + '</pre>');
+                                       
                 $.unblockUI();
+                if (callback) {
+                    callback();
+                }
             }
         });
     }, 200);
 };
 
 
-function reverseTransform(editor, cont) {
+function reverseTransform(editor, cont, errorCont, dontBlock) {
     var serializer = new XMLSerializer();
     if ($('#html-view .error').length > 0) {
+        if (errorCont) {
+            errorCont();
+        }
         return;
     }
-    $.blockUI({message: 'Ładowanie...'});
+    if (!dontBlock) {
+        $.blockUI({message: 'Ładowanie...'});
+    }
     setTimeout(function() {
         html2xml({
             xml: serializer.serializeToString($('#html-view div').get(0)),
             success: function(text) {
                 editor.setCode(text);
-                $.unblockUI();
+                if (!dontBlock) {
+                    $.unblockUI();
+                }
                 if (cont) {
                     cont();
                 }
             }, error: function(text) {
                 $('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
-                $.unblockUI();
+                if (!dontBlock) {
+                    $.unblockUI();
+                }
+                if (errorCont) {
+                    errorCont();
+                }
             }
         });
     }, 200);
@@ -373,6 +397,23 @@ function html(element) {
 
         return true;
     }
+       
+       var ANNOT_ALLOWED = ['wyroznienie'];
+       
+       function html2plainText(fragment) {
+               var text = "";
+               
+               $(fragment.childNodes).each(function() {
+                       if(this.nodeType == 3) // textNode
+                           text += this.nodeValue;
+                       else if (this.nodeType == 1 
+                           && $.inArray($(this).attr('x-node'), ANNOT_ALLOWED) != -1 ){
+                               text += html2plainText(this);                           
+                       }                       
+               });
+               
+               return text;            
+       }
     
     function addAnnotation()
     {
@@ -398,7 +439,10 @@ function html(element) {
             return false;
         }
 
-        var text = range.toString();
+               // BUG #273 - selected text can contain themes, which should be omited from
+               // defining term
+        var text = html2plainText( range.cloneContents() ); 
+               
         var tag = $('<span></span>');
         range.collapse(false);
         range.insertNode(tag[0]);
@@ -471,7 +515,7 @@ function html(element) {
                 etag.replaceWith(text);
                 xml2html({
                     xml: '<motyw id="m'+id+'"></motyw>',
-                    success: function(text) {
+                    success: function(text) {                                          
                         mtag = $('<span></span>');
                         spoint.insertNode(mtag[0]);
                         mtag.replaceWith(text);
@@ -555,8 +599,14 @@ function html(element) {
                 
                 function save(argument) {
                     var nodeName = $box.attr('x-node') || 'pe';
+                                       var insertedText = $('textarea', $overlay).val();
+                                       
+                                       if ($origin.is('.motyw')) {
+                                               insertedText = insertedText.replace(/,\s*$/, '');
+                                       }
+                                       
                     xml2html({
-                        xml: '<' + nodeName + '>' + $('textarea', $overlay).val() + '</' + nodeName + '>',
+                        xml: '<' + nodeName + '>' + insertedText + '</' + nodeName + '>',
                         success: function(element) {
                             $box.html($(element).html());
                             $overlay.remove();
@@ -654,7 +704,7 @@ $(function() {
                         name: $('#document-name').html(),
                         text: metaComment + editor.getCode(),
                         revision: $('#document-revision').html(),
-                        author: 'annonymous',
+                        author: $('#username').html() || 'annonymous',
                         comment: $('#komentarz').val()
                     };
 
@@ -678,7 +728,7 @@ $(function() {
                     })
                 }
                 
-                if ('#simple-view-tab.active') {
+                if ($('#simple-view-tab').hasClass('active')) {
                     reverseTransform(editor, doSave);
                 } else {
                     doSave();
@@ -688,19 +738,20 @@ $(function() {
             $('#save-cancel').click(function() {
                 $.unblockUI();
             });
-
-            $('#simple-view-tab').click(function() {
-                if ($(this).hasClass('active')) {
+            
+            function changeTab(callback) {
+                if ($('#simple-view-tab').hasClass('active')) {
                     return;
                 }
-                $(this).addClass('active');
+                $('#simple-view-tab').addClass('active');
                 $('#source-view-tab').removeClass('active');
                 $('#source-editor').hide();
                 $('#simple-editor').show();
-                transform(editor);
-            });
-
-            $('#source-view-tab').click(function() {
+                transform(editor, callback);
+            }        
+            $('#simple-view-tab').click(function() { changeTab(); });
+            
+            $('#source-view-tab').click(function() { 
                 if ($(this).hasClass('active')) {
                     return;
                 }
@@ -708,7 +759,7 @@ $(function() {
                 $('#simple-view-tab').removeClass('active');
                 $('#simple-editor').hide();
                 $('#source-editor').show();
-                reverseTransform(editor);
+                reverseTransform(editor);    
             });
 
             $('#source-editor .toolbar button').click(function(event) {
@@ -727,7 +778,7 @@ $(function() {
             $('.toolbar-buttons-container').hide();
             $('.toolbar select').change();
 
-            $('#simple-view-tab').click();
+            changeTab(function() { $('#loading-overlay').fadeOut() }, function() { $('#loading-overlay').fadeOut() }, true)
         }
     });