1 // Serializuje XML, wstawiając odpowiednie ilości białych znaków między elementami
2 function serialize(element) {
3 if (element.nodeType == 3) { // tekst
4 return [$.trim(element.nodeValue)];
5 } else if (element.nodeType != 1) { // pomijamy węzły nie będące elementami XML ani tekstem
10 var hasContent = false;
13 result.push(element.tagName);
15 // Mozilla nie uważa deklaracji namespace za atrybuty
16 var ns = element.tagName.indexOf(':');
17 if (ns != -1 && $.browser.mozilla) {
18 result.push(' xmlns:');
19 result.push(element.tagName.substring(0, ns));
21 result.push(element.namespaceURI);
25 if (element.attributes) {
26 for (var i=0; i < element.attributes.length; i++) {
27 var attr = element.attributes[i];
29 result.push(attr.name);
31 result.push(attr.value);
37 if (element.childNodes.length == 0) {
42 for (var i=0; i < element.childNodes.length; i++) {
43 result = result.concat(serialize(element.childNodes[i]));
47 result.push(element.tagName);
51 if (element.tagName == 'akap' || element.tagName == 'akap_dialog' || element.tagName == 'akap_cd' || element.tagName == 'strofa') {
52 result.push('\n\n\n');
53 } else if (element.tagName == 'naglowek_rozdzial') {
55 } else if (element.tagName == 'rdf:RDF') {
56 result.push('\n\n\n\n\n');
57 } else if (element.tagName.indexOf('dc:') != -1) {
66 function highlight(colour) {
68 if (window.getSelection) {
70 sel = window.getSelection();
72 range = sel.getRangeAt(0);
74 document.designMode = "on";
76 sel.removeAllRanges();
79 // Use HiliteColor since some browsers apply BackColor to the whole block
80 if ( !document.execCommand("HiliteColor", false, colour) ) {
81 document.execCommand("BackColor", false, colour);
83 document.designMode = "off";
84 } else if (document.selection && document.selection.createRange) {
86 range = document.selection.createRange();
87 range.execCommand("BackColor", false, colour);
91 // function unselectThemes(themeId) {
92 // $('.Apple-style-span').each(function() {
93 // $(this).after($(this).html());
98 function gallery(element) {
99 var element = $(element);
100 var imageDimensions = {};
102 function changePage(pageNumber) {
103 $('img', element).attr('src', element.data('images')[pageNumber - 1]);
106 function normalizeNumber(pageNumber) {
107 // Numer strony musi być pomiędzy 1 a najwyższym numerem
108 var pageCount = element.data('images').length;
109 pageNumber = parseInt(pageNumber, 10);
111 if (!pageNumber || pageNumber == NaN || pageNumber == Infinity || pageNumber == -Infinity) {
113 } else if (pageNumber < 1) {
115 } else if (pageNumber > pageCount) {
123 url: '/gallery/sample',
127 success: function(data) {
128 element.data('images', data);
129 var pn = $('.page-number', element);
130 pn.change(function(event) {
131 console.log('change!', $(this).val());
132 event.preventDefault();
133 var n = normalizeNumber(pn.val());
137 $('.previous-page', element).click(function() {
138 pn.val(normalizeNumber(pn.val()) - 1);
141 $('.next-page', element).click(function() {
142 pn.val(normalizeNumber(pn.val()) + 1);
147 var image = $('img', element).attr('unselectable', 'on');
149 var imageOrigin = {};
152 $('.zoom-in', element).click(function() {
153 zoomFactor = Math.min(2, zoomFactor + 0.2);
156 $('.zoom-out', element).click(function() {
157 zoomFactor = Math.max(0.2, zoomFactor - 0.2);
161 $('img', element).load(function() {
162 image.css({width: null, height: null});
164 width: $(this).width() * zoomFactor,
165 height: $(this).height() * zoomFactor,
166 originWidth: $(this).width(),
167 originHeight: $(this).height(),
168 galleryWidth: $(this).parent().width(),
169 galleryHeight: $(this).parent().height()
171 console.log('load', imageDimensions)
172 var position = normalizePosition(
173 image.position().left,
174 image.position().top,
175 imageDimensions.galleryWidth,
176 imageDimensions.galleryHeight,
177 imageDimensions.width,
178 imageDimensions.height
180 image.css({left: position.x, top: position.y, width: $(this).width() * zoomFactor, height: $(this).height() * zoomFactor});
183 $(window).resize(function() {
184 imageDimensions.galleryWidth = image.parent().width();
185 imageDimensions.galleryHeight = image.parent().height();
188 function bounds(galleryWidth, galleryHeight, imageWidth, imageHeight) {
192 minX: galleryWidth - imageWidth,
193 minY: galleryHeight - imageHeight
197 function normalizePosition(x, y, galleryWidth, galleryHeight, imageWidth, imageHeight) {
198 var b = bounds(galleryWidth, galleryHeight, imageWidth, imageHeight);
200 x: Math.min(b.maxX, Math.max(b.minX, x)),
201 y: Math.min(b.maxY, Math.max(b.minY, y))
205 function onMouseMove(event) {
206 var position = normalizePosition(
207 event.clientX - origin.x + imageOrigin.left,
208 event.clientY - origin.y + imageOrigin.top,
209 imageDimensions.galleryWidth,
210 imageDimensions.galleryHeight,
211 imageDimensions.width,
212 imageDimensions.height
214 image.css({position: 'absolute', top: position.y, left: position.x});
218 function setZoom(factor) {
223 imageDimensions.width = imageDimensions.originWidth * zoomFactor;
224 imageDimensions.height = imageDimensions.originHeight * zoomFactor;
225 var position = normalizePosition(
226 image.position().left,
227 image.position().top,
228 imageDimensions.galleryWidth,
229 imageDimensions.galleryHeight,
230 imageDimensions.width,
231 imageDimensions.height
233 console.log(image.position(), imageDimensions, position);
234 image.css({width: imageDimensions.width, height: imageDimensions.height,
235 left: position.x, top: position.y});
239 window.setZoom = setZoom;
241 function onMouseUp(event) {
243 .unbind('mousemove.gallery')
244 .unbind('mouseup.gallery');
248 image.bind('mousedown', function(event) {
253 imageOrigin = image.position();
255 .bind('mousemove.gallery', onMouseMove)
256 .bind('mouseup.gallery', onMouseUp);
264 function html(element) {
265 var element = $(element);
267 function selectTheme(themeId)
269 var selection = window.getSelection();
271 // remove current selection
272 selection.removeAllRanges();
274 var range = document.createRange();
275 var s = $(".motyw[theme-class='"+themeId+"']")[0];
276 var e = $(".end[theme-class='"+themeId+"']")[0];
277 // console.log('Selecting range:', themeId, range, s, e);
280 range.setStartAfter(s);
281 range.setEndBefore(e);
282 selection.addRange(range);
283 // highlight('yellow');
284 // selection.removeAllRanges();
288 var button = $('<button class="edit-button">Edytuj</button>');
289 $(element).bind('mousemove', function(event) {
290 var editable = $(event.target).closest('*[x-editable]');
291 $('.active[x-editable]', element).not(editable).removeClass('active').children('.edit-button').remove();
292 if (!editable.hasClass('active')) {
293 editable.addClass('active').append(button);
297 $('.motyw').live('click', function() {
298 selectTheme($(this).attr('theme-class'));
307 CodeMirror.fromTextArea('id_text', {
308 parserfile: 'parsexml.js',
309 path: "/static/js/lib/codemirror/",
310 stylesheet: "/static/css/xmlcolors.css",
312 useHTMLKludges: false
314 iframeClass: 'xml-iframe',
318 initCallback: function(editor) {
320 function createXSLT(xsl) {
321 var p = new XSLTProcessor();
322 p.importStylesheet(xsl);
326 function transform() {
327 $('#simple-editor').block({message: 'Ładowanie...'});
328 setTimeout(function() {
330 url: '/static/xsl/wl2html_client.xsl',
332 success: function(data) {
334 var parser = new DOMParser();
335 var serializer = new XMLSerializer();
336 var htmlXSL = createXSLT(data);
338 doc = editor.getCode().replace(/\/\s+/g, '<br />');
339 doc = parser.parseFromString(doc, 'text/xml');
340 console.log('xml', doc);
341 doc = htmlXSL.transformToFragment(doc, document);
342 console.log('after transform', doc);
343 $('#html-view').html(doc.firstChild);
344 $('#simple-editor').unblock();
346 error: function() {alert('Error loading XSL!')}
351 function reverseTransform () {
352 $('#source-editor').block({message: 'Ładowanie...'});
353 setTimeout(function() {
355 url: '/static/xsl/html2wl_client.xsl',
357 success: function(data) {
359 var parser = new DOMParser();
360 var serializer = new XMLSerializer();
361 var xsl = createXSLT(data);
363 doc = serializer.serializeToString($('#html-view div').get(0))
364 doc = parser.parseFromString(doc, 'text/xml');
365 console.log('xml',doc, doc.documentElement);
366 // TODO: Sprawdzenie błędów
367 doc = xsl.transformToDocument(doc);
368 console.log('after transform', doc, doc.documentElement);
369 doc = serialize(doc.documentElement).join('');
370 // doc = serializer.serializeToString(doc.documentElement)
372 $('#source-editor').unblock();
374 error: function() {alert('Error loading XSL!')}
379 $('#save-button').click(function(event) {
380 event.preventDefault();
381 $.blockUI({message: $('#save-dialog')});
383 // console.log(editor.getCode(), $('form input[name=text]').get(0));
384 // $('form textarea[name=text]').val(editor.getCode());
385 // $('form').ajaxSubmit(function() {
386 // alert('Udało się!');
390 $('#save-ok').click(function() {
391 $.blockUI({message: 'Zapisywanie...'});
394 name: $('#document-name').html(),
395 text: editor.getCode(),
396 revision: $('#document-revision').html(),
397 author: 'annonymous',
398 comment: $('#komentarz').val()
404 url: document.location.href,
408 success: function() {
411 error: function(xhr, textStatus, errorThrown) {
412 alert('error: ' + textStatus + ' ' + errorThrown);
417 $('#simple-view-tab').click(function() {
418 if ($(this).hasClass('active')) {
421 $(this).addClass('active');
422 $('#source-view-tab').removeClass('active');
423 $('#source-editor').hide();
424 $('#simple-editor').show();
428 $('#source-view-tab').click(function() {
429 if ($(this).hasClass('active')) {
432 $(this).addClass('active');
433 $('#simple-view-tab').removeClass('active');
434 $('#simple-editor').hide();
435 $('#source-editor').show();
439 $('.toolbar button').click(function(event) {
440 event.preventDefault();
441 var params = eval("(" + $(this).attr('ui:action-params') + ")");
442 scriptletCenter.scriptlets[$(this).attr('ui:action')](editor, params);
445 $('.toolbar select').change(function() {
446 var slug = $(this).val();
448 $('.toolbar-buttons-container').hide().filter('[data-group=' + slug + ']').show();
452 $('.toolbar-buttons-container').hide();
453 $('.toolbar select').change();
455 $('#simple-view-tab').click();
459 $(window).resize(function() {
460 $('iframe').height($(window).height() - $('#tabs').outerHeight() - $('#source-editor .toolbar').outerHeight());
465 $('.vsplitbar').click(function() {
466 if ($('#sidebar').width() == 0) {
467 $('#sidebar').width(480).css({right: 0}).show();
468 $('#source-editor, #simple-editor').css({right: 495});
469 $('.vsplitbar').css({right: 480}).addClass('active');
470 // $('#splitter').trigger('resize', [$(window).width() - 480]);
472 $('#sidebar').width(0).hide();
473 $('#source-editor, #simple-editor').css({right: 15});
474 $('.vsplitbar').css({right: 0}).removeClass('active');
475 // $('#splitter').trigger('resize', [$(window).width()]);