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         if ($origin.is('.motyw')) {
 
 236             $('.delete-button', $overlay).click(function(){
 
 237                 if (window.confirm("Czy jesteś pewien, że chcesz usunąć ten motyw ?")) {
 
 238                     $('[theme-class=' + $origin.attr('theme-class') + ']').remove();
 
 240                     $(document).unbind('click.blur-overlay');
 
 246             $('.delete-button', $overlay).hide();
 
 250         var serializer = new XMLSerializer();
 
 255             success: function(text){
 
 256                 $('textarea', $overlay).val($.trim(text));
 
 258                 setTimeout(function(){
 
 259                     $('textarea', $overlay).elastic().focus();
 
 262                 function save(argument){
 
 263                     var nodeName = $box.attr('x-node') || 'pe';
 
 264                     var insertedText = $('textarea', $overlay).val();
 
 266                     if ($origin.is('.motyw')) {
 
 267                         insertedText = insertedText.replace(/,\s*$/, '');
 
 271                         xml: '<' + nodeName + '>' + insertedText + '</' + nodeName + '>',
 
 272                         success: function(element){
 
 273                             $origin.html($(element).html());
 
 276                         error: function(text){
 
 278                             alert('Błąd! ' + text);
 
 283                 $('.accept-button', $overlay).click(function(){
 
 287                 $(document).bind('click.blur-overlay', function(event){
 
 288                     if ($(event.target).parents('.html-editarea').length > 0) {
 
 293                     $(document).unbind('click.blur-overlay');
 
 297             error: function(text){
 
 298                 alert('Błąd! ' + text);
 
 303     function VisualPerspective(options){
 
 305         var old_callback = options.callback;
 
 307         options.callback = function(){
 
 308             var element = $("#html-view");
 
 309             var button = $('<button class="edit-button">Edytuj</button>');
 
 311             if (!CurrentDocument.readonly) {
 
 312                 $('#html-view').bind('mousemove', function(event){
 
 313                     var editable = $(event.target).closest('*[x-editable]');
 
 314                     $('.active', element).not(editable).removeClass('active').children('.edit-button').remove();
 
 316                     if (!editable.hasClass('active')) {
 
 317                         editable.addClass('active').append(button);
 
 319                     if (editable.is('.annotation-inline-box')) {
 
 320                         $('*[x-annotation-box]', editable).css({
 
 321                             position: 'absolute',
 
 322                             left: event.clientX - editable.offset().left + 5,
 
 323                             top: event.clientY - editable.offset().top + 5
 
 327                         $('*[x-annotation-box]').hide();
 
 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());
 
 348             $('.motyw').live('click', function(){
 
 349                 selectTheme($(this).attr('theme-class'));
 
 352             old_callback.call(this);
 
 355         $.wiki.Perspective.call(this, options);
 
 358     VisualPerspective.prototype = new $.wiki.Perspective();
 
 360     VisualPerspective.prototype.freezeState = function(){
 
 364     VisualPerspective.prototype.onEnter = function(success, failure){
 
 365         $.wiki.Perspective.prototype.onEnter.call(this);
 
 368             message: 'Uaktualnianie widoku...'
 
 371         function _finalize(callback){
 
 379             success: function(element){
 
 380                 $('#html-view').html(element);
 
 383             error: function(text){
 
 384                 var message = $('<pre></pre>');
 
 386                 $('#html-view').html('<p class="error">Wystąpił błąd:</p><pre>' +
 
 394     VisualPerspective.prototype.onExit = function(success, failure){
 
 398             message: 'Zapisywanie widoku...'
 
 401         function _finalize(callback){
 
 407         if ($('#html-view .error').length > 0)
 
 408             return _finalize(failure);
 
 411             element: $('#html-view div').get(0),
 
 412             success: function(text){
 
 413                 self.doc.setText(text);
 
 416             error: function(text){
 
 417                 $('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
 
 423     $.wiki.VisualPerspective = VisualPerspective;