2 function highlight(colour) {
4 if (window.getSelection) {
6 sel = window.getSelection();
8 range = sel.getRangeAt(0);
10 document.designMode = "on";
12 sel.removeAllRanges();
15 // Use HiliteColor since some browsers apply BackColor to the whole block
16 if ( !document.execCommand("HiliteColor", false, colour) ) {
17 document.execCommand("BackColor", false, colour);
19 document.designMode = "off";
20 } else if (document.selection && document.selection.createRange) {
22 range = document.selection.createRange();
23 range.execCommand("BackColor", false, colour);
27 // function unselectThemes(themeId) {
28 // $('.Apple-style-span').each(function() {
29 // $(this).after($(this).html());
34 function gallery(element, url) {
35 var element = $(element);
36 var imageDimensions = {};
37 element.data('images', []);
39 function changePage(pageNumber) {
40 $('img', element).attr('src', element.data('images')[pageNumber - 1]);
43 function normalizeNumber(pageNumber) {
44 // Numer strony musi być pomiędzy 1 a najwyższym numerem
45 var pageCount = element.data('images').length;
46 pageNumber = parseInt(pageNumber, 10);
48 if (!pageNumber || pageNumber == NaN || pageNumber == Infinity || pageNumber == -Infinity) {
50 } else if (pageNumber < 1) {
52 } else if (pageNumber > pageCount) {
59 var pn = $('.page-number', element);
60 pn.change(function(event) {
61 console.log('change!', $(this).val());
62 event.preventDefault();
63 var n = normalizeNumber(pn.val());
67 $('.previous-page', element).click(function() {
68 pn.val(normalizeNumber(pn.val()) - 1);
71 $('.next-page', element).click(function() {
72 pn.val(normalizeNumber(pn.val()) + 1);
77 var image = $('img', element).attr('unselectable', 'on');
82 $('.zoom-in', element).click(function() {
83 zoomFactor = Math.min(2, zoomFactor + 0.2);
86 $('.zoom-out', element).click(function() {
87 zoomFactor = Math.max(0.2, zoomFactor - 0.2);
90 $('.change-gallery', element).click(function() {
91 $('.chosen-gallery').val($('#document-meta .gallery').html() || '/gallery/');
92 $('.gallery-image').animate({top: 53}, 200);
93 $('.chosen-gallery').focus();
95 $('.change-gallery-ok', element).click(function() {
96 if ($('#document-meta .gallery').length == 0) {
97 $('<div class="gallery"></div>').appendTo('#document-meta');
99 $('#document-meta .gallery').html($('.chosen-gallery').val());
100 updateGallery($('.chosen-gallery').val());
101 $('.gallery-image').animate({top: 27}, 200);
103 $('.change-gallery-cancel', element).click(function() {
104 $('.gallery-image').animate({top: 27}, 200);
107 $('img', element).load(function() {
108 image.css({width: null, height: null});
110 width: $(this).width() * zoomFactor,
111 height: $(this).height() * zoomFactor,
112 originWidth: $(this).width(),
113 originHeight: $(this).height(),
114 galleryWidth: $(this).parent().width(),
115 galleryHeight: $(this).parent().height()
117 console.log('load', imageDimensions)
118 var position = normalizePosition(
119 image.position().left,
120 image.position().top,
121 imageDimensions.galleryWidth,
122 imageDimensions.galleryHeight,
123 imageDimensions.width,
124 imageDimensions.height
126 image.css({left: position.x, top: position.y, width: $(this).width() * zoomFactor, height: $(this).height() * zoomFactor});
129 $(window).resize(function() {
130 imageDimensions.galleryWidth = image.parent().width();
131 imageDimensions.galleryHeight = image.parent().height();
134 function bounds(galleryWidth, galleryHeight, imageWidth, imageHeight) {
138 minX: galleryWidth - imageWidth,
139 minY: galleryHeight - imageHeight
143 function normalizePosition(x, y, galleryWidth, galleryHeight, imageWidth, imageHeight) {
144 var b = bounds(galleryWidth, galleryHeight, imageWidth, imageHeight);
146 x: Math.min(b.maxX, Math.max(b.minX, x)),
147 y: Math.min(b.maxY, Math.max(b.minY, y))
151 function onMouseMove(event) {
152 var position = normalizePosition(
153 event.clientX - origin.x + imageOrigin.left,
154 event.clientY - origin.y + imageOrigin.top,
155 imageDimensions.galleryWidth,
156 imageDimensions.galleryHeight,
157 imageDimensions.width,
158 imageDimensions.height
160 image.css({position: 'absolute', top: position.y, left: position.x});
164 function setZoom(factor) {
169 imageDimensions.width = imageDimensions.originWidth * zoomFactor;
170 imageDimensions.height = imageDimensions.originHeight * zoomFactor;
171 var position = normalizePosition(
172 image.position().left,
173 image.position().top,
174 imageDimensions.galleryWidth,
175 imageDimensions.galleryHeight,
176 imageDimensions.width,
177 imageDimensions.height
179 console.log(image.position(), imageDimensions, position);
180 image.css({width: imageDimensions.width, height: imageDimensions.height,
181 left: position.x, top: position.y});
185 function onMouseUp(event) {
187 .unbind('mousemove.gallery')
188 .unbind('mouseup.gallery');
192 image.bind('mousedown', function(event) {
197 imageOrigin = image.position();
199 .bind('mousemove.gallery', onMouseMove)
200 .bind('mouseup.gallery', onMouseUp);
204 function updateGallery(url) {
210 success: function(data) {
211 element.data('images', data);
214 $('img', element).show();
217 error: function(data) {
218 element.data('images', []);
221 $('img', element).hide();
232 function transform(editor) {
233 $.blockUI({message: 'Ładowanie...'});
234 setTimeout(function() {
236 xml: editor.getCode(),
237 success: function(element) {
238 $('#html-view').html(element);
240 }, error: function(text) {
241 $('#html-view').html('<p class="error">Wystąpił błąd:</p><pre>' + text + '</pre>');
249 function reverseTransform(editor) {
250 var serializer = new XMLSerializer();
251 if ($('#html-view .error').length > 0) {
254 $.blockUI({message: 'Ładowanie...'});
255 setTimeout(function() {
257 xml: serializer.serializeToString($('#html-view div').get(0)),
258 success: function(text) {
259 editor.setCode(text);
261 }, error: function(text) {
262 $('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
270 function html(element) {
271 var element = $(element);
273 function selectTheme(themeId)
275 var selection = window.getSelection();
277 // remove current selection
278 selection.removeAllRanges();
280 var range = document.createRange();
281 var s = $(".motyw[theme-class='"+themeId+"']")[0];
282 var e = $(".end[theme-class='"+themeId+"']")[0];
285 range.setStartAfter(s);
286 range.setEndBefore(e);
287 selection.addRange(range);
291 // function openForEdit($origin)
293 // // if(this.currentOpen && this.currentOpen != $origin) {
294 // // this.closeWithSave(this.currentOpen);
299 // // annotations overlay their sub box - not their own box //
300 // if($origin.is(".annotation-inline-box"))
301 // $box = $("*[x-annotation-box]", $origin);
305 // var x = $box[0].offsetLeft;
306 // var y = $box[0].offsetTop;
307 // var w = $box.outerWidth();
308 // var h = $box.innerHeight();
310 // console.log("Edit origin:", $origin, " box:", $box);
311 // console.log("offsetParent:", $box[0].offsetParent);
312 // console.log("Dimensions: ", x, y, w , h);
314 // // start edition on this node
315 // var $overlay = $('<div class="html-editarea"><textarea></textarea></div>');
317 // h = Math.max(h - 20, 2*parseInt($box.css('line-height')));
322 // position: 'absolute',
329 // $($box[0].offsetParent).append($overlay);
330 // console.log($overlay);
333 // $('.edit-button').live('click', function() {
334 // openForEdit($(this).parent());
337 var button = $('<button class="edit-button">Edytuj</button>');
338 $(element).bind('mousemove', function(event) {
339 var editable = $(event.target).closest('*[x-editable]');
340 $('.active[x-editable]', element).not(editable).removeClass('active').children('.edit-button').remove();
341 if (!editable.hasClass('active')) {
342 editable.addClass('active').append(button);
346 $('.motyw').live('click', function() {
347 selectTheme($(this).attr('theme-class'));
353 gallery('#sidebar', $('#document-meta .gallery').html());
356 CodeMirror.fromTextArea('id_text', {
357 parserfile: 'parsexml.js',
358 path: "/static/js/lib/codemirror/",
359 stylesheet: "/static/css/xmlcolors.css",
361 useHTMLKludges: false
363 iframeClass: 'xml-iframe',
367 initCallback: function(editor) {
368 $('#save-button').click(function(event) {
369 event.preventDefault();
370 $.blockUI({message: $('#save-dialog')});
373 $('#save-ok').click(function() {
374 $.blockUI({message: 'Zapisywanie...'});
376 var metaComment = '<!--';
377 $('#document-meta div').each(function() {
378 metaComment += '\n\t' + $(this).attr('class') + ': ' + $(this).html();
380 metaComment += '\n-->'
383 name: $('#document-name').html(),
384 text: metaComment + editor.getCode(),
385 revision: $('#document-revision').html(),
386 author: 'annonymous',
387 comment: $('#komentarz').val()
393 url: document.location.href,
397 success: function(data) {
399 editor.setCode(data.text);
400 $('#document-revision').html(data.revision);
402 console.log(data.errors);
407 error: function(xhr, textStatus, errorThrown) {
408 alert('error: ' + textStatus + ' ' + errorThrown);
413 $('#save-cancel').click(function() {
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();
436 reverseTransform(editor);
439 $('#source-editor .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');
471 $('#sidebar').width(0).hide();
472 $('#source-editor, #simple-editor').css({right: 15});
473 $('.vsplitbar').css({right: 0}).removeClass('active');