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]')) {
483 $('.delete-button', $overlay).click(function(){
484 if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten przypis?")) {
487 $(document).unbind('click.blur-overlay');
493 $('.delete-button', $overlay).html("Anuluj");
494 $('.delete-button', $overlay).click(function(){
495 if (window.confirm("Czy jesteś pewien, że chcesz anulować zmiany?")) {
497 $(document).unbind('click.blur-overlay');
504 var serializer = new XMLSerializer();
506 if($box.attr("x-edit-attribute")) {
507 source = $('<span x-pass-thru="true"/>');
508 source.text($box.attr("x-a-wl-" + $box.attr("x-edit-attribute")));
517 success: function(text){
518 $('textarea', $overlay).val($.trim(text));
520 setTimeout(function(){
521 $('textarea', $overlay).elastic().focus();
524 function save(argument){
525 var nodeName = $box.attr('x-node') || 'pe';
526 var insertedText = $('textarea', $overlay).val();
528 if ($origin.is('[x-node="motyw"]')) {
529 insertedText = insertedText.replace(/,\s*$/, '');
532 if($box.attr("x-edit-attribute")) {
533 xml = '<' + nodeName + ' ' + $box.attr("x-edit-attribute") + '="' + insertedText + '"/>';
535 xml = '<' + nodeName + '>' + insertedText + '</' + nodeName + '>';
541 success: function(element){
542 if (nodeName == 'out-of-flow-text') {
543 $(element).children().insertAfter($origin);
546 else if ($box.attr('x-edit-attribute')) {
547 $(element).insertAfter($origin);
551 $origin.html($(element).html());
555 error: function(text){
556 alert('Błąd! ' + text);
560 var msg = $("<div class='saveNotify'><p>Pamiętaj, żeby zapisać swoje zmiany.</p></div>");
561 $("#base").prepend(msg);
562 $('#base .saveNotify').fadeOut(3000, function(){
567 $('.akap-edit-button', $overlay).click(function(){
568 var textAreaOpened = $('textarea', $overlay)[0];
571 var buttonName = this.innerHTML;
573 if(buttonName == "słowo obce") {
574 startTag = "<slowo_obce>";
575 endTag = "</slowo_obce>";
576 } else if (buttonName == "wyróżnienie") {
577 startTag = "<wyroznienie>";
578 endTag = "</wyroznienie>";
579 } else if (buttonName == "tytuł dzieła") {
580 startTag = "<tytul_dziela>";
581 endTag = "</tytul_dziela>";
582 } else if(buttonName == "znak spec."){
587 var myField = textAreaOpened;
590 if (document.selection) {
591 textAreaOpened.focus();
592 sel = document.selection.createRange();
593 sel.text = startTag + sel.text + endTag;
595 //MOZILLA/NETSCAPE support
596 else if (textAreaOpened.selectionStart || textAreaOpened.selectionStart == '0') {
597 var startPos = textAreaOpened.selectionStart;
598 var endPos = textAreaOpened.selectionEnd;
599 textAreaOpened.value = textAreaOpened.value.substring(0, startPos)
600 + startTag + textAreaOpened.value.substring(startPos, endPos) + endTag + textAreaOpened.value.substring(endPos, textAreaOpened.value.length);
604 $('.accept-button', $overlay).click(function(){
606 $(document).unbind('click.blur-overlay');
609 $(document).bind('click.blur-overlay', function(event){
610 if ($(event.target).closest('.html-editarea, #specialCharsContainer').length > 0) {
614 $(document).unbind('click.blur-overlay');
618 error: function(text){
619 alert('Błąd! ' + text);
625 function VisualPerspective(options){
628 var old_callback = options.callback;
630 options.callback = function(){
631 var element = $("#html-view");
632 var button = $('<button class="edit-button">Edytuj</button>');
634 if (!CurrentDocument.readonly) {
636 $('#html-view').bind('mousemove', function(event){
637 var editable = $(event.target).closest('*[x-editable]');
638 $('.active', element).not(editable).removeClass('active').children('.edit-button').remove();
640 if (!editable.hasClass('active')) {
641 editable.addClass('active').append(button);
643 if (editable.is('.annotation-inline-box')) {
644 $('*[x-annotation-box]', editable).css({
645 // left: event.clientX - editable.offset().left + 5,
646 // top: event.clientY - editable.offset().top + 5
650 // $('*[x-annotation-box]').hide();
654 perspective.caret = new Caret(element);
656 $('#insert-reference-button').click(function(){
661 $('#insert-annotation-button').click(function(){
666 $('#insert-theme-button').click(function(){
672 $(".insert-inline-tag").click(function() {
673 perspective.insertInlineTag($(this).attr('data-tag'));
677 $(".insert-char").click(function() {
678 console.log('perspective', perspective);
679 addSymbol(caret=perspective.caret);
683 $(document).on('click', '.edit-button', function(event){
684 event.preventDefault();
685 openForEdit($(this).parent());
690 $(document).on('click', '[x-node="motyw"]', function(){
691 selectTheme($(this).attr('theme-class'));
694 element.on('click', '.annotation', function(event) {
695 event.preventDefault();
696 $('[x-annotation-box]', $(this).parent()).toggleClass('editing');
700 old_callback.call(this);
703 $.wiki.Perspective.call(this, options);
706 VisualPerspective.prototype = new $.wiki.Perspective();
708 VisualPerspective.prototype.onEnter = function(success, failure){
709 $.wiki.Perspective.prototype.onEnter.call(this);
712 message: 'Uaktualnianie widoku...'
715 function _finalize(callback){
724 base: this.doc.getBase(),
725 success: function(element){
727 var htmlView = $('#html-view');
728 htmlView.html(element);
732 error: function(text, source){
733 err = '<p class="error">Wystąpił błąd:</p><p>'+text+'</p>';
735 err += '<pre>'+source.replace(/&/g, '&').replace(/</g, '<')+'</pre>'
736 $('#html-view').html(err);
742 VisualPerspective.prototype.onExit = function(success, failure){
747 $.wiki.exitTab('#PropertiesPerspective');
750 message: 'Zapisywanie widoku...'
753 function _finalize(callback){
759 if ($('#html-view .error').length > 0)
760 return _finalize(failure);
763 element: $('#html-view').get(0),
765 success: function(text){
766 self.doc.setText(text);
769 error: function(text){
770 $('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
776 VisualPerspective.prototype.insertInlineTag = function(tag) {
779 let selection = window.getSelection();
780 var n = selection.rangeCount;
781 if (n != 1 || selection.isCollapsed) {
782 window.alert("Nie zaznaczono obszaru");
785 let range = selection.getRangeAt(0);
788 // Both ends are in the same x-node container.
789 // TODO: That the container is a inline-text container.
790 let node = range.startContainer;
791 if (node.nodeType == node.TEXT_NODE) {
792 node = node.parentNode;
794 let endNode = range.endContainer;
795 if (endNode.nodeType == endNode.TEXT_NODE) {
796 endNode = endNode.parentNode;
798 if (node != endNode) {
799 window.alert("Zły obszar.");
803 // We will construct a HTML element with the range selected.
804 let div = $("<span x-pass-thru='true'>");
806 contents = $(node).contents();
807 let startChildIndex = node == range.startContainer ? 0 : contents.index(range.startContainer);
808 let endChildIndex = contents.index(range.endContainer);
810 current = range.startContainer;
811 if (current.nodeType == current.TEXT_NODE) {
812 current = current.splitText(range.startOffset);
814 while (current != range.endContainer) {
815 n = current.nextSibling;
816 $(current).appendTo(div);
819 if (current.nodeType == current.TEXT_NODE) {
820 end = current.splitText(range.endOffset);
822 $(current).appendTo(div);
826 success: function(d) {
828 xml: d = '<' + tag + '>' + d + '</' + tag + '>',
829 success: function(html) {
831 node.insertBefore($(html)[0], end);
835 error: function(a, b) {
841 $.wiki.VisualPerspective = VisualPerspective;