Ticket 547, gallery sidebar title and styling
[redakcja.git] / redakcja / static / js / wiki / xslt.js
index a5093b0..a0d0d4b 100644 (file)
@@ -19,6 +19,7 @@ function withStylesheets(code_block, onError)
        $.ajax({
                url: STATIC_URL + 'xsl/wl2html_client.xsl',
                dataType: 'xml',
+               timeout: 10000,
                success: function(data) {
                xml2htmlStylesheet = createXSLT(data);
                 $.unblockUI();
@@ -33,71 +34,72 @@ function withStylesheets(code_block, onError)
        }
 }
 
-var canonThemes = null;
 
 // Wykonuje block z załadowanymi kanonicznymi motywami
 function withThemes(code_block, onError)
 {
-    if (!canonThemes) {
-        $.blockUI({message: 'Ładowanie motywów...'});
+    if (typeof withThemes.canon == 'undefined') {
         $.ajax({
             url: '/themes',
             dataType: 'text',
             success: function(data) {
-                canonThemes = {};
-                themes = data.split('\n');
-                for (i in themes) {
-                    canonThemes[themes[i]] = 1;
-                }
-                $.unblockUI();
-                code_block();
+                withThemes.canon = data.split('\n');
+                code_block(withThemes.canon);
             },
-            error: onError
+            error: function() {
+                withThemes.canon = null;
+                code_block(withThemes.canon);
+            }
         })
     }
     else {
-        code_block();
+        code_block(withThemes.canon);
     }
 }
 
 
-
 function xml2html(options) {
     withStylesheets(function() {
-        withThemes(function() {
-            var xml = options.xml.replace(/\/\s+/g, '<br />');
-            var parser = new DOMParser();
-            var serializer = new XMLSerializer();
-            var doc = parser.parseFromString(xml, 'text/xml');
-            var error = $('parsererror', doc);
-
-            if (error.length == 0) {
-                doc = xml2htmlStylesheet.transformToFragment(doc, document);
-                console.log(doc.firstChild);
-
-                if(doc.firstChild === null) {
-                    options.error("Błąd w przetwarzaniu XML.");
-                    return;
-                }
-
-                error = $('parsererror', doc);
+        var xml = options.xml.replace(/\/(\s+)/g, '<br />$1');
+        var parser = new DOMParser();
+        var serializer = new XMLSerializer();
+        var doc = parser.parseFromString(xml, 'text/xml');
+        var error = $('parsererror', doc);
+
+        if (error.length == 0) {
+            doc = xml2htmlStylesheet.transformToFragment(doc, document);
+            console.log(doc.firstChild);
+
+        if(doc.firstChild === null) {
+            options.error("Błąd w przetwarzaniu XML.");
+                return;
             }
 
-            if (error.length > 0 && options.error) {
-                options.error(error.text());
-            } else {
-                $('.theme-text-list', doc.firstChild).each(function(){
-                    var themes = $(this).html().split(',');
-                    for (i in themes) {
-                        themes[i] = $.trim(themes[i]);
-                        if (!(themes[i] in canonThemes))
-                            themes[i] = '<span x-pass-thru="true" class="noncanon">' + themes[i] + "</span>"
-                    }
-                    $(this).html(themes.join(', '));
-                });
-                options.success(doc.firstChild);
-            }
-        }, function() { options.error && options.error('Nie udało się załadować motywów'); });
+            error = $('parsererror', doc);
+        }
+
+        if (error.length > 0 && options.error) {
+            source = $('sourcetext', doc);
+            source_text = source.text();
+            source.text('');
+            options.error(error.text(), source_text);
+        } else {
+            options.success(doc.firstChild);
+
+            withThemes(function(canonThemes) {
+                if (canonThemes != null) {
+                    $('.theme-text-list').addClass('canon').each(function(){
+                        var themes = $(this).html().split(',');
+                        for (i in themes) {
+                            themes[i] = $.trim(themes[i]);
+                            if (canonThemes.indexOf(themes[i]) == -1)
+                                themes[i] = '<span x-pass-thru="true" class="noncanon">' + themes[i] + "</span>"
+                        }
+                        $(this).html(themes.join(', '));
+                    });
+                }
+            });
+        }
     }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
 }
 
@@ -271,7 +273,7 @@ HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
                                if(xnode === 'wers') {
                                        /* push children */
                                        if(self._verseBefore(token.node))
-                                               self.result += '/\n';
+                                               self.result += '/';
                                        self._pushChildren(token.node);
                                        break;
                                };