User:Yair rand/addimage.js

if(wgNamespaceNumber == 0 && wgAction == "view" && !/&printable=yes|&diff=|&oldid=/.test(window.location.search)) { addOnloadHook(function  {    bodyContent = document.getElementById('bodyContent');    mw.util.addPortletLink('p-tb', 'javascript:addImage', 'Add image', 'newimagebutton');  }); }

var addingImage = 0;

function addImage { if(! document.onmousemove && addingImage == 0) {   addingImage = 1;

var adderObject = {       createForm: function {           return(              newNode              ( 'form', { style: 'float:right' }, newNode (                 'table',                  newNode                  ( 'tbody', newNode (                     'tr',                      newNode('td', 'File name:'),                      newNode('td', temp = newNode('input',{'name':'filename'}))                    ), newNode (                     'tr',                      newNode('td', 'Caption: '),                      newNode('td', newNode('input', {'name':'caption'}))                    ), newNode (                     'tr',                      newNode                      ( 'td', { colspan: 2, align: 'center' }, newNode('input', {type: 'submit', value: 'Place'}) )                   )                  )                )              )            );          },        fields: {           'filename': function (txt, error) { return util.validateNoWikisyntax('filename', true)(txt, error); }, 'caption': function (txt, error) {               if(txt) { return txt; } else { return error("Please specify a caption."); } }         },        onsubmit: function(values, render) {           render (             '',               function (newhtml)                 { addImage2(values.filename, values.caption, newhtml); }            ); bodyContent.removeChild(bodyContent.firstChild); }     };

new AdderWrapper(new Editor, adderObject, bodyContent, bodyContent.firstChild); temp.focus; } }

function addImage2(q, qq, newBoxHtml) { if(! document.onmousemove) {   currentBoxToBeAdded = (     newNode      ( 'div', { style: 'position:fixed;left:0px;top:0px;z-index:5;' } )   );

currentBoxToBeAdded.innerHTML = newBoxHtml; setUpBoxToBeAdded;

for(var z = 2; z <= 5; z++) {     var y = bodyContent.getElementsByTagName('h' + z); for(var x = 0; x < y.length; x++) {       y[x].onmouseover = function {           this.parentNode.insertBefore(currentBoxToBeAdded, this.nextSibling); currentBoxToBeAdded.style.position = ''; }     }    }

document.body.onclick = function {       if(! currentBoxToBeAdded.style.position) addImage3(q,qq,currentBoxToBeAdded.previousSibling); } } }

function addImage3(q,qq,qqq) { for(var z = 2; z <= 6; z++) {   var y = document.getElementsByTagName('h' + z); for(var x = 0; x < y.length; x++) {     y[x].onmouseover = null; } }

document.body.onclick = null; document.onmousemove = null; document.body.style.cursor = ''; addingImage = 0; var qqqq = currentBoxToBeAdded.firstChild; currentBoxToBeAdded.removeChild(qqqq); currentBoxToBeAdded.parentNode.removeChild(currentBoxToBeAdded);

function addImage4(wikitext) {   var findNumberOfHeaders = Number(qqq.firstChild.getElementsByTagName('a')[0].href.match(/\d*$/)); wikitext = (     wikitext.replace      ( new RegExp (         "((?:(^|\n)=[\\s\\S]*?){" + findNumberOfHeaders + "}[\\s\\S]*?)\n"        ), '$1\n\n' )   );    ccc = wikitext; return wikitext; }

var editor = new Editor; editor.addEdit({             edit: addImage4,              redo: function  { qqq.parentNode.insertBefore(qqqq,qqq.nextSibling); },              undo: function  { qqqq.parentNode.removeChild(qqqq); },              summary: "+"          },qqqq); }