3 /* Show theme to the user */
4 function selectTheme(themeId){
5 var selection = window.getSelection();
6 selection.removeAllRanges();
8 var range = document.createRange();
9 var s = $("[x-node='motyw'][theme-class='" + themeId + "']")[0];
10 var e = $("[x-node='end'][theme-class='" + themeId + "']")[0];
13 range.setStartAfter(s);
14 range.setEndBefore(e);
15 selection.addRange(range);
19 /* Verify insertion port for annotation or theme */
20 function verifyTagInsertPoint(node){
21 if (node.nodeType == Node.TEXT_NODE) {
22 node = node.parentNode;
25 if (node.nodeType != Node.ELEMENT_NODE) {
30 if (node.attr('id') == 'caret') {
33 while (node.attr('x-pass-thru')) {
36 var xtype = node.attr('x-node');
38 if (!xtype || (xtype.search(':') >= 0) ||
48 function verifyThemeBoundaryPoint(node) {
49 if (!verifyTagInsertPoint(node)) return false;
51 // don't allow themes inside annotations
52 if (node.closest('[x-node="pe"]').length > 0)
58 /* Convert HTML fragment to plaintext */
59 var ANNOT_FORBIDDEN = ['pt', 'pa', 'pr', 'pe', 'begin', 'end', 'motyw'];
61 function html2plainText(fragment){
64 $(fragment.childNodes).each(function(){
65 if (this.nodeType == Node.TEXT_NODE)
66 text += this.nodeValue;
68 if (this.nodeType == Node.ELEMENT_NODE &&
69 $.inArray($(this).attr('x-node'), ANNOT_FORBIDDEN) == -1) {
70 text += html2plainText(this);
79 /* Insert annotation using current selection */
80 function addAnnotation(){
81 var selection = window.getSelection();
82 var n = selection.rangeCount;
84 if (selection.isCollapsed) {
85 window.alert("Nie zaznaczono żadnego obszaru");
89 var range = selection.getRangeAt(n - 1);
91 if (!verifyTagInsertPoint(range.endContainer)) {
92 window.alert("Nie można wstawić w to miejsce przypisu.");
97 for (let i = 0; i < n; ++ i) {
98 let rangeI = selection.getRangeAt(i);
99 if (verifyTagInsertPoint(rangeI.startContainer) &&
100 verifyTagInsertPoint(rangeI.endContainer)) {
101 text += html2plainText(rangeI.cloneContents());
104 var tag = $('<span></span>');
105 range.collapse(false);
106 range.insertNode(tag[0]);
109 xml: '<pe><slowo_obce>' + text + '</slowo_obce> --- </pe>',
110 success: function(text){
117 alert('Błąd przy dodawaniu przypisu:' + errors);
127 /* Insert theme using current selection */
130 var selection = window.getSelection();
131 var n = selection.rangeCount;
134 window.alert("Nie zaznaczono żadnego obszaru");
138 // for now allow only 1 range
140 window.alert("Zaznacz jeden obszar.");
145 // remember the selected range
146 var range = selection.getRangeAt(0);
149 if ($(range.startContainer).is('.html-editarea') ||
150 $(range.endContainer).is('.html-editarea')) {
151 window.alert("Motywy można oznaczać tylko na tekście nie otwartym do edycji. \n Zamknij edytowany fragment i spróbuj ponownie.");
155 // verify if the start/end points make even sense -
156 // they must be inside a x-node (otherwise they will be discarded)
157 // and the x-node must be a main text
158 if (!verifyThemeBoundaryPoint(range.startContainer)) {
159 window.alert("Motyw nie może się zaczynać w tym miejscu.");
163 if (!verifyThemeBoundaryPoint(range.endContainer)) {
164 window.alert("Motyw nie może się kończyć w tym miejscu.");
168 var date = (new Date()).getTime();
169 var random = Math.floor(4000000000 * Math.random());
170 var id = ('' + date) + '-' + ('' + random);
172 var createPoint = function(container, offset) {
173 var offsetBetweenCommas = function(text, offset) {
174 if(text.length < 2 || offset < 1 || offset > text.length)
176 return text[offset-1] === ',' && text[offset] === ',';
178 var point = document.createRange();
179 offset = offsetBetweenCommas(container.textContent, offset) ? offset - 1 : offset;
180 point.setStart(container, offset);
184 var spoint = createPoint(range.startContainer, range.startOffset);
185 var epoint = createPoint(range.endContainer, range.endOffset);
187 var mtag, btag, etag, errors;
192 xml: '<end id="e' + id + '" />',
193 success: function(text){
194 etag = $('<span></span>');
195 epoint.insertNode(etag[0]);
196 etag.replaceWith(text);
198 xml: '<motyw id="m' + id + '"></motyw>',
199 success: function(text){
200 mtag = $('<span></span>');
201 spoint.insertNode(mtag[0]);
202 mtag.replaceWith(text);
204 xml: '<begin id="b' + id + '" />',
205 success: function(text){
206 btag = $('<span></span>');
207 spoint.insertNode(btag[0])
208 btag.replaceWith(text);
209 selection.removeAllRanges();
210 openForEdit($('[x-node="motyw"][theme-class="' + id + '"]'));
219 function addSymbol(caret) {
223 editArea = $("textarea", caret.element)[0];
225 editArea = $('div.html-editarea textarea')[0];
229 var specialCharsContainer = $("<div id='specialCharsContainer'><a href='#' id='specialCharsClose'>Zamknij</a><table id='tableSpecialChars' style='width: 600px;'></table></div>");
231 var specialChars = [' ', 'Ą','ą','Ć','ć','Ę','ę','Ł','ł','Ń','ń','Ó','ó','Ś','ś','Ż','ż','Ź','ź','Á','á','À','à',
232 'Â','â','Ä','ä','Å','å','Ā','ā','Ă','ă','Ã','ã',
233 'Æ','æ','Ç','ç','Č','č','Ċ','ċ','Ď','ď','É','é','È','è',
234 'Ê','ê','Ë','ë','Ē','ē','Ě','ě','Ġ','ġ','Ħ','ħ','Í','í','Î','î',
235 'Ī','ī','Ĭ','ĭ','Ľ','ľ','Ñ','ñ','Ň','ň','Ó','ó','Ö','ö',
236 'Ô','ô','Ō','ō','Ǒ','ǒ','Œ','œ','Ø','ø','Ř','ř','Š',
237 'š','Ş','ş','Ť','ť','Ţ','ţ','Ű','ű','Ú','ú','Ù','ù',
238 'Ü','ü','Ů','ů','Ū','ū','Û','û','Ŭ','ŭ',
239 'Ý','ý','Ž','ž','ß','Ð','ð','Þ','þ','А','а','Б',
240 'б','В','в','Г','г','Д','д','Е','е','Ё','ё','Ж',
241 'ж','З','з','И','и','Й','й','К','к','Л','л','М',
242 'м','Н','н','О','о','П','п','Р','р','С','с',
243 'Т','т','У','у','Ф','ф','Х','х','Ц','ц','Ч',
244 'ч','Ш','ш','Щ','щ','Ъ','ъ','Ы','ы','Ь','ь','Э',
245 'э','Ю','ю','Я','я','ѓ','є','і','ї','ј','љ','њ',
246 'Ґ','ґ','Α','α','Β','β','Γ','γ','Δ','δ','Ε','ε',
247 'Ζ','ζ','Η','η','Θ','θ','Ι','ι','Κ','κ','Λ','λ','Μ',
248 'μ','Ν','ν','Ξ','ξ','Ο','ο','Π','π','Ρ','ρ','Σ','ς','σ',
249 'Τ','τ','Υ','υ','Φ','φ','Χ','χ','Ψ','ψ','Ω','ω','–',
250 '—','¡','¿','$','¢','£','€','©','®','°','¹','²','³',
251 '¼','½','¾','†','§','‰','•','←','↑','→','↓',
252 '„','”','„”','«','»','«»','»«','’','[',']','~','|','−','·',
253 '×','÷','≈','≠','±','≤','≥','∈'];
254 var tableContent = "<tr>";
256 for(var i in specialChars) {
257 if(i % 14 == 0 && i > 0) {
258 tableContent += "</tr><tr>";
260 tableContent += "<td><input type='button' class='specialBtn' value='"+specialChars[i]+"'/></td>";
263 tableContent += "</tr>";
264 $("body").append(specialCharsContainer);
267 // localStorage for recently used characters - reading
268 if (typeof(localStorage) != 'undefined') {
269 if (localStorage.getItem("recentSymbols")) {
270 var recent = localStorage.getItem("recentSymbols");
271 var recentArray = recent.split(";");
273 for(var i in recentArray.reverse()) {
274 recentRow += "<td><input type='button' class='specialBtn recentSymbol' value='"+recentArray[i]+"'/></td>";
276 recentRow = "<tr>" + recentRow + "</tr>";
279 $("#tableSpecialChars").append(recentRow);
280 $("#tableSpecialChars").append(tableContent);
284 $('.specialBtn').click(function(){
285 var insertVal = $(this).val();
287 // if we want to surround text with quotes
288 // not sure if just check if value has length == 2
291 caret.insertChar(insertVal);
294 if (insertVal.length == 2) {
295 var startTag = insertVal[0];
296 var endTag = insertVal[1];
297 var textAreaOpened = editArea;
299 if (document.selection) {
300 textAreaOpened.focus();
301 sel = document.selection.createRange();
302 sel.text = startTag + sel.text + endTag;
304 //MOZILLA/NETSCAPE support
305 else if (textAreaOpened.selectionStart || textAreaOpened.selectionStart == '0') {
306 var startPos = textAreaOpened.selectionStart;
307 var endPos = textAreaOpened.selectionEnd;
308 textAreaOpened.value = textAreaOpened.value.substring(0, startPos)
309 + startTag + textAreaOpened.value.substring(startPos, endPos) + endTag + textAreaOpened.value.substring(endPos, textAreaOpened.value.length);
312 insertAtCaret(editArea, insertVal);
316 // localStorage for recently used characters - saving
317 if (typeof(localStorage) != 'undefined') {
318 if (localStorage.getItem("recentSymbols")) {
319 var recent = localStorage.getItem("recentSymbols");
320 var recentArray = recent.split(";");
321 var valIndex = $.inArray(insertVal, recentArray);
324 // value not present in array yet
325 if(recentArray.length > 13){
327 recentArray.push(insertVal);
329 recentArray.push(insertVal);
332 // value already in the array
333 for(var i = valIndex; i < recentArray.length; i++){
334 recentArray[i] = recentArray[i+1];
336 recentArray[recentArray.length-1] = insertVal;
338 localStorage.setItem("recentSymbols", recentArray.join(";"));
340 localStorage.setItem("recentSymbols", insertVal);
343 $(specialCharsContainer).remove();
345 $('#specialCharsClose').click(function(){
346 $(specialCharsContainer).remove();
350 window.alert('Najedź na fragment tekstu, wybierz "Edytuj" i ustaw kursor na miejscu gdzie chcesz wstawić symbol.');
354 function insertAtCaret(txtarea,text) {
355 /* http://www.scottklarr.com/topic/425/how-to-insert-text-into-a-textarea-where-the-cursor-is/ */
356 var scrollPos = txtarea.scrollTop;
359 var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) );
362 var range = document.selection.createRange();
363 range.moveStart ('character', -txtarea.value.length);
364 strPos = backStart = range.text.length;
365 } else if (br == "ff") {
366 strPos = txtarea.selectionStart;
367 backStart = txtarea.selectionEnd;
369 var front = (txtarea.value).substring(0,strPos);
370 var back = (txtarea.value).substring(backStart,txtarea.value.length);
371 txtarea.value=front+text+back;
372 strPos = strPos + text.length;
375 var range = document.selection.createRange();
376 range.moveStart ('character', -txtarea.value.length);
377 range.moveStart ('character', strPos);
378 range.moveEnd ('character', 0);
380 } else if (br == "ff") {
381 txtarea.selectionStart = strPos;
382 txtarea.selectionEnd = strPos;
385 txtarea.scrollTop = scrollPos;
388 /* open edition window for selected fragment */
389 function openForEdit($origin){
392 // annotations overlay their sub box - not their own box //
393 if ($origin.is(".annotation-inline-box")) {
394 $box = $("*[x-annotation-box]", $origin);
399 var x = $box[0].offsetLeft;
400 var y = $box[0].offsetTop;
402 var w = $box.outerWidth();
403 var h = $box.innerHeight();
405 if ($origin.is(".annotation-inline-box")) {
406 w = Math.max(w, 400);
408 if($('.htmlview div').offset().left + $('.htmlview div').width() > ($('.vsplitbar').offset().left - 480)){
409 x = -(Math.max($origin.offset().left, $origin.width()));
414 if ($origin.is('.reference-inline-box')) {
420 $('.htmlview div').offset().left + $('.htmlview div').width() - 400
424 // start edition on this node
425 var $overlay = $('<div class="html-editarea"><button class="accept-button">Zapisz</button><button class="delete-button">Usuń</button><button class="tytul-button akap-edit-button">tytuł dzieła</button><button class="wyroznienie-button akap-edit-button">wyróżnienie</button><button class="slowo-button akap-edit-button">słowo obce</button><button class="znak-button akap-edit-button">znak spec.</button><textarea></textarea></div>').css({
426 position: 'absolute',
431 }).appendTo($box[0].offsetParent || $box.parent()).show();
434 if ($origin.is('*[x-edit-no-format]')) {
435 $('.akap-edit-button').remove();
438 if ($origin.is('[x-node="motyw"]')) {
439 $.themes.autocomplete($('textarea', $overlay));
442 if ($origin.is('[x-node="motyw"]')){
443 $('.delete-button', $overlay).click(function(){
444 if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten motyw?")) {
445 $('[theme-class="' + $origin.attr('theme-class') + '"]').remove();
447 $(document).unbind('click.blur-overlay');
452 else if($box.is('*[x-annotation-box]') || $origin.is('*[x-edit-attribute]') || $origin.is('*[x-node="uwaga"]')) {
454 switch ($origin.attr('x-node')) {
464 $('.delete-button', $overlay).click(function(){
465 if (window.confirm("Czy jesteś pewien, że chcesz usunąć " + q + "?")) {
468 $(document).unbind('click.blur-overlay');
474 $('.delete-button', $overlay).html("Anuluj");
475 $('.delete-button', $overlay).click(function(){
476 if (window.confirm("Czy jesteś pewien, że chcesz anulować zmiany?")) {
478 $(document).unbind('click.blur-overlay');
485 var serializer = new XMLSerializer();
487 if($box.attr("x-edit-attribute")) {
488 source = $('<span x-pass-thru="true"/>');
489 source.text($box.attr("x-a-wl-" + $box.attr("x-edit-attribute")));
498 success: function(text){
499 $('textarea', $overlay).val($.trim(text));
501 setTimeout(function(){
502 $('textarea', $overlay).elastic().focus();
505 function save(argument){
506 var nodeName = $box.attr('x-node') || 'pe';
507 var insertedText = $('textarea', $overlay).val();
509 if ($origin.is('[x-node="motyw"]')) {
510 insertedText = insertedText.replace(/,\s*$/, '');
513 if($box.attr("x-edit-attribute")) {
514 xml = '<' + nodeName + ' ' + $box.attr("x-edit-attribute") + '="' + insertedText + '"/>';
516 xml = '<' + nodeName + '>' + insertedText + '</' + nodeName + '>';
522 success: function(element){
523 if (nodeName == 'out-of-flow-text') {
524 $(element).children().insertAfter($origin);
527 else if ($box.attr('x-edit-attribute')) {
528 $(element).insertAfter($origin);
532 $origin.html($(element).html());
536 error: function(text){
537 alert('Błąd! ' + text);
541 var msg = $("<div class='saveNotify'><p>Pamiętaj, żeby zapisać swoje zmiany.</p></div>");
542 $("#base").prepend(msg);
543 $('#base .saveNotify').fadeOut(3000, function(){
548 $('.akap-edit-button', $overlay).click(function(){
549 var textAreaOpened = $('textarea', $overlay)[0];
552 var buttonName = this.innerHTML;
554 if(buttonName == "słowo obce") {
555 startTag = "<slowo_obce>";
556 endTag = "</slowo_obce>";
557 } else if (buttonName == "wyróżnienie") {
558 startTag = "<wyroznienie>";
559 endTag = "</wyroznienie>";
560 } else if (buttonName == "tytuł dzieła") {
561 startTag = "<tytul_dziela>";
562 endTag = "</tytul_dziela>";
563 } else if(buttonName == "znak spec."){
568 var myField = textAreaOpened;
571 if (document.selection) {
572 textAreaOpened.focus();
573 sel = document.selection.createRange();
574 sel.text = startTag + sel.text + endTag;
576 //MOZILLA/NETSCAPE support
577 else if (textAreaOpened.selectionStart || textAreaOpened.selectionStart == '0') {
578 var startPos = textAreaOpened.selectionStart;
579 var endPos = textAreaOpened.selectionEnd;
580 textAreaOpened.value = textAreaOpened.value.substring(0, startPos)
581 + startTag + textAreaOpened.value.substring(startPos, endPos) + endTag + textAreaOpened.value.substring(endPos, textAreaOpened.value.length);
585 $('.accept-button', $overlay).click(function(){
587 $(document).unbind('click.blur-overlay');
590 $(document).bind('click.blur-overlay', function(event){
591 if ($(event.target).closest('.html-editarea, #specialCharsContainer').length > 0) {
595 $(document).unbind('click.blur-overlay');
599 error: function(text){
600 alert('Błąd! ' + text);
605 function createUwagaBefore(before) {
608 success: function(element){
609 let $element = $(element);
610 $element.insertBefore(before);
611 openForEdit($element);
616 function VisualPerspective(options){
617 perspective = self = this;
619 var old_callback = options.callback;
621 options.callback = function(){
622 var element = $("#html-view");
623 var button = $('<button class="edit-button active-block-button">Edytuj</button>');
624 var uwagaButton = $('<button class="uwaga-button active-block-button">Uwaga</button>');
626 if (!CurrentDocument.readonly) {
628 $('#html-view').bind('mousemove', function(event){
629 var editable = $(event.target).closest('*[x-editable]');
630 $('.active', element).not(editable).removeClass('active').children('.active-block-button').remove();
632 if (!editable.hasClass('active')) {
633 editable.addClass('active').append(button);
634 if (!editable.is('[x-edit-attribute]') &&
635 !editable.is('.annotation-inline-box') &&
636 !editable.is('[x-edit-no-format]')
638 editable.append(uwagaButton);
641 if (editable.is('.annotation-inline-box')) {
642 $('*[x-annotation-box]', editable).css({
647 perspective.caret = new Caret(element);
649 $('#insert-reference-button').click(function(){
654 $('#insert-annotation-button').click(function(){
659 $('#insert-theme-button').click(function(){
665 $(".insert-inline-tag").click(function() {
666 perspective.insertInlineTag($(this).attr('data-tag'));
670 $(".insert-char").click(function() {
671 addSymbol(caret=perspective.caret);
675 $(document).on('click', '.edit-button', function(event){
676 event.preventDefault();
677 openForEdit($(this).parent());
680 $(document).on('click', '.uwaga-button', function(event){
681 event.preventDefault();
682 createUwagaBefore($(this).parent());
686 $(document).on('click', '[x-node="motyw"]', function(){
687 selectTheme($(this).attr('theme-class'));
690 element.on('click', '.annotation', function(event) {
691 event.preventDefault();
692 event.redakcja_caret_ignore = true;
693 $('[x-annotation-box]', $(this).parent()).toggleClass('editing');
694 perspective.caret.detach();
697 old_callback.call(this);
700 $.wiki.Perspective.call(this, options);
703 VisualPerspective.prototype = new $.wiki.Perspective();
705 VisualPerspective.prototype.onEnter = function(success, failure){
706 $.wiki.Perspective.prototype.onEnter.call(this);
709 message: 'Uaktualnianie widoku...'
712 function _finalize(callback){
721 base: this.doc.getBase(),
722 success: function(element){
724 var htmlView = $('#html-view');
725 htmlView.html(element);
729 error: function(text, source){
730 err = '<p class="error">Wystąpił błąd:</p><p>'+text+'</p>';
732 err += '<pre>'+source.replace(/&/g, '&').replace(/</g, '<')+'</pre>'
733 $('#html-view').html(err);
739 VisualPerspective.prototype.onExit = function(success, failure){
744 $.wiki.exitTab('#PropertiesPerspective');
747 message: 'Zapisywanie widoku...'
750 function _finalize(callback){
756 if ($('#html-view .error').length > 0)
757 return _finalize(failure);
760 element: $('#html-view').get(0),
762 success: function(text){
763 self.doc.setText(text);
766 error: function(text){
767 $('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
773 VisualPerspective.prototype.insertInlineTag = function(tag) {
776 let selection = window.getSelection();
777 var n = selection.rangeCount;
778 if (n != 1 || selection.isCollapsed) {
779 window.alert("Nie zaznaczono obszaru");
782 let range = selection.getRangeAt(0);
785 // Both ends are in the same x-node container.
786 // TODO: That the container is a inline-text container.
787 let node = range.startContainer;
788 if (node.nodeType == node.TEXT_NODE) {
789 node = node.parentNode;
791 let endNode = range.endContainer;
792 if (endNode.nodeType == endNode.TEXT_NODE) {
793 endNode = endNode.parentNode;
795 if (node != endNode) {
796 window.alert("Zły obszar.");
800 // We will construct a HTML element with the range selected.
801 let div = $("<span x-pass-thru='true'>");
803 contents = $(node).contents();
804 let startChildIndex = node == range.startContainer ? 0 : contents.index(range.startContainer);
805 let endChildIndex = contents.index(range.endContainer);
807 current = range.startContainer;
808 if (current.nodeType == current.TEXT_NODE) {
809 current = current.splitText(range.startOffset);
811 while (current != range.endContainer) {
812 n = current.nextSibling;
813 $(current).appendTo(div);
816 if (current.nodeType == current.TEXT_NODE) {
817 end = current.splitText(range.endOffset);
819 $(current).appendTo(div);
823 success: function(d) {
825 xml: d = '<' + tag + '>' + d + '</' + tag + '>',
826 success: function(html) {
828 node.insertBefore($(html)[0], end);
832 error: function(a, b) {
838 VisualPerspective.prototype.insertAtRange = function(range, elem) {
840 let $end = $(range.endContainer);
841 if ($end.attr('id') == 'caret') {
842 self.caret.insert(elem);
844 range.insertNode(elem[0]);
848 VisualPerspective.prototype.addReference = function() {
850 var selection = window.getSelection();
851 var n = selection.rangeCount;
853 // TODO: if no selection, take caret position..
855 window.alert("Nie zaznaczono żadnego obszaru");
859 var range = selection.getRangeAt(n - 1);
860 if (!verifyTagInsertPoint(range.endContainer)) {
861 window.alert("Nie można wstawić w to miejsce referencji.");
865 var tag = $('<span></span>');
867 range.collapse(false);
868 self.insertAtRange(range, tag);
871 xml: '<ref href=""/>',
872 success: function(text){
879 alert('Błąd przy dodawaniu referncji:' + errors);
884 $.wiki.VisualPerspective = VisualPerspective;