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 == 3) { // Text Node
22 node = node.parentNode;
25 if (node.nodeType != 1) {
30 var xtype = node.attr('x-node');
32 if (!xtype || (xtype.search(':') >= 0) ||
39 // don't allow themes inside annotations
40 if (node.closest('[x-node="pe"]').length > 0)
46 /* Convert HTML fragment to plaintext */
47 var ANNOT_FORBIDDEN = ['pt', 'pa', 'pr', 'pe', 'begin', 'end', 'motyw'];
49 function html2plainText(fragment){
52 $(fragment.childNodes).each(function(){
53 if (this.nodeType == 3) // textNode
54 text += this.nodeValue;
56 if (this.nodeType == 1 &&
57 $.inArray($(this).attr('x-node'), ANNOT_FORBIDDEN) == -1) {
58 text += html2plainText(this);
67 /* Insert annotation using current selection */
68 function addAnnotation(){
69 var selection = window.getSelection();
70 var n = selection.rangeCount;
73 window.alert("Nie zaznaczono żadnego obszaru");
77 // for now allow only 1 range
79 window.alert("Zaznacz jeden obszar");
83 // remember the selected range
84 var range = selection.getRangeAt(0);
86 if (!verifyTagInsertPoint(range.endContainer)) {
87 window.alert("Nie można wstawić w to miejsce przypisu.");
91 // BUG #273 - selected text can contain themes, which should be omitted from
93 var text = html2plainText(range.cloneContents());
94 var tag = $('<span></span>');
95 range.collapse(false);
96 range.insertNode(tag[0]);
99 xml: '<pe><slowo_obce>' + text + '</slowo_obce> --- </pe>',
100 success: function(text){
107 alert('Błąd przy dodawaniu przypisu:' + errors);
113 function addReference(){
114 var selection = window.getSelection();
115 var n = selection.rangeCount;
118 window.alert("Nie zaznaczono żadnego obszaru");
122 // for now allow only 1 range
124 window.alert("Zaznacz jeden obszar");
128 // remember the selected range
129 var range = selection.getRangeAt(0);
131 if (!verifyTagInsertPoint(range.endContainer)) {
132 window.alert("Nie można wstawić w to miejsce przypisu.");
136 var tag = $('<span></span>');
137 range.collapse(false);
138 range.insertNode(tag[0]);
141 xml: '<ref href=""/>',
142 success: function(text){
149 alert('Błąd przy dodawaniu referncji:' + errors);
157 /* Insert theme using current selection */
160 var selection = window.getSelection();
161 var n = selection.rangeCount;
164 window.alert("Nie zaznaczono żadnego obszaru");
168 // for now allow only 1 range
170 window.alert("Zaznacz jeden obszar.");
175 // remember the selected range
176 var range = selection.getRangeAt(0);
179 if ($(range.startContainer).is('.html-editarea') ||
180 $(range.endContainer).is('.html-editarea')) {
181 window.alert("Motywy można oznaczać tylko na tekście nie otwartym do edycji. \n Zamknij edytowany fragment i spróbuj ponownie.");
185 // verify if the start/end points make even sense -
186 // they must be inside a x-node (otherwise they will be discarded)
187 // and the x-node must be a main text
188 if (!verifyTagInsertPoint(range.startContainer)) {
189 window.alert("Motyw nie może się zaczynać w tym miejscu.");
193 if (!verifyTagInsertPoint(range.endContainer)) {
194 window.alert("Motyw nie może się kończyć w tym miejscu.");
198 var date = (new Date()).getTime();
199 var random = Math.floor(4000000000 * Math.random());
200 var id = ('' + date) + '-' + ('' + random);
202 var createPoint = function(container, offset) {
203 var offsetBetweenCommas = function(text, offset) {
204 if(text.length < 2 || offset < 1 || offset > text.length)
206 return text[offset-1] === ',' && text[offset] === ',';
208 var point = document.createRange();
209 offset = offsetBetweenCommas(container.textContent, offset) ? offset - 1 : offset;
210 point.setStart(container, offset);
214 var spoint = createPoint(range.startContainer, range.startOffset);
215 var epoint = createPoint(range.endContainer, range.endOffset);
217 var mtag, btag, etag, errors;
222 xml: '<end id="e' + id + '" />',
223 success: function(text){
224 etag = $('<span></span>');
225 epoint.insertNode(etag[0]);
226 etag.replaceWith(text);
228 xml: '<motyw id="m' + id + '"></motyw>',
229 success: function(text){
230 mtag = $('<span></span>');
231 spoint.insertNode(mtag[0]);
232 mtag.replaceWith(text);
234 xml: '<begin id="b' + id + '" />',
235 success: function(text){
236 btag = $('<span></span>');
237 spoint.insertNode(btag[0])
238 btag.replaceWith(text);
239 selection.removeAllRanges();
240 openForEdit($('[x-node="motyw"][theme-class="' + id + '"]'));
249 function addSymbol(caret) {
253 editArea = $("textarea", caret.element)[0];
255 editArea = $('div.html-editarea textarea')[0];
259 var specialCharsContainer = $("<div id='specialCharsContainer'><a href='#' id='specialCharsClose'>Zamknij</a><table id='tableSpecialChars' style='width: 600px;'></table></div>");
261 var specialChars = [' ', 'Ą','ą','Ć','ć','Ę','ę','Ł','ł','Ń','ń','Ó','ó','Ś','ś','Ż','ż','Ź','ź','Á','á','À','à',
262 'Â','â','Ä','ä','Å','å','Ā','ā','Ă','ă','Ã','ã',
263 'Æ','æ','Ç','ç','Č','č','Ċ','ċ','Ď','ď','É','é','È','è',
264 'Ê','ê','Ë','ë','Ē','ē','Ě','ě','Ġ','ġ','Ħ','ħ','Í','í','Î','î',
265 'Ī','ī','Ĭ','ĭ','Ľ','ľ','Ñ','ñ','Ň','ň','Ó','ó','Ö','ö',
266 'Ô','ô','Ō','ō','Ǒ','ǒ','Œ','œ','Ø','ø','Ř','ř','Š',
267 'š','Ş','ş','Ť','ť','Ţ','ţ','Ű','ű','Ú','ú','Ù','ù',
268 'Ü','ü','Ů','ů','Ū','ū','Û','û','Ŭ','ŭ',
269 'Ý','ý','Ž','ž','ß','Ð','ð','Þ','þ','А','а','Б',
270 'б','В','в','Г','г','Д','д','Е','е','Ё','ё','Ж',
271 'ж','З','з','И','и','Й','й','К','к','Л','л','М',
272 'м','Н','н','О','о','П','п','Р','р','С','с',
273 'Т','т','У','у','Ф','ф','Х','х','Ц','ц','Ч',
274 'ч','Ш','ш','Щ','щ','Ъ','ъ','Ы','ы','Ь','ь','Э',
275 'э','Ю','ю','Я','я','ѓ','є','і','ї','ј','љ','њ',
276 'Ґ','ґ','Α','α','Β','β','Γ','γ','Δ','δ','Ε','ε',
277 'Ζ','ζ','Η','η','Θ','θ','Ι','ι','Κ','κ','Λ','λ','Μ',
278 'μ','Ν','ν','Ξ','ξ','Ο','ο','Π','π','Ρ','ρ','Σ','ς','σ',
279 'Τ','τ','Υ','υ','Φ','φ','Χ','χ','Ψ','ψ','Ω','ω','–',
280 '—','¡','¿','$','¢','£','€','©','®','°','¹','²','³',
281 '¼','½','¾','†','§','‰','•','←','↑','→','↓',
282 '„','”','„”','«','»','«»','»«','’','[',']','~','|','−','·',
283 '×','÷','≈','≠','±','≤','≥','∈'];
284 var tableContent = "<tr>";
286 for(var i in specialChars) {
287 if(i % 14 == 0 && i > 0) {
288 tableContent += "</tr><tr>";
290 tableContent += "<td><input type='button' class='specialBtn' value='"+specialChars[i]+"'/></td>";
293 tableContent += "</tr>";
294 $("body").append(specialCharsContainer);
297 // localStorage for recently used characters - reading
298 if (typeof(localStorage) != 'undefined') {
299 if (localStorage.getItem("recentSymbols")) {
300 var recent = localStorage.getItem("recentSymbols");
301 var recentArray = recent.split(";");
303 for(var i in recentArray.reverse()) {
304 recentRow += "<td><input type='button' class='specialBtn recentSymbol' value='"+recentArray[i]+"'/></td>";
306 recentRow = "<tr>" + recentRow + "</tr>";
309 $("#tableSpecialChars").append(recentRow);
310 $("#tableSpecialChars").append(tableContent);
314 $('.specialBtn').click(function(){
315 var insertVal = $(this).val();
317 // if we want to surround text with quotes
318 // not sure if just check if value has length == 2
321 caret.insertChar(insertVal);
324 if (insertVal.length == 2) {
325 var startTag = insertVal[0];
326 var endTag = insertVal[1];
327 var textAreaOpened = editArea;
329 if (document.selection) {
330 textAreaOpened.focus();
331 sel = document.selection.createRange();
332 sel.text = startTag + sel.text + endTag;
334 //MOZILLA/NETSCAPE support
335 else if (textAreaOpened.selectionStart || textAreaOpened.selectionStart == '0') {
336 var startPos = textAreaOpened.selectionStart;
337 var endPos = textAreaOpened.selectionEnd;
338 textAreaOpened.value = textAreaOpened.value.substring(0, startPos)
339 + startTag + textAreaOpened.value.substring(startPos, endPos) + endTag + textAreaOpened.value.substring(endPos, textAreaOpened.value.length);
342 insertAtCaret(editArea, insertVal);
346 // localStorage for recently used characters - saving
347 if (typeof(localStorage) != 'undefined') {
348 if (localStorage.getItem("recentSymbols")) {
349 var recent = localStorage.getItem("recentSymbols");
350 var recentArray = recent.split(";");
351 var valIndex = $.inArray(insertVal, recentArray);
354 // value not present in array yet
355 if(recentArray.length > 13){
357 recentArray.push(insertVal);
359 recentArray.push(insertVal);
362 // value already in the array
363 for(var i = valIndex; i < recentArray.length; i++){
364 recentArray[i] = recentArray[i+1];
366 recentArray[recentArray.length-1] = insertVal;
368 localStorage.setItem("recentSymbols", recentArray.join(";"));
370 localStorage.setItem("recentSymbols", insertVal);
373 $(specialCharsContainer).remove();
375 $('#specialCharsClose').click(function(){
376 $(specialCharsContainer).remove();
380 window.alert('Najedź na fragment tekstu, wybierz "Edytuj" i ustaw kursor na miejscu gdzie chcesz wstawić symbol.');
384 function insertAtCaret(txtarea,text) {
385 /* http://www.scottklarr.com/topic/425/how-to-insert-text-into-a-textarea-where-the-cursor-is/ */
386 var scrollPos = txtarea.scrollTop;
389 var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) );
392 var range = document.selection.createRange();
393 range.moveStart ('character', -txtarea.value.length);
394 strPos = backStart = range.text.length;
395 } else if (br == "ff") {
396 strPos = txtarea.selectionStart;
397 backStart = txtarea.selectionEnd;
399 var front = (txtarea.value).substring(0,strPos);
400 var back = (txtarea.value).substring(backStart,txtarea.value.length);
401 txtarea.value=front+text+back;
402 strPos = strPos + text.length;
405 var range = document.selection.createRange();
406 range.moveStart ('character', -txtarea.value.length);
407 range.moveStart ('character', strPos);
408 range.moveEnd ('character', 0);
410 } else if (br == "ff") {
411 txtarea.selectionStart = strPos;
412 txtarea.selectionEnd = strPos;
415 txtarea.scrollTop = scrollPos;
418 /* open edition window for selected fragment */
419 function openForEdit($origin){
422 // annotations overlay their sub box - not their own box //
423 if ($origin.is(".annotation-inline-box")) {
424 $box = $("*[x-annotation-box]", $origin);
429 var x = $box[0].offsetLeft;
430 var y = $box[0].offsetTop;
432 var w = $box.outerWidth();
433 var h = $box.innerHeight();
435 if ($origin.is(".annotation-inline-box")) {
436 w = Math.max(w, 400);
438 if($('.htmlview div').offset().left + $('.htmlview div').width() > ($('.vsplitbar').offset().left - 480)){
439 x = -(Math.max($origin.offset().left, $origin.width()));
444 if ($origin.is('.reference-inline-box')) {
450 $('.htmlview div').offset().left + $('.htmlview div').width() - 400
454 // start edition on this node
455 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({
456 position: 'absolute',
461 }).appendTo($box[0].offsetParent || $box.parent()).show();
464 if ($origin.is('*[x-edit-no-format]')) {
465 $('.akap-edit-button').remove();
468 if ($origin.is('[x-node="motyw"]')) {
469 $.themes.autocomplete($('textarea', $overlay));
472 if ($origin.is('[x-node="motyw"]')){
473 $('.delete-button', $overlay).click(function(){
474 if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten motyw?")) {
475 $('[theme-class="' + $origin.attr('theme-class') + '"]').remove();
477 $(document).unbind('click.blur-overlay');
482 else if($box.is('*[x-annotation-box]') || $origin.is('*[x-edit-attribute]') || $origin.is('*[x-node="uwaga"]')) {
484 switch ($origin.attr('x-node')) {
494 $('.delete-button', $overlay).click(function(){
495 if (window.confirm("Czy jesteś pewien, że chcesz usunąć " + q + "?")) {
498 $(document).unbind('click.blur-overlay');
504 $('.delete-button', $overlay).html("Anuluj");
505 $('.delete-button', $overlay).click(function(){
506 if (window.confirm("Czy jesteś pewien, że chcesz anulować zmiany?")) {
508 $(document).unbind('click.blur-overlay');
515 var serializer = new XMLSerializer();
517 if($box.attr("x-edit-attribute")) {
518 source = $('<span x-pass-thru="true"/>');
519 source.text($box.attr("x-a-wl-" + $box.attr("x-edit-attribute")));
528 success: function(text){
529 $('textarea', $overlay).val($.trim(text));
531 setTimeout(function(){
532 $('textarea', $overlay).elastic().focus();
535 function save(argument){
536 var nodeName = $box.attr('x-node') || 'pe';
537 var insertedText = $('textarea', $overlay).val();
539 if ($origin.is('[x-node="motyw"]')) {
540 insertedText = insertedText.replace(/,\s*$/, '');
543 if($box.attr("x-edit-attribute")) {
544 xml = '<' + nodeName + ' ' + $box.attr("x-edit-attribute") + '="' + insertedText + '"/>';
546 xml = '<' + nodeName + '>' + insertedText + '</' + nodeName + '>';
552 success: function(element){
553 if (nodeName == 'out-of-flow-text') {
554 $(element).children().insertAfter($origin);
557 else if ($box.attr('x-edit-attribute')) {
558 $(element).insertAfter($origin);
562 $origin.html($(element).html());
566 error: function(text){
567 alert('Błąd! ' + text);
571 var msg = $("<div class='saveNotify'><p>Pamiętaj, żeby zapisać swoje zmiany.</p></div>");
572 $("#base").prepend(msg);
573 $('#base .saveNotify').fadeOut(3000, function(){
578 $('.akap-edit-button', $overlay).click(function(){
579 var textAreaOpened = $('textarea', $overlay)[0];
582 var buttonName = this.innerHTML;
584 if(buttonName == "słowo obce") {
585 startTag = "<slowo_obce>";
586 endTag = "</slowo_obce>";
587 } else if (buttonName == "wyróżnienie") {
588 startTag = "<wyroznienie>";
589 endTag = "</wyroznienie>";
590 } else if (buttonName == "tytuł dzieła") {
591 startTag = "<tytul_dziela>";
592 endTag = "</tytul_dziela>";
593 } else if(buttonName == "znak spec."){
598 var myField = textAreaOpened;
601 if (document.selection) {
602 textAreaOpened.focus();
603 sel = document.selection.createRange();
604 sel.text = startTag + sel.text + endTag;
606 //MOZILLA/NETSCAPE support
607 else if (textAreaOpened.selectionStart || textAreaOpened.selectionStart == '0') {
608 var startPos = textAreaOpened.selectionStart;
609 var endPos = textAreaOpened.selectionEnd;
610 textAreaOpened.value = textAreaOpened.value.substring(0, startPos)
611 + startTag + textAreaOpened.value.substring(startPos, endPos) + endTag + textAreaOpened.value.substring(endPos, textAreaOpened.value.length);
615 $('.accept-button', $overlay).click(function(){
617 $(document).unbind('click.blur-overlay');
620 $(document).bind('click.blur-overlay', function(event){
621 if ($(event.target).closest('.html-editarea, #specialCharsContainer').length > 0) {
625 $(document).unbind('click.blur-overlay');
629 error: function(text){
630 alert('Błąd! ' + text);
635 function createUwagaBefore(before) {
638 success: function(element){
639 let $element = $(element);
640 $element.insertBefore(before);
641 openForEdit($element);
646 function VisualPerspective(options){
649 var old_callback = options.callback;
651 options.callback = function(){
652 var element = $("#html-view");
653 var button = $('<button class="edit-button active-block-button">Edytuj</button>');
654 var uwagaButton = $('<button class="uwaga-button active-block-button">Uwaga</button>');
656 if (!CurrentDocument.readonly) {
658 $('#html-view').bind('mousemove', function(event){
659 var editable = $(event.target).closest('*[x-editable]');
660 $('.active', element).not(editable).removeClass('active').children('.active-block-button').remove();
662 if (!editable.hasClass('active')) {
663 editable.addClass('active').append(button);
664 if (!editable.is('[x-edit-attribute]')) {
665 editable.append(uwagaButton);
668 if (editable.is('.annotation-inline-box')) {
669 $('*[x-annotation-box]', editable).css({
674 perspective.caret = new Caret(element);
676 $('#insert-reference-button').click(function(){
681 $('#insert-annotation-button').click(function(){
686 $('#insert-theme-button').click(function(){
692 $(".insert-inline-tag").click(function() {
693 perspective.insertInlineTag($(this).attr('data-tag'));
697 $(".insert-char").click(function() {
698 console.log('perspective', perspective);
699 addSymbol(caret=perspective.caret);
703 $(document).on('click', '.edit-button', function(event){
704 event.preventDefault();
705 openForEdit($(this).parent());
708 $(document).on('click', '.uwaga-button', function(event){
709 event.preventDefault();
710 createUwagaBefore($(this).parent());
714 $(document).on('click', '[x-node="motyw"]', function(){
715 selectTheme($(this).attr('theme-class'));
718 element.on('click', '.annotation', function(event) {
719 event.preventDefault();
720 $('[x-annotation-box]', $(this).parent()).toggleClass('editing');
724 old_callback.call(this);
727 $.wiki.Perspective.call(this, options);
730 VisualPerspective.prototype = new $.wiki.Perspective();
732 VisualPerspective.prototype.onEnter = function(success, failure){
733 $.wiki.Perspective.prototype.onEnter.call(this);
736 message: 'Uaktualnianie widoku...'
739 function _finalize(callback){
748 base: this.doc.getBase(),
749 success: function(element){
751 var htmlView = $('#html-view');
752 htmlView.html(element);
756 error: function(text, source){
757 err = '<p class="error">Wystąpił błąd:</p><p>'+text+'</p>';
759 err += '<pre>'+source.replace(/&/g, '&').replace(/</g, '<')+'</pre>'
760 $('#html-view').html(err);
766 VisualPerspective.prototype.onExit = function(success, failure){
771 $.wiki.exitTab('#PropertiesPerspective');
774 message: 'Zapisywanie widoku...'
777 function _finalize(callback){
783 if ($('#html-view .error').length > 0)
784 return _finalize(failure);
787 element: $('#html-view').get(0),
789 success: function(text){
790 self.doc.setText(text);
793 error: function(text){
794 $('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
800 VisualPerspective.prototype.insertInlineTag = function(tag) {
803 let selection = window.getSelection();
804 var n = selection.rangeCount;
805 if (n != 1 || selection.isCollapsed) {
806 window.alert("Nie zaznaczono obszaru");
809 let range = selection.getRangeAt(0);
812 // Both ends are in the same x-node container.
813 // TODO: That the container is a inline-text container.
814 let node = range.startContainer;
815 if (node.nodeType == node.TEXT_NODE) {
816 node = node.parentNode;
818 let endNode = range.endContainer;
819 if (endNode.nodeType == endNode.TEXT_NODE) {
820 endNode = endNode.parentNode;
822 if (node != endNode) {
823 window.alert("Zły obszar.");
827 // We will construct a HTML element with the range selected.
828 let div = $("<span x-pass-thru='true'>");
830 contents = $(node).contents();
831 let startChildIndex = node == range.startContainer ? 0 : contents.index(range.startContainer);
832 let endChildIndex = contents.index(range.endContainer);
834 current = range.startContainer;
835 if (current.nodeType == current.TEXT_NODE) {
836 current = current.splitText(range.startOffset);
838 while (current != range.endContainer) {
839 n = current.nextSibling;
840 $(current).appendTo(div);
843 if (current.nodeType == current.TEXT_NODE) {
844 end = current.splitText(range.endOffset);
846 $(current).appendTo(div);
850 success: function(d) {
852 xml: d = '<' + tag + '>' + d + '</' + tag + '>',
853 success: function(html) {
855 node.insertBefore($(html)[0], end);
859 error: function(a, b) {
865 $.wiki.VisualPerspective = VisualPerspective;