includes/clientside/static/ajax.js
changeset 80 cb7dde69c301
parent 78 4df25dfdde63
child 87 570f68c3fe36
equal deleted inserted replaced
79:5faff33a6580 80:cb7dde69c301
   891           {
   891           {
   892             catbox.appendChild(document.createTextNode(' '));
   892             catbox.appendChild(document.createTextNode(' '));
   893             var a = document.createElement('a');
   893             var a = document.createElement('a');
   894             a.appendChild(document.createTextNode('[X]'));
   894             a.appendChild(document.createTextNode('[X]'));
   895             a.href = '#';
   895             a.href = '#';
   896             a.onclick = function() { return false; }
   896             a._js_tag_id = json.tags[i].id;
       
   897             a.onclick = function() { ajaxDeleteTag(this, this._js_tag_id); return false; }
   897             catbox.appendChild(a);
   898             catbox.appendChild(a);
   898           }
   899           }
   899           if ( ( i + 1 ) < json.tags.length )
   900           if ( ( i + 1 ) < json.tags.length )
   900             catbox.appendChild(document.createTextNode(', '));
   901             catbox.appendChild(document.createTextNode(', '));
   901         }
   902         }
   910         }
   911         }
   911       }
   912       }
   912     });
   913     });
   913 }
   914 }
   914 
   915 
       
   916 var addtag_open = false;
       
   917 
   915 function ajaxAddTagStage1()
   918 function ajaxAddTagStage1()
   916 {
   919 {
       
   920   if ( addtag_open )
       
   921     return false;
   917   var catbox = document.getElementById('mdgCatBox');
   922   var catbox = document.getElementById('mdgCatBox');
   918   var adddiv = document.createElement('div');
   923   var adddiv = document.createElement('div');
   919   var text = document.createElement('input');
   924   var text = document.createElement('input');
   920   var addlink = document.createElement('a');
   925   var addlink = document.createElement('a');
   921   addlink.href = '#';
   926   addlink.href = '#';
   922   addlink.onclick = function() { return false; };
   927   addlink.onclick = function() { ajaxAddTagStage2(this.parentNode.firstChild.nextSibling.value, this.parentNode); return false; };
   923   addlink.appendChild(document.createTextNode('+ Add'));
   928   addlink.appendChild(document.createTextNode('+ Add'));
   924   text.type = 'text';
   929   text.type = 'text';
   925   text.size = '15';
   930   text.size = '15';
       
   931   text.onkeyup = function(e)
       
   932   {
       
   933     if ( e.keyCode == 13 )
       
   934     {
       
   935       ajaxAddTagStage2(this.value, this.parentNode);
       
   936     }
       
   937   }
   926   
   938   
   927   adddiv.style.margin = '5px 0 0 0';
   939   adddiv.style.margin = '5px 0 0 0';
   928   adddiv.appendChild(document.createTextNode('Add a tag: '));
   940   adddiv.appendChild(document.createTextNode('Add a tag: '));
   929   adddiv.appendChild(text);
   941   adddiv.appendChild(text);
   930   adddiv.appendChild(document.createTextNode(' '));
   942   adddiv.appendChild(document.createTextNode(' '));
   931   adddiv.appendChild(addlink);
   943   adddiv.appendChild(addlink);
   932   catbox.appendChild(adddiv);
   944   catbox.appendChild(adddiv);
       
   945   addtag_open = true;
       
   946 }
       
   947 
       
   948 var addtag_nukeme = false;
       
   949 
       
   950 function ajaxAddTagStage2(tag, nukeme)
       
   951 {
       
   952   if ( !addtag_open )
       
   953     return false;
       
   954   if ( addtag_nukeme )
       
   955     return false;
       
   956   addtag_nukeme = nukeme;
       
   957   tag = ajaxEscape(tag);
       
   958   setAjaxLoading();
       
   959   ajaxPost(stdAjaxPrefix + '&_mode=addtag', 'tag=' + tag, function()
       
   960     {
       
   961       if ( ajax.readyState == 4 )
       
   962       {
       
   963         unsetAjaxLoading();
       
   964         var nukeme = addtag_nukeme;
       
   965         addtag_nukeme = false;
       
   966         var resptext = String(ajax.responseText + ' ');
       
   967         resptext = resptext.substr(0, resptext.length-1);
       
   968         if ( resptext.substr(0, 1) != '{' )
       
   969         {
       
   970           alert('Invalid JSON response from server:\n' + resptext);
       
   971           return false;
       
   972         }
       
   973         var json = parseJSON(resptext);
       
   974         var parent = nukeme.parentNode;
       
   975         parent.removeChild(nukeme);
       
   976         addtag_open = false;
       
   977         if ( json.success )
       
   978         {
       
   979           var node = parent.childNodes[1];
       
   980           var insertafter = false;
       
   981           var nukeafter = false;
       
   982           if ( node.nodeValue == 'No tags on this page' )
       
   983           {
       
   984             nukeafter = true;
       
   985           }
       
   986           insertafter = parent.childNodes[ parent.childNodes.length - 3 ];
       
   987           // these need to be inserted in reverse order
       
   988           if ( json.can_del )
       
   989           {
       
   990             var a = document.createElement('a');
       
   991             a.appendChild(document.createTextNode('[X]'));
       
   992             a.href = '#';
       
   993             a._js_tag_id = json.tag_id;
       
   994             a.onclick = function() { ajaxDeleteTag(this, this._js_tag_id); return false; }
       
   995             insertAfter(parent, a, insertafter);
       
   996             insertAfter(parent, document.createTextNode(' '), insertafter);
       
   997           }
       
   998           insertAfter(parent, document.createTextNode(json.tag), insertafter);
       
   999           if ( !nukeafter )
       
  1000           {
       
  1001             insertAfter(parent, document.createTextNode(', '), insertafter);
       
  1002           }
       
  1003           if ( nukeafter )
       
  1004           {
       
  1005             parent.removeChild(insertafter);
       
  1006           }
       
  1007         }
       
  1008         else
       
  1009         {
       
  1010           alert(json.error);
       
  1011         }
       
  1012       }
       
  1013     });
       
  1014 }
       
  1015 
       
  1016 function ajaxDeleteTag(parentobj, tag_id)
       
  1017 {
       
  1018   var arrDelete = [ parentobj, parentobj.previousSibling, parentobj.previousSibling.previousSibling ];
       
  1019   var parent = parentobj.parentNode;
       
  1020   var writeNoTags = false;
       
  1021   if ( parentobj.previousSibling.previousSibling.previousSibling.nodeValue == ', ' )
       
  1022     arrDelete.push(parentobj.previousSibling.previousSibling.previousSibling);
       
  1023   else if ( parentobj.previousSibling.previousSibling.previousSibling.nodeValue == 'Page tags: ' )
       
  1024     arrDelete.push(parentobj.nextSibling);
       
  1025   
       
  1026   if ( parentobj.previousSibling.previousSibling.previousSibling.nodeValue == 'Page tags: ' &&
       
  1027        parentobj.nextSibling.nextSibling.firstChild )
       
  1028     if ( parentobj.nextSibling.nextSibling.firstChild.nodeValue == '(add a tag)')
       
  1029       writeNoTags = true;
       
  1030     
       
  1031   ajaxPost(stdAjaxPrefix + '&_mode=deltag', 'tag_id=' + String(tag_id), function()
       
  1032     {
       
  1033       if ( ajax.readyState == 4 )
       
  1034       {
       
  1035         if ( ajax.responseText == 'success' )
       
  1036         {
       
  1037           for ( var i = 0; i < arrDelete.length; i++ )
       
  1038           {
       
  1039             try
       
  1040             {
       
  1041               parent.removeChild(arrDelete[i]);
       
  1042             } catch(e) {}
       
  1043           }
       
  1044           if ( writeNoTags )
       
  1045           {
       
  1046             var node1 = document.createTextNode('No tags on this page');
       
  1047             var node2 = document.createTextNode(' ');
       
  1048             insertAfter(parent, node1, parent.firstChild);
       
  1049             insertAfter(parent, node2, node1);
       
  1050           }
       
  1051         }
       
  1052         else
       
  1053         {
       
  1054           alert(ajax.responseText);
       
  1055         }
       
  1056       }
       
  1057     });
   933 }
  1058 }
   934 
  1059 
   935 function ajaxTagToCat()
  1060 function ajaxTagToCat()
   936 {
  1061 {
   937   if ( !catHTMLBuf )
  1062   if ( !catHTMLBuf )