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 fragment 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: '<pe><slowo_obce>' + text + '</slowo_obce> --- </pe>',
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', {
237 if ($origin.is('.motyw')){
238 $('.delete-button', $overlay).click(function(){
239 if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten motyw ?")) {
240 $('[theme-class=' + $origin.attr('theme-class') + ']').remove();
242 $(document).unbind('click.blur-overlay');
247 else if($box.is('*[x-annotation-box]')) {
248 $('.delete-button', $overlay).click(function(){
249 if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten przypis?")) {
252 $(document).unbind('click.blur-overlay');
258 $('.delete-button', $overlay).hide();
262 var serializer = new XMLSerializer();
267 success: function(text){
268 $('textarea', $overlay).val($.trim(text));
270 setTimeout(function(){
271 $('textarea', $overlay).elastic().focus();
274 function save(argument){
275 var nodeName = $box.attr('x-node') || 'pe';
276 var insertedText = $('textarea', $overlay).val();
278 if ($origin.is('.motyw')) {
279 insertedText = insertedText.replace(/,\s*$/, '');
283 xml: '<' + nodeName + '>' + insertedText + '</' + nodeName + '>',
284 success: function(element){
285 $origin.html($(element).html());
288 error: function(text){
290 alert('Błąd! ' + text);
295 $('.accept-button', $overlay).click(function(){
299 $(document).bind('click.blur-overlay', function(event){
300 if ($(event.target).parents('.html-editarea').length > 0) {
305 $(document).unbind('click.blur-overlay');
309 error: function(text){
310 alert('Błąd! ' + text);
315 function VisualPerspective(options){
317 var old_callback = options.callback;
319 options.callback = function(){
320 var element = $("#html-view");
321 var button = $('<button class="edit-button">Edytuj</button>');
323 if (!CurrentDocument.readonly) {
324 $('#html-view').bind('mousemove', function(event){
325 var editable = $(event.target).closest('*[x-editable]');
326 $('.active', element).not(editable).removeClass('active').children('.edit-button').remove();
328 if (!editable.hasClass('active')) {
329 editable.addClass('active').append(button);
331 if (editable.is('.annotation-inline-box')) {
332 $('*[x-annotation-box]', editable).css({
333 position: 'absolute',
334 left: event.clientX - editable.offset().left + 5,
335 top: event.clientY - editable.offset().top + 5
339 $('*[x-annotation-box]').hide();
343 $('#insert-annotation-button').click(function(){
348 $('#insert-theme-button').click(function(){
353 $('.edit-button').live('click', function(event){
354 event.preventDefault();
355 openForEdit($(this).parent());
360 $('.motyw').live('click', function(){
361 selectTheme($(this).attr('theme-class'));
364 old_callback.call(this);
367 $.wiki.Perspective.call(this, options);
370 VisualPerspective.prototype = new $.wiki.Perspective();
372 VisualPerspective.prototype.freezeState = function(){
376 VisualPerspective.prototype.onEnter = function(success, failure){
377 $.wiki.Perspective.prototype.onEnter.call(this);
380 message: 'Uaktualnianie widoku...'
383 function _finalize(callback){
391 success: function(element){
392 $('#html-view').html(element);
395 error: function(text){
396 var message = $('<pre></pre>');
398 $('#html-view').html('<p class="error">Wystąpił błąd:</p><pre>' +
406 VisualPerspective.prototype.onExit = function(success, failure){
410 message: 'Zapisywanie widoku...'
413 function _finalize(callback){
419 if ($('#html-view .error').length > 0)
420 return _finalize(failure);
423 element: $('#html-view div').get(0),
424 success: function(text){
425 self.doc.setText(text);
428 error: function(text){
429 $('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
435 $.wiki.VisualPerspective = VisualPerspective;