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.is('*[x-annotation-box] *'))
46 /* Convert HTML frament to plaintext */
47 var ANNOT_ALLOWED = ['wyroznienie', 'slowo_obce', 'osoba'];
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_ALLOWED) != -1) {
58 text += html2plainText(this);
66 /* Insert annotation using current selection */
67 function addAnnotation(){
68 var selection = window.getSelection();
69 var n = selection.rangeCount;
72 window.alert("Nie zaznaczono żadnego obszaru");
76 // for now allow only 1 range
78 window.alert("Zaznacz jeden obszar");
82 // remember the selected range
83 var range = selection.getRangeAt(0);
85 if (!verifyTagInsertPoint(range.endContainer)) {
86 window.alert("Nie można wstawić w to miejsce przypisu.");
90 // BUG #273 - selected text can contain themes, which should be omited from
92 var text = html2plainText(range.cloneContents());
94 var tag = $('<span></span>');
95 range.collapse(false);
96 range.insertNode(tag[0]);
99 xml: '<pr><slowo_obce>' + text + '</slowo_obce> --- </pr>',
100 success: function(text){
107 alert('Błąd przy dodawaniu przypisu:' + errors);
113 /* Insert theme using current selection */
116 var selection = window.getSelection();
117 var n = selection.rangeCount;
120 window.alert("Nie zaznaczono żadnego obszaru");
124 // for now allow only 1 range
126 window.alert("Zaznacz jeden obszar.");
131 // remember the selected range
132 var range = selection.getRangeAt(0);
135 if ($(range.startContainer).is('.html-editarea') ||
136 $(range.endContainer).is('.html-editarea')) {
137 window.alert("Motywy można oznaczać tylko na tekście nie otwartym do edycji. \n Zamknij edytowany fragment i spróbuj ponownie.");
141 // verify if the start/end points make even sense -
142 // they must be inside a x-node (otherwise they will be discarded)
143 // and the x-node must be a main text
144 if (!verifyTagInsertPoint(range.startContainer)) {
145 window.alert("Motyw nie może się zaczynać w tym miejscu.");
149 if (!verifyTagInsertPoint(range.endContainer)) {
150 window.alert("Motyw nie może się kończyć w tym miejscu.");
154 var date = (new Date()).getTime();
155 var random = Math.floor(4000000000 * Math.random());
156 var id = ('' + date) + '-' + ('' + random);
158 var spoint = document.createRange();
159 var epoint = document.createRange();
161 spoint.setStart(range.startContainer, range.startOffset);
162 epoint.setStart(range.endContainer, range.endOffset);
164 var mtag, btag, etag, errors;
169 xml: '<end id="e' + id + '" />',
170 success: function(text){
171 etag = $('<span></span>');
172 epoint.insertNode(etag[0]);
173 etag.replaceWith(text);
175 xml: '<motyw id="m' + id + '"></motyw>',
176 success: function(text){
177 mtag = $('<span></span>');
178 spoint.insertNode(mtag[0]);
179 mtag.replaceWith(text);
181 xml: '<begin id="b' + id + '" />',
182 success: function(text){
183 btag = $('<span></span>');
184 spoint.insertNode(btag[0])
185 btag.replaceWith(text);
186 selection.removeAllRanges();
187 openForEdit($('.motyw[theme-class=' + id + ']'));
196 /* open edition window for selected fragment */
197 function openForEdit($origin){
200 // annotations overlay their sub box - not their own box //
201 if ($origin.is(".annotation-inline-box")) {
202 $box = $("*[x-annotation-box]", $origin);
208 var x = $box[0].offsetLeft;
209 var y = $box[0].offsetTop;
210 var w = $box.outerWidth();
211 var h = $box.innerHeight();
213 if ($origin.is(".annotation-inline-box")) {
214 w = Math.max(w, 400);
218 // start edition on this node
219 var $overlay = $('<div class="html-editarea"><button class="accept-button">Zapisz</button><button class="delete-button">Usuń</button><textarea></textarea></div>').css({
220 position: 'absolute',
225 }).appendTo($box[0].offsetParent || $box.parent()).show();
227 if ($origin.is('.motyw')) {
228 $('textarea', $overlay).autocomplete(THEMES, {
235 $('.delete-button', $overlay).click(function(){
236 if ($origin.is('.motyw')) {
237 $('[theme-class=' + $origin.attr('theme-class') + ']').remove();
243 $(document).unbind('click.blur-overlay');
248 var serializer = new XMLSerializer();
253 success: function(text){
254 $('textarea', $overlay).val($.trim(text));
256 setTimeout(function(){
257 $('textarea', $overlay).elastic().focus();
260 function save(argument){
261 var nodeName = $box.attr('x-node') || 'pe';
262 var insertedText = $('textarea', $overlay).val();
264 if ($origin.is('.motyw')) {
265 insertedText = insertedText.replace(/,\s*$/, '');
269 xml: '<' + nodeName + '>' + insertedText + '</' + nodeName + '>',
270 success: function(element){
271 $origin.html($(element).html());
274 error: function(text){
276 alert('Błąd! ' + text);
281 $('.accept-button', $overlay).click(function(){
285 $(document).bind('click.blur-overlay', function(event){
286 if ($(event.target).parents('.html-editarea').length > 0) {
291 $(document).unbind('click.blur-overlay');
295 error: function(text){
296 alert('Błąd! ' + text);
301 function VisualPerspective(doc, callback)
303 this.perspective_id = 'VisualPerspective';
306 var element = $("html-view");
307 var button = $('<button class="edit-button">Edytuj</button>');
309 $('#html-view').bind('mousemove', function(event){
310 var editable = $(event.target).closest('*[x-editable]');
311 $('.active[x-editable]', element).not(editable).removeClass('active').children('.edit-button').remove();
312 if (!editable.hasClass('active')) {
313 editable.addClass('active').append(button);
315 if (editable.is('.annotation-inline-box')) {
316 $('*[x-annotation-box]', editable).css({
317 position: 'absolute',
318 left: event.clientX - editable.offset().left + 5,
319 top: event.clientY - editable.offset().top + 5
323 $('*[x-annotation-box]').hide();
327 $('.motyw').live('click', function(){
328 selectTheme($(this).attr('theme-class'));
331 $('#insert-annotation-button').click(function(){
336 $('#insert-theme-button').click(function(){
341 $('.edit-button').live('click', function(event){
342 event.preventDefault();
343 openForEdit($(this).parent());
349 VisualPerspective.prototype = new $.wiki.Perspective();
351 VisualPerspective.prototype.freezeState = function() {
355 VisualPerspective.prototype.onEnter = function(success, failure)
357 $.wiki.Perspective.prototype.onEnter.call(this);
360 message: 'Uaktualnianie widoku...'
363 function _finalize(callback) {
365 if (callback) callback();
370 success: function(element){
371 $('#html-view').html(element);
374 error: function(text) {
375 var message = $('<pre></pre>');
377 $('#html-view').html('<p class="error">Wystąpił błąd:</p><pre>' +
385 VisualPerspective.prototype.onExit = function(success, failure)
390 message: 'Zapisywanie widoku...'
393 function _finalize(callback) {
395 if (callback) callback();
398 if ($('#html-view .error').length > 0)
399 return _finalize(failure);
402 element: $('#html-view div').get(0),
403 success: function(text) {
404 self.doc.setText(text);
407 error: function(text) {
408 $('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
414 $.wiki.VisualPerspective = VisualPerspective;