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_FORBIDDEN = ['pt', 'pa', 'pr', 'pe', 'begin', 'end', 'theme'];
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());
95 var tag = $('<span></span>');
96 range.collapse(false);
97 range.insertNode(tag[0]);
100 xml: '<pr><slowo_obce>' + text + '</slowo_obce> --- </pr>',
101 success: function(text){
108 alert('Błąd przy dodawaniu przypisu:' + errors);
114 /* Insert theme using current selection */
117 var selection = window.getSelection();
118 var n = selection.rangeCount;
121 window.alert("Nie zaznaczono żadnego obszaru");
125 // for now allow only 1 range
127 window.alert("Zaznacz jeden obszar.");
132 // remember the selected range
133 var range = selection.getRangeAt(0);
136 if ($(range.startContainer).is('.html-editarea') ||
137 $(range.endContainer).is('.html-editarea')) {
138 window.alert("Motywy można oznaczać tylko na tekście nie otwartym do edycji. \n Zamknij edytowany fragment i spróbuj ponownie.");
142 // verify if the start/end points make even sense -
143 // they must be inside a x-node (otherwise they will be discarded)
144 // and the x-node must be a main text
145 if (!verifyTagInsertPoint(range.startContainer)) {
146 window.alert("Motyw nie może się zaczynać w tym miejscu.");
150 if (!verifyTagInsertPoint(range.endContainer)) {
151 window.alert("Motyw nie może się kończyć w tym miejscu.");
155 var date = (new Date()).getTime();
156 var random = Math.floor(4000000000 * Math.random());
157 var id = ('' + date) + '-' + ('' + random);
159 var spoint = document.createRange();
160 var epoint = document.createRange();
162 spoint.setStart(range.startContainer, range.startOffset);
163 epoint.setStart(range.endContainer, range.endOffset);
165 var mtag, btag, etag, errors;
170 xml: '<end id="e' + id + '" />',
171 success: function(text){
172 etag = $('<span></span>');
173 epoint.insertNode(etag[0]);
174 etag.replaceWith(text);
176 xml: '<motyw id="m' + id + '"></motyw>',
177 success: function(text){
178 mtag = $('<span></span>');
179 spoint.insertNode(mtag[0]);
180 mtag.replaceWith(text);
182 xml: '<begin id="b' + id + '" />',
183 success: function(text){
184 btag = $('<span></span>');
185 spoint.insertNode(btag[0])
186 btag.replaceWith(text);
187 selection.removeAllRanges();
188 openForEdit($('.motyw[theme-class=' + id + ']'));
197 /* open edition window for selected fragment */
198 function openForEdit($origin){
201 // annotations overlay their sub box - not their own box //
202 if ($origin.is(".annotation-inline-box")) {
203 $box = $("*[x-annotation-box]", $origin);
209 var x = $box[0].offsetLeft;
210 var y = $box[0].offsetTop;
211 var w = $box.outerWidth();
212 var h = $box.innerHeight();
214 if ($origin.is(".annotation-inline-box")) {
215 w = Math.max(w, 400);
219 // start edition on this node
220 var $overlay = $('<div class="html-editarea"><button class="accept-button">Zapisz</button><button class="delete-button">Usuń</button><textarea></textarea></div>').css({
221 position: 'absolute',
226 }).appendTo($box[0].offsetParent || $box.parent()).show();
228 if ($origin.is('.motyw')) {
229 $('textarea', $overlay).autocomplete(THEMES, {
236 if ($origin.is('.motyw')){
237 $('.delete-button', $overlay).click(function(){
238 if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten motyw ?")) {
239 $('[theme-class=' + $origin.attr('theme-class') + ']').remove();
241 $(document).unbind('click.blur-overlay');
246 else if($box.is('*[x-annotation-box]')) {
247 $('.delete-button', $overlay).click(function(){
248 if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten przypis?")) {
251 $(document).unbind('click.blur-overlay');
257 $('.delete-button', $overlay).hide();
261 var serializer = new XMLSerializer();
266 success: function(text){
267 $('textarea', $overlay).val($.trim(text));
269 setTimeout(function(){
270 $('textarea', $overlay).elastic().focus();
273 function save(argument){
274 var nodeName = $box.attr('x-node') || 'pe';
275 var insertedText = $('textarea', $overlay).val();
277 if ($origin.is('.motyw')) {
278 insertedText = insertedText.replace(/,\s*$/, '');
282 xml: '<' + nodeName + '>' + insertedText + '</' + nodeName + '>',
283 success: function(element){
284 $origin.html($(element).html());
287 error: function(text){
289 alert('Błąd! ' + text);
294 $('.accept-button', $overlay).click(function(){
298 $(document).bind('click.blur-overlay', function(event){
299 if ($(event.target).parents('.html-editarea').length > 0) {
304 $(document).unbind('click.blur-overlay');
308 error: function(text){
309 alert('Błąd! ' + text);
314 function VisualPerspective(options){
316 var old_callback = options.callback;
318 options.callback = function(){
319 var element = $("#html-view");
320 var button = $('<button class="edit-button">Edytuj</button>');
322 if (!CurrentDocument.readonly) {
323 $('#html-view').bind('mousemove', function(event){
324 var editable = $(event.target).closest('*[x-editable]');
325 $('.active', element).not(editable).removeClass('active').children('.edit-button').remove();
327 if (!editable.hasClass('active')) {
328 editable.addClass('active').append(button);
330 if (editable.is('.annotation-inline-box')) {
331 $('*[x-annotation-box]', editable).css({
332 position: 'absolute',
333 left: event.clientX - editable.offset().left + 5,
334 top: event.clientY - editable.offset().top + 5
338 $('*[x-annotation-box]').hide();
342 $('#insert-annotation-button').click(function(){
347 $('#insert-theme-button').click(function(){
352 $('.edit-button').live('click', function(event){
353 event.preventDefault();
354 openForEdit($(this).parent());
359 $('.motyw').live('click', function(){
360 selectTheme($(this).attr('theme-class'));
363 old_callback.call(this);
366 $.wiki.Perspective.call(this, options);
369 VisualPerspective.prototype = new $.wiki.Perspective();
371 VisualPerspective.prototype.freezeState = function(){
375 VisualPerspective.prototype.onEnter = function(success, failure){
376 $.wiki.Perspective.prototype.onEnter.call(this);
379 message: 'Uaktualnianie widoku...'
382 function _finalize(callback){
390 success: function(element){
391 $('#html-view').html(element);
394 error: function(text){
395 var message = $('<pre></pre>');
397 $('#html-view').html('<p class="error">Wystąpił błąd:</p><pre>' +
405 VisualPerspective.prototype.onExit = function(success, failure){
409 message: 'Zapisywanie widoku...'
412 function _finalize(callback){
418 if ($('#html-view .error').length > 0)
419 return _finalize(failure);
422 element: $('#html-view div').get(0),
423 success: function(text){
424 self.doc.setText(text);
427 error: function(text){
428 $('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
434 $.wiki.VisualPerspective = VisualPerspective;