1function tagentry_clicktag(tagname, cbox) { 2 tagentry_settag(tagname,cbox.checked); 3} 4function tagentry_settag(tagname, on) { 5 var oldtext = document.getElementById('wiki__text').value; 6 var tagstart = oldtext.toLowerCase().indexOf("{{tag>"); 7 8 if (tagstart >= 0) { 9 var tagend = oldtext.substr(tagstart).indexOf("}}"); 10 if (tagend < 0) { 11 alert('incomlete "{{tag>}}" - missing trailing "}}" ?'); 12 return; 13 } 14 // remove this tag is already present 15 var len = tagname.length+1; 16 var s=tagstart; 17 var l=tagend; 18 var f=-1; 19 while ((f=oldtext.toLowerCase().substr(s,l).indexOf(tagname.toLowerCase())) >= 0) { 20 var cs = oldtext.substr(s+f-1,1); // char before 21 if (cs != ' ' && cs != '>' ) { s+=f+len; l-=f+len; continue; } 22 var ce = oldtext.substr(s+f+len-1,1); // char after 23 if (ce != ' ' && ce != '}' ) { s+=f+len; l-=f+len; continue; } 24 if (ce == '}' ) { // no trailing whitespace to remove 25 if (cs == '>' ) { len--; } else { f--; } 26 } 27 oldtext = oldtext.substr(0,s+f)+oldtext.substr(s+f+len); 28 l = oldtext.substr(s).indexOf("}}"); 29 } 30 tagend = oldtext.substr(tagstart).indexOf("}}"); 31 32 if (tagend < 0) { 33 alert('incomlete "{{tag>}}" - missing trailing "}}" ?'); 34 return; 35 } 36 37 if (on) { // insert tag 38 var split = tagstart+tagend; 39 var ws =""; 40 if (tagend != 6) ws=" "; // empty "{{tag>" - TODO remove it!? 41 oldtext = oldtext.substr(0,split)+ws+tagname+oldtext.substr(split); 42 } 43 } else { 44 if (on) { // insert tag 45 oldtext+='\n{{tag>'+tagname+'}}'; 46 } 47 } 48 49 document.getElementById('wiki__text').value = oldtext; 50} 51