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 = $(".motyw[theme-class='" + themeId + "']")[0];
10 var e = $(".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($('.motyw[theme-class="' + id + '"]'));
249 function addSymbol() {
250 if($('div.html-editarea textarea')[0]) {
251 var specialCharsContainer = $("<div id='specialCharsContainer'><a href='#' id='specialCharsClose'>Zamknij</a><table id='tableSpecialChars' style='width: 600px;'></table></div>");
253 var specialChars = [' ', 'Ą','ą','Ć','ć','Ę','ę','Ł','ł','Ń','ń','Ó','ó','Ś','ś','Ż','ż','Ź','ź','Á','á','À','à',
254 'Â','â','Ä','ä','Å','å','Ā','ā','Ă','ă','Ã','ã',
255 'Æ','æ','Ç','ç','Č','č','Ċ','ċ','Ď','ď','É','é','È','è',
256 'Ê','ê','Ë','ë','Ē','ē','Ě','ě','Ġ','ġ','Ħ','ħ','Í','í','Î','î',
257 'Ī','ī','Ĭ','ĭ','Ľ','ľ','Ñ','ñ','Ň','ň','Ó','ó','Ö','ö',
258 'Ô','ô','Ō','ō','Ǒ','ǒ','Œ','œ','Ø','ø','Ř','ř','Š',
259 'š','Ş','ş','Ť','ť','Ţ','ţ','Ű','ű','Ú','ú','Ù','ù',
260 'Ü','ü','Ů','ů','Ū','ū','Û','û','Ŭ','ŭ',
261 'Ý','ý','Ž','ž','ß','Ð','ð','Þ','þ','А','а','Б',
262 'б','В','в','Г','г','Д','д','Е','е','Ё','ё','Ж',
263 'ж','З','з','И','и','Й','й','К','к','Л','л','М',
264 'м','Н','н','О','о','П','п','Р','р','С','с',
265 'Т','т','У','у','Ф','ф','Х','х','Ц','ц','Ч',
266 'ч','Ш','ш','Щ','щ','Ъ','ъ','Ы','ы','Ь','ь','Э',
267 'э','Ю','ю','Я','я','ѓ','є','і','ї','ј','љ','њ',
268 'Ґ','ґ','Α','α','Β','β','Γ','γ','Δ','δ','Ε','ε',
269 'Ζ','ζ','Η','η','Θ','θ','Ι','ι','Κ','κ','Λ','λ','Μ',
270 'μ','Ν','ν','Ξ','ξ','Ο','ο','Π','π','Ρ','ρ','Σ','ς','σ',
271 'Τ','τ','Υ','υ','Φ','φ','Χ','χ','Ψ','ψ','Ω','ω','–',
272 '—','¡','¿','$','¢','£','€','©','®','°','¹','²','³',
273 '¼','½','¾','†','§','‰','•','←','↑','→','↓',
274 '„','”','„”','«','»','«»','»«','’','[',']','~','|','−','·',
275 '×','÷','≈','≠','±','≤','≥','∈'];
276 var tableContent = "<tr>";
278 for(var i in specialChars) {
279 if(i % 14 == 0 && i > 0) {
280 tableContent += "</tr><tr>";
282 tableContent += "<td><input type='button' class='specialBtn' value='"+specialChars[i]+"'/></td>";
285 tableContent += "</tr>";
286 $("body").append(specialCharsContainer);
289 // localStorage for recently used characters - reading
290 if (typeof(localStorage) != 'undefined') {
291 if (localStorage.getItem("recentSymbols")) {
292 var recent = localStorage.getItem("recentSymbols");
293 var recentArray = recent.split(";");
295 for(var i in recentArray.reverse()) {
296 recentRow += "<td><input type='button' class='specialBtn recentSymbol' value='"+recentArray[i]+"'/></td>";
298 recentRow = "<tr>" + recentRow + "</tr>";
301 $("#tableSpecialChars").append(recentRow);
302 $("#tableSpecialChars").append(tableContent);
306 $('.specialBtn').click(function(){
307 var editArea = $('div.html-editarea textarea')[0];
308 var insertVal = $(this).val();
310 // if we want to surround text with quotes
311 // not sure if just check if value has length == 2
313 if (insertVal.length == 2) {
314 var startTag = insertVal[0];
315 var endTag = insertVal[1];
316 var textAreaOpened = editArea;
318 if (document.selection) {
319 textAreaOpened.focus();
320 sel = document.selection.createRange();
321 sel.text = startTag + sel.text + endTag;
323 //MOZILLA/NETSCAPE support
324 else if (textAreaOpened.selectionStart || textAreaOpened.selectionStart == '0') {
325 var startPos = textAreaOpened.selectionStart;
326 var endPos = textAreaOpened.selectionEnd;
327 textAreaOpened.value = textAreaOpened.value.substring(0, startPos)
328 + startTag + textAreaOpened.value.substring(startPos, endPos) + endTag + textAreaOpened.value.substring(endPos, textAreaOpened.value.length);
331 // if we just want to insert single symbol
332 insertAtCaret(editArea, insertVal);
335 // localStorage for recently used characters - saving
336 if (typeof(localStorage) != 'undefined') {
337 if (localStorage.getItem("recentSymbols")) {
338 var recent = localStorage.getItem("recentSymbols");
339 var recentArray = recent.split(";");
340 var valIndex = $.inArray(insertVal, recentArray);
343 // value not present in array yet
344 if(recentArray.length > 13){
346 recentArray.push(insertVal);
348 recentArray.push(insertVal);
351 // value already in the array
352 for(var i = valIndex; i < recentArray.length; i++){
353 recentArray[i] = recentArray[i+1];
355 recentArray[recentArray.length-1] = insertVal;
357 localStorage.setItem("recentSymbols", recentArray.join(";"));
359 localStorage.setItem("recentSymbols", insertVal);
362 $(specialCharsContainer).remove();
364 $('#specialCharsClose').click(function(){
365 $(specialCharsContainer).remove();
369 window.alert('Najedź na fragment tekstu, wybierz "Edytuj" i ustaw kursor na miejscu gdzie chcesz wstawić symbol.');
373 function insertAtCaret(txtarea,text) {
374 /* http://www.scottklarr.com/topic/425/how-to-insert-text-into-a-textarea-where-the-cursor-is/ */
375 var scrollPos = txtarea.scrollTop;
378 var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) );
381 var range = document.selection.createRange();
382 range.moveStart ('character', -txtarea.value.length);
383 strPos = backStart = range.text.length;
384 } else if (br == "ff") {
385 strPos = txtarea.selectionStart;
386 backStart = txtarea.selectionEnd;
388 var front = (txtarea.value).substring(0,strPos);
389 var back = (txtarea.value).substring(backStart,txtarea.value.length);
390 txtarea.value=front+text+back;
391 strPos = strPos + text.length;
394 var range = document.selection.createRange();
395 range.moveStart ('character', -txtarea.value.length);
396 range.moveStart ('character', strPos);
397 range.moveEnd ('character', 0);
399 } else if (br == "ff") {
400 txtarea.selectionStart = strPos;
401 txtarea.selectionEnd = strPos;
404 txtarea.scrollTop = scrollPos;
407 /* open edition window for selected fragment */
408 function openForEdit($origin){
411 // annotations overlay their sub box - not their own box //
412 if ($origin.is(".annotation-inline-box")) {
413 $box = $("*[x-annotation-box]", $origin);
418 var x = $box[0].offsetLeft;
419 var y = $box[0].offsetTop;
421 var w = $box.outerWidth();
422 var h = $box.innerHeight();
424 if ($origin.is(".annotation-inline-box")) {
425 w = Math.max(w, 400);
427 if($('.htmlview div').offset().left + $('.htmlview div').width() > ($('.vsplitbar').offset().left - 480)){
428 x = -(Math.max($origin.offset().left, $origin.width()));
433 if ($origin.is('.reference-inline-box')) {
439 $('.htmlview div').offset().left + $('.htmlview div').width() - 400
443 // start edition on this node
444 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({
445 position: 'absolute',
450 }).appendTo($box[0].offsetParent || $box.parent()).show();
453 if ($origin.is('*[x-edit-no-format]')) {
454 $('.akap-edit-button').remove();
457 if ($origin.is('.motyw')) {
458 $.themes.autocomplete($('textarea', $overlay));
461 if ($origin.is('.motyw')){
462 $('.delete-button', $overlay).click(function(){
463 if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten motyw?")) {
464 $('[theme-class="' + $origin.attr('theme-class') + '"]').remove();
466 $(document).unbind('click.blur-overlay');
471 else if($box.is('*[x-annotation-box]') || $origin.is('*[x-edit-attribute]')) {
472 $('.delete-button', $overlay).click(function(){
473 if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten przypis?")) {
476 $(document).unbind('click.blur-overlay');
482 $('.delete-button', $overlay).html("Anuluj");
483 $('.delete-button', $overlay).click(function(){
484 if (window.confirm("Czy jesteś pewien, że chcesz anulować zmiany?")) {
486 $(document).unbind('click.blur-overlay');
493 var serializer = new XMLSerializer();
495 if($box.attr("x-edit-attribute")) {
496 source = $('<span x-pass-thru="true"/>');
497 source.text($box.attr("data-wlf-" + $box.attr("x-edit-attribute")));
506 success: function(text){
507 $('textarea', $overlay).val($.trim(text));
509 setTimeout(function(){
510 $('textarea', $overlay).elastic().focus();
513 function save(argument){
514 var nodeName = $box.attr('x-node') || 'pe';
515 var insertedText = $('textarea', $overlay).val();
517 if ($origin.is('.motyw')) {
518 insertedText = insertedText.replace(/,\s*$/, '');
521 if($box.attr("x-edit-attribute")) {
522 xml = '<' + nodeName + ' ' + $box.attr("x-edit-attribute") + '="' + insertedText + '"/>';
524 xml = '<' + nodeName + '>' + insertedText + '</' + nodeName + '>';
530 success: function(element){
531 if (nodeName == 'out-of-flow-text') {
532 $(element).children().insertAfter($origin);
535 else if ($box.attr('x-edit-attribute')) {
536 $(element).insertAfter($origin);
540 $origin.html($(element).html());
544 error: function(text){
545 alert('Błąd! ' + text);
549 var msg = $("<div class='saveNotify'><p>Pamiętaj, żeby zapisać swoje zmiany.</p></div>");
550 $("#base").prepend(msg);
551 $('#base .saveNotify').fadeOut(3000, function(){
556 $('.akap-edit-button', $overlay).click(function(){
557 var textAreaOpened = $('textarea', $overlay)[0];
560 var buttonName = this.innerHTML;
562 if(buttonName == "słowo obce") {
563 startTag = "<slowo_obce>";
564 endTag = "</slowo_obce>";
565 } else if (buttonName == "wyróżnienie") {
566 startTag = "<wyroznienie>";
567 endTag = "</wyroznienie>";
568 } else if (buttonName == "tytuł dzieła") {
569 startTag = "<tytul_dziela>";
570 endTag = "</tytul_dziela>";
571 } else if(buttonName == "znak spec."){
576 var myField = textAreaOpened;
579 if (document.selection) {
580 textAreaOpened.focus();
581 sel = document.selection.createRange();
582 sel.text = startTag + sel.text + endTag;
584 //MOZILLA/NETSCAPE support
585 else if (textAreaOpened.selectionStart || textAreaOpened.selectionStart == '0') {
586 var startPos = textAreaOpened.selectionStart;
587 var endPos = textAreaOpened.selectionEnd;
588 textAreaOpened.value = textAreaOpened.value.substring(0, startPos)
589 + startTag + textAreaOpened.value.substring(startPos, endPos) + endTag + textAreaOpened.value.substring(endPos, textAreaOpened.value.length);
593 $('.accept-button', $overlay).click(function(){
595 $(document).unbind('click.blur-overlay');
598 $(document).bind('click.blur-overlay', function(event){
599 if ($(event.target).closest('.html-editarea, #specialCharsContainer').length > 0) {
603 $(document).unbind('click.blur-overlay');
607 error: function(text){
608 alert('Błąd! ' + text);
614 function VisualPerspective(options){
616 var old_callback = options.callback;
618 options.callback = function(){
619 var element = $("#html-view");
620 var button = $('<button class="edit-button">Edytuj</button>');
622 if (!CurrentDocument.readonly) {
623 $('#html-view').bind('mousemove', function(event){
624 var editable = $(event.target).closest('*[x-editable]');
625 $('.active', element).not(editable).removeClass('active').children('.edit-button').remove();
627 if (!editable.hasClass('active')) {
628 editable.addClass('active').append(button);
630 if (editable.is('.annotation-inline-box')) {
631 $('*[x-annotation-box]', editable).css({
632 position: 'absolute',
633 left: event.clientX - editable.offset().left + 5,
634 top: event.clientY - editable.offset().top + 5
638 $('*[x-annotation-box]').hide();
642 $('#insert-reference-button').click(function(){
647 $('#insert-annotation-button').click(function(){
652 $('#insert-theme-button').click(function(){
657 $(document).on('click', '.edit-button', function(event){
658 event.preventDefault();
659 openForEdit($(this).parent());
664 $(document).on('click', '.motyw', function(){
665 selectTheme($(this).attr('theme-class'));
668 old_callback.call(this);
671 $.wiki.Perspective.call(this, options);
674 VisualPerspective.prototype = new $.wiki.Perspective();
676 VisualPerspective.prototype.freezeState = function(){
680 VisualPerspective.prototype.onEnter = function(success, failure){
681 $.wiki.Perspective.prototype.onEnter.call(this);
684 message: 'Uaktualnianie widoku...'
687 function _finalize(callback){
695 base: this.doc.getBase(),
696 success: function(element){
697 var htmlView = $('#html-view');
698 htmlView.html(element);
699 htmlView.find('*[x-node]').dblclick(function(e) {
700 if($(e.target).is('textarea'))
702 var selection = window.getSelection();
703 selection.collapseToStart();
704 selection.modify('extend', 'forward', 'word');
709 error: function(text, source){
710 err = '<p class="error">Wystąpił błąd:</p><p>'+text+'</p>';
712 err += '<pre>'+source.replace(/&/g, '&').replace(/</g, '<')+'</pre>'
713 $('#html-view').html(err);
719 VisualPerspective.prototype.onExit = function(success, failure){
723 message: 'Zapisywanie widoku...'
726 function _finalize(callback){
732 if ($('#html-view .error').length > 0)
733 return _finalize(failure);
736 element: $('#html-view').get(0),
738 success: function(text){
739 self.doc.setText(text);
742 error: function(text){
743 $('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
749 $.wiki.VisualPerspective = VisualPerspective;