plugins/gallery/tagging.js
changeset 24 7f3bd3030ae9
parent 18 c1c398349651
child 25 d9f37d2ea2cf
equal deleted inserted replaced
23:43f9c64a7672 24:7f3bd3030ae9
     7   }
     7   }
     8   image.parentNode.setAttribute('canvas:oncomplete', 'snapr_process_canvas_add(this);');
     8   image.parentNode.setAttribute('canvas:oncomplete', 'snapr_process_canvas_add(this);');
     9   image.parentNode.setAttribute('canvas:oncancel', 'obj.onclick = null;');
     9   image.parentNode.setAttribute('canvas:oncancel', 'obj.onclick = null;');
    10 }
    10 }
    11 
    11 
    12 function snapr_process_canvas_add(obj)
    12 function snapr_process_canvas_add(obj, tag_data)
    13 {
    13 {
    14   obj.onclick = null;
    14   obj.onclick = null;
    15   var abs_x = $(obj).Left() + obj.canvas.left;
    15   var abs_x = $(obj).Left() + obj.canvas.left;
    16   var abs_y = $(obj).Top()  + obj.canvas.top;
    16   var abs_y = $(obj).Top()  + obj.canvas.top;
    17   var height = obj.canvas.height + 2;
    17   var height = obj.canvas.height + 2;
    26   entry_div.appendChild(document.createElement('br'));
    26   entry_div.appendChild(document.createElement('br'));
    27   
    27   
    28   var ta = document.createElement('textarea');
    28   var ta = document.createElement('textarea');
    29   ta.rows = '7';
    29   ta.rows = '7';
    30   ta.cols = '30';
    30   ta.cols = '30';
       
    31   if ( typeof(tag_data) == 'string' )
       
    32     ta.value = tag_data;
    31   entry_div.appendChild(ta);
    33   entry_div.appendChild(ta);
    32   
    34   
    33   entry_div.appendChild(document.createElement('br'));
    35   entry_div.appendChild(document.createElement('br'));
    34   
    36   
    35   var a_add = document.createElement('a');
    37   var a_add = document.createElement('a');
    69   var parent = obj.parentNode;
    71   var parent = obj.parentNode;
    70   parent.removeChild(parent.canvas_box_obj);
    72   parent.removeChild(parent.canvas_box_obj);
    71   parent.removeChild(obj);
    73   parent.removeChild(obj);
    72   
    74   
    73   var canvas_json = toJSONString(canvas_data);
    75   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);
    76   ajaxPost(makeUrlNS('Gallery', id), 'ajax=true&act=add_tag&tag=' + ajaxEscape(tag) + '&canvas_params=' + ajaxEscape(canvas_json), snapr_process_ajax_tag_packet);
    75 }
    77 }
    76 
    78 
    77 function snapr_finalize_canvas_cancel(obj)
    79 function snapr_finalize_canvas_cancel(obj)
    78 {
    80 {
    79   var parent = obj.parentNode;
    81   var parent = obj.parentNode;
    80   parent.removeChild(parent.canvas_box_obj);
    82   parent.removeChild(parent.canvas_box_obj);
    81   parent.removeChild(obj);
    83   parent.removeChild(obj);
    82 }
    84 }
    83 
    85 
       
    86 function snapr_finalize_canvas_edit_cancel(obj)
       
    87 {
       
    88   var old_value = obj.getElementsByTagName('textarea')[0];
       
    89   old_value = old_value.OriginalValue;
       
    90   var canvas = obj.parentNode;
       
    91   var canvas_data = canvas.canvas;
       
    92   var note_id = canvas.tag_id;
       
    93   var auth_delete = canvas.auth_delete;
       
    94   var parent = canvas.parentNode;
       
    95   canvas.removeChild(obj);
       
    96   parent.removeChild(canvas);
       
    97   // redraw the note
       
    98   snapr_draw_note(parent, old_value, canvas_data, note_id, false, auth_delete);
       
    99 }
       
   100 
       
   101 function snapr_finalize_canvas_edit_delete(obj)
       
   102 {
       
   103   var old_value = obj.getElementsByTagName('textarea')[0];
       
   104   old_value = old_value.OriginalValue;
       
   105   var canvas = obj.parentNode;
       
   106   var canvas_data = canvas.canvas;
       
   107   var note_id = canvas.tag_id;
       
   108   var auth_delete = canvas.auth_delete;
       
   109   var parent = canvas.parentNode;
       
   110   canvas.removeChild(obj);
       
   111   parent.removeChild(canvas);
       
   112   // redraw the note
       
   113   var note = snapr_draw_note(parent, old_value, canvas_data, note_id, false, auth_delete);
       
   114   // now nuke it
       
   115   snapr_nuke_tag(note);
       
   116 }
       
   117 
    84 function snapr_draw_note(obj, tag, canvas_data, note_id, initial_hide, auth_delete)
   118 function snapr_draw_note(obj, tag, canvas_data, note_id, initial_hide, auth_delete)
    85 {
   119 {
    86   var newbox = canvas_create_box(obj, canvas_data.left, canvas_data.top, canvas_data.width, canvas_data.height);
   120   var newbox = canvas_create_box(obj, canvas_data.left, canvas_data.top, canvas_data.width, canvas_data.height);
    87   newbox.tag_id = note_id;
   121   newbox.tag_id = note_id;
       
   122   newbox.canvas = canvas_data;
       
   123   newbox.auth_delete = auth_delete;
    88   obj.onmouseover = function()
   124   obj.onmouseover = function()
    89   {
   125   {
    90     var boxen = this.getElementsByTagName('div');
   126     var boxen = this.getElementsByTagName('div');
    91     for ( var i = 0; i < boxen.length; i++ )
   127     for ( var i = 0; i < boxen.length; i++ )
    92       if ( boxen[i].className == 'canvasbox' )
   128       if ( boxen[i].className == 'canvasbox' )
   111     this.firstChild.style.borderColor = '#FFFFFF';
   147     this.firstChild.style.borderColor = '#FFFFFF';
   112     snapr_hide_note(this.noteObj);
   148     snapr_hide_note(this.noteObj);
   113   }
   149   }
   114   if ( auth_delete )
   150   if ( auth_delete )
   115   {
   151   {
       
   152     /*
   116     var p = document.createElement('p');
   153     var p = document.createElement('p');
   117     p.style.cssFloat = 'right';
   154     p.style.cssFloat = 'right';
   118     p.style.styleFloat = 'right';
   155     p.style.styleFloat = 'right';
   119     p.style.fontWeight = 'bold';
   156     p.style.fontWeight = 'bold';
   120     p.style.margin = '5px';
   157     p.style.margin = '5px';
   127       return false;
   164       return false;
   128     }
   165     }
   129     a_del.appendChild(document.createTextNode('[X]'));
   166     a_del.appendChild(document.createTextNode('[X]'));
   130     p.appendChild(a_del);
   167     p.appendChild(a_del);
   131     newbox.firstChild.appendChild(p);
   168     newbox.firstChild.appendChild(p);
       
   169     */
       
   170     newbox.style.cursor = 'pointer';
       
   171     newbox.onclick = function()
       
   172     {
       
   173       snapr_run_tag_editor(this);
       
   174     }
   132   }
   175   }
   133   var abs_x = $(newbox).Left();
   176   var abs_x = $(newbox).Left();
   134   var abs_y = $(newbox).Top() + $(newbox).Height() + 2;
   177   var abs_y = $(newbox).Top() + $(newbox).Height() + 2;
   135   var noteObj = document.createElement('div');
   178   var noteObj = document.createElement('div');
   136   newbox.noteObj = noteObj;
   179   newbox.noteObj = noteObj;
   137   noteObj.className = 'snapr_tag';
   180   noteObj.className = 'snapr_tag';
   138   noteObj.style.display = 'none';
   181   noteObj.style.display = 'none';
   139   noteObj.style.position = 'absolute';
   182   noteObj.style.position = 'absolute';
   140   noteObj.style.top = abs_y + 'px';
   183   noteObj.style.top = abs_y + 'px';
   141   noteObj.style.left = abs_x + 'px';
   184   noteObj.style.left = abs_x + 'px';
       
   185   noteObj.style.zIndex = '100';
   142   var re = new RegExp(unescape('%0A'), 'g');
   186   var re = new RegExp(unescape('%0A'), 'g');
   143   noteObj.innerHTML = tag.replace(re, "<br />\n");
   187   noteObj.innerHTML = tag.replace(re, "<br />\n");
   144   obj.appendChild(noteObj);
   188   obj.appendChild(noteObj);
   145   if ( initial_hide )
   189   if ( initial_hide )
   146     newbox.style.display = 'none';
   190     newbox.style.display = 'none';
       
   191   return newbox;
   147 }
   192 }
   148 
   193 
   149 function snapr_display_note(note)
   194 function snapr_display_note(note)
   150 {
   195 {
   151   //domObjChangeOpac(0, note);
   196   //domObjChangeOpac(0, note);
   170   if ( !id )
   215   if ( !id )
   171     return false;
   216     return false;
   172   ajaxPost(makeUrlNS('Gallery', id), 'ajax=true&act=del_tag&tag_id=' + obj.tag_id, snapr_process_ajax_tag_packet);
   217   ajaxPost(makeUrlNS('Gallery', id), 'ajax=true&act=del_tag&tag_id=' + obj.tag_id, snapr_process_ajax_tag_packet);
   173 }
   218 }
   174 
   219 
       
   220 function snapr_run_tag_editor(obj)
       
   221 {
       
   222   obj.onclick = null;
       
   223   var abs_x = $(obj).Left();
       
   224   var abs_y = $(obj).Top();
       
   225   var height = $(obj).Height() + 2;
       
   226   
       
   227   var value = obj.nextSibling.innerHTML;
       
   228   var regex = new RegExp('<br>', 'g');
       
   229   value = value.replace(regex, '');
       
   230   obj.parentNode.removeChild(obj.nextSibling);
       
   231   
       
   232   var entry_div = document.createElement('div');
       
   233   entry_div.className = 'snapr_tag_entry';
       
   234   entry_div.style.position = 'absolute';
       
   235   entry_div.style.top = String(height) + 'px';
       
   236   entry_div.style.left = '0px';
       
   237   entry_div.style.zIndex = '100';
       
   238   
       
   239   entry_div.appendChild(document.createTextNode('Enter a tag:'));
       
   240   entry_div.appendChild(document.createElement('br'));
       
   241   
       
   242   var ta = document.createElement('textarea');
       
   243   ta.rows = '7';
       
   244   ta.cols = '30';
       
   245   ta.value = value;
       
   246   ta.style.backgroundColor = '#FFFFFF';
       
   247   ta.style.borderWidth = '0';
       
   248   ta.style.color = '#000000';
       
   249   ta.OriginalValue = value;
       
   250   entry_div.appendChild(ta);
       
   251   
       
   252   entry_div.appendChild(document.createElement('br'));
       
   253   
       
   254   var a_add = document.createElement('a');
       
   255   a_add.href = '#';
       
   256   a_add.onclick = function()
       
   257   {
       
   258     snapr_finalize_canvas_edit(this.parentNode.parentNode, this.parentNode.getElementsByTagName('textarea')[0]['value'], this.parentNode);
       
   259     return false;
       
   260   }
       
   261   a_add.appendChild(document.createTextNode('Save tag'));
       
   262   entry_div.appendChild(a_add);
       
   263   
       
   264   entry_div.appendChild(document.createTextNode(' | '));
       
   265   
       
   266   var a_cancel = document.createElement('a');
       
   267   a_cancel.href = '#';
       
   268   a_cancel.onclick = function()
       
   269   {
       
   270     snapr_finalize_canvas_edit_cancel(this.parentNode);
       
   271     return false;
       
   272   }
       
   273   a_cancel.appendChild(document.createTextNode('Cancel'));
       
   274   entry_div.appendChild(a_cancel);
       
   275   
       
   276   entry_div.appendChild(document.createTextNode(' | '));
       
   277   
       
   278   var a_del = document.createElement('a');
       
   279   a_del.href = '#';
       
   280   a_del.onclick = function()
       
   281   {
       
   282     snapr_finalize_canvas_edit_delete(this.parentNode);
       
   283     return false;
       
   284   }
       
   285   a_del.style.color = '#FF0000';
       
   286   a_del.appendChild(document.createTextNode('Delete'));
       
   287   entry_div.appendChild(a_del);
       
   288   
       
   289   obj.appendChild(entry_div);
       
   290   ta.focus();
       
   291 }
       
   292 
       
   293 function snapr_finalize_canvas_edit(canvas, value, editor)
       
   294 {
       
   295   var canvas_data = canvas.canvas;
       
   296   var note_id = canvas.tag_id;
       
   297   var parent = canvas.parentNode;
       
   298   canvas.removeChild(editor);
       
   299   parent.removeChild(canvas);
       
   300   // send the edit across the 'net
       
   301   var parent_obj = document.getElementById('snapr_preview_img').parentNode;
       
   302   var id = parent_obj.getAttribute('snapr:imgid');
       
   303   if ( !id )
       
   304     return false;
       
   305   
       
   306   var canvas_json = toJSONString(canvas_data);
       
   307   ajaxPost(makeUrlNS('Gallery', id), 'ajax=true&act=edit_tag&tag=' + ajaxEscape(value) + '&canvas_params=' + ajaxEscape(canvas_json) + '&tag_id=' + note_id, snapr_process_ajax_tag_packet);
       
   308 }
       
   309 
   175 function snapr_process_ajax_tag_packet()
   310 function snapr_process_ajax_tag_packet()
   176 {
   311 {
   177   if ( ajax.readyState == 4 && ajax.status == 200 )
   312   if ( ajax.readyState == 4 && ajax.status == 200 )
   178   {
   313   {
   179     var response = String(ajax.responseText + '');
   314     var response = String(ajax.responseText + '');
   180     if ( response.substr(0, 1) != '[' && response.substr(0, 1) != '{' )
   315     if ( response.substr(0, 1) != '[' && response.substr(0, 1) != '{' )
   181     {
   316     {
   182       handle_invalid_json(response);
   317       new messagebox(MB_OK|MB_ICONSTOP, 'JSON response invalid', 'Received unexpected response:<pre>' + response + '</pre>');
   183       return false;
   318       return false;
   184     }
   319     }
   185     response = parseJSON(response);
   320     response = parseJSON(response);
   186     if ( response.mode )
   321     if ( response.mode )
   187     {
   322     {