plugins/gallery/tagging.js
changeset 18 c1c398349651
child 24 7f3bd3030ae9
equal deleted inserted replaced
15:ac7d3dedcc44 18:c1c398349651
       
     1 function snapr_add_tag()
       
     2 {
       
     3   var image = document.getElementById('snapr_preview_img');
       
     4   image.parentNode.onclick = function(e)
       
     5   {
       
     6     canvas_click(this);
       
     7   }
       
     8   image.parentNode.setAttribute('canvas:oncomplete', 'snapr_process_canvas_add(this);');
       
     9   image.parentNode.setAttribute('canvas:oncancel', 'obj.onclick = null;');
       
    10 }
       
    11 
       
    12 function snapr_process_canvas_add(obj)
       
    13 {
       
    14   obj.onclick = null;
       
    15   var abs_x = $(obj).Left() + obj.canvas.left;
       
    16   var abs_y = $(obj).Top()  + obj.canvas.top;
       
    17   var height = obj.canvas.height + 2;
       
    18   
       
    19   var entry_div = document.createElement('div');
       
    20   entry_div.className = 'snapr_tag_entry';
       
    21   entry_div.style.position = 'absolute';
       
    22   entry_div.style.top = String(abs_y + height) + 'px';
       
    23   entry_div.style.left = String(abs_x)+ 'px';
       
    24   
       
    25   entry_div.appendChild(document.createTextNode('Enter a tag:'));
       
    26   entry_div.appendChild(document.createElement('br'));
       
    27   
       
    28   var ta = document.createElement('textarea');
       
    29   ta.rows = '7';
       
    30   ta.cols = '30';
       
    31   entry_div.appendChild(ta);
       
    32   
       
    33   entry_div.appendChild(document.createElement('br'));
       
    34   
       
    35   var a_add = document.createElement('a');
       
    36   a_add.href = '#';
       
    37   a_add.onclick = function()
       
    38   {
       
    39     snapr_finalize_canvas_add(this.parentNode, this.parentNode.parentNode.canvas, this.previousSibling.previousSibling.value);
       
    40     return false;
       
    41   }
       
    42   a_add.appendChild(document.createTextNode('Add tag'));
       
    43   entry_div.appendChild(a_add);
       
    44   
       
    45   entry_div.appendChild(document.createTextNode(' | '));
       
    46   
       
    47   var a_cancel = document.createElement('a');
       
    48   a_cancel.href = '#';
       
    49   a_cancel.onclick = function()
       
    50   {
       
    51     snapr_finalize_canvas_cancel(this.parentNode);
       
    52     return false;
       
    53   }
       
    54   a_cancel.appendChild(document.createTextNode('Cancel'));
       
    55   entry_div.appendChild(a_cancel);
       
    56   
       
    57   obj.appendChild(entry_div);
       
    58   ta.focus();
       
    59 }
       
    60 
       
    61 function snapr_finalize_canvas_add(obj, canvas_data, tag)
       
    62 {
       
    63   // add the new box
       
    64   var id = obj.parentNode.getAttribute('snapr:imgid');
       
    65   if ( !id )
       
    66     return false;
       
    67   
       
    68   // destroy form, etc.
       
    69   var parent = obj.parentNode;
       
    70   parent.removeChild(parent.canvas_box_obj);
       
    71   parent.removeChild(obj);
       
    72   
       
    73   var canvas_json = toJSONString(canvas_data);
       
    74   ajaxPost(makeUrlNS('Gallery', id), 'ajax=true&act=add_tag&tag=' + escape(tag) + '&canvas_params=' + escape(canvas_json), snapr_process_ajax_tag_packet);
       
    75 }
       
    76 
       
    77 function snapr_finalize_canvas_cancel(obj)
       
    78 {
       
    79   var parent = obj.parentNode;
       
    80   parent.removeChild(parent.canvas_box_obj);
       
    81   parent.removeChild(obj);
       
    82 }
       
    83 
       
    84 function snapr_draw_note(obj, tag, canvas_data, note_id, initial_hide, auth_delete)
       
    85 {
       
    86   var newbox = canvas_create_box(obj, canvas_data.left, canvas_data.top, canvas_data.width, canvas_data.height);
       
    87   newbox.tag_id = note_id;
       
    88   obj.onmouseover = function()
       
    89   {
       
    90     var boxen = this.getElementsByTagName('div');
       
    91     for ( var i = 0; i < boxen.length; i++ )
       
    92       if ( boxen[i].className == 'canvasbox' )
       
    93         boxen[i].style.display = 'block';
       
    94   }
       
    95   obj.onmouseout = function()
       
    96   {
       
    97     var boxen = this.getElementsByTagName('div');
       
    98     for ( var i = 0; i < boxen.length; i++ )
       
    99       if ( boxen[i].className == 'canvasbox' )
       
   100         boxen[i].style.display = 'none';
       
   101   }
       
   102   newbox.onmouseover = function()
       
   103   {
       
   104     this.style.borderColor = '#FFFF00';
       
   105     this.firstChild.style.borderColor = '#000000';
       
   106     snapr_display_note(this.noteObj);
       
   107   }
       
   108   newbox.onmouseout = function()
       
   109   {
       
   110     this.style.borderColor = '#000000';
       
   111     this.firstChild.style.borderColor = '#FFFFFF';
       
   112     snapr_hide_note(this.noteObj);
       
   113   }
       
   114   if ( auth_delete )
       
   115   {
       
   116     var p = document.createElement('p');
       
   117     p.style.cssFloat = 'right';
       
   118     p.style.styleFloat = 'right';
       
   119     p.style.fontWeight = 'bold';
       
   120     p.style.margin = '5px';
       
   121     var a_del = document.createElement('a');
       
   122     a_del.style.color = '#FF0000';
       
   123     a_del.href = '#';
       
   124     a_del.onclick = function()
       
   125     {
       
   126       snapr_nuke_tag(this.parentNode.parentNode.parentNode);
       
   127       return false;
       
   128     }
       
   129     a_del.appendChild(document.createTextNode('[X]'));
       
   130     p.appendChild(a_del);
       
   131     newbox.firstChild.appendChild(p);
       
   132   }
       
   133   var abs_x = $(newbox).Left();
       
   134   var abs_y = $(newbox).Top() + $(newbox).Height() + 2;
       
   135   var noteObj = document.createElement('div');
       
   136   newbox.noteObj = noteObj;
       
   137   noteObj.className = 'snapr_tag';
       
   138   noteObj.style.display = 'none';
       
   139   noteObj.style.position = 'absolute';
       
   140   noteObj.style.top = abs_y + 'px';
       
   141   noteObj.style.left = abs_x + 'px';
       
   142   var re = new RegExp(unescape('%0A'), 'g');
       
   143   noteObj.innerHTML = tag.replace(re, "<br />\n");
       
   144   obj.appendChild(noteObj);
       
   145   if ( initial_hide )
       
   146     newbox.style.display = 'none';
       
   147 }
       
   148 
       
   149 function snapr_display_note(note)
       
   150 {
       
   151   //domObjChangeOpac(0, note);
       
   152   note.style.display = 'block';
       
   153   //domOpacity(note, 0, 100, 500);
       
   154 }
       
   155 
       
   156 function snapr_hide_note(note)
       
   157 {
       
   158   //domOpacity(note, 100, 0, 500);
       
   159   //setTimeout(function()
       
   160   //  {
       
   161       note.style.display = 'none';
       
   162   //  }, 600);
       
   163 }
       
   164 
       
   165 function snapr_nuke_tag(obj)
       
   166 {
       
   167   // add the new box
       
   168   var parent_obj = document.getElementById('snapr_preview_img').parentNode;
       
   169   var id = parent_obj.getAttribute('snapr:imgid');
       
   170   if ( !id )
       
   171     return false;
       
   172   ajaxPost(makeUrlNS('Gallery', id), 'ajax=true&act=del_tag&tag_id=' + obj.tag_id, snapr_process_ajax_tag_packet);
       
   173 }
       
   174 
       
   175 function snapr_process_ajax_tag_packet()
       
   176 {
       
   177   if ( ajax.readyState == 4 && ajax.status == 200 )
       
   178   {
       
   179     var response = String(ajax.responseText + '');
       
   180     if ( response.substr(0, 1) != '[' && response.substr(0, 1) != '{' )
       
   181     {
       
   182       handle_invalid_json(response);
       
   183       return false;
       
   184     }
       
   185     response = parseJSON(response);
       
   186     if ( response.mode )
       
   187     {
       
   188       if ( response.mode == 'error' )
       
   189       {
       
   190         alert(response.error);
       
   191         return false;
       
   192       }
       
   193     }
       
   194     var parent_obj = document.getElementById('snapr_preview_img').parentNode;
       
   195     for ( var i = 0; i < response.length; i++ )
       
   196     {
       
   197       var packet = response[i];
       
   198       switch(packet.mode)
       
   199       {
       
   200         case 'add':
       
   201           snapr_draw_note(parent_obj, packet.tag, packet.canvas_data, packet.note_id, packet.initial_hide, packet.auth_delete);
       
   202           break;
       
   203         case 'remove':
       
   204           // Server requested to remove a tag
       
   205           var divs = parent_obj.getElementsByTagName('div');
       
   206           for ( var i = 0; i < divs.length; i++ )
       
   207           {
       
   208             var box = divs[i];
       
   209             if ( box.className == 'canvasbox' )
       
   210             {
       
   211               if ( box.tag_id == packet.note_id )
       
   212               {
       
   213                 // You. We have orders to shoot. Stand in front of wall.
       
   214                 var sibling = box.nextSibling;
       
   215                 var parent = box.parentNode;
       
   216                 // BLAM.
       
   217                 parent.removeChild(sibling);
       
   218                 parent.removeChild(box);
       
   219                 break;
       
   220               }
       
   221             }
       
   222           }
       
   223           break;
       
   224       }
       
   225     }
       
   226   }
       
   227 }
       
   228 
       
   229 var snapr_tags_onload = function()
       
   230 {
       
   231   // add the new box
       
   232   var parent_obj = document.getElementById('snapr_preview_img').parentNode;
       
   233   var id = parent_obj.getAttribute('snapr:imgid');
       
   234   if ( !id )
       
   235     return false;
       
   236   ajaxPost(makeUrlNS('Gallery', id), 'ajax=true&act=get_tags', snapr_process_ajax_tag_packet);
       
   237 }
       
   238 
       
   239 addOnloadHook(snapr_tags_onload);
       
   240