MediaWiki:Gadget-DefinitionsAdder.js: difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
m use fewer global variables |
No edit summary |
||
Line 1: | Line 1: | ||
// This script adds "Add definition" and "Add image" buttons to the toolbox section of the sidebar. |
// This script adds "Add definition" and "Add image" buttons to the toolbox section of the sidebar. |
||
// Bug: Undoing multiple image additions breaks the script. |
|||
// Still some bugs with the undo button. |
|||
var bodyContent; |
var bodyContent; |
||
Line 16: | Line 16: | ||
var ccc; |
var ccc; |
||
var addingImage = 0; |
var addingImage = 0; |
||
var definitionHover |
|||
var tempDefinitionText |
|||
function setUpBoxToBeAdded() |
function setUpBoxToBeAdded() |
||
Line 54: | Line 56: | ||
{ |
{ |
||
size: 100, |
size: 100, |
||
blur: function(){ |
|||
if(definitionHover) |
|||
⚫ | |||
else |
|||
{tempDefinitionText=currentBoxToBeAdded.lastChild.lastChild.value;} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{ |
|||
⚫ | |||
⚫ | |||
} |
|||
}, |
|||
value: tempDefinitionText || "" |
|||
} |
} |
||
) |
) |
||
Line 67: | Line 84: | ||
{ |
{ |
||
ols[i].onmouseover = |
ols[i].onmouseover = |
||
function () { this.style.borderBottom = '1px solid #000000' }; |
function () { this.style.borderBottom = '1px solid #000000'; definitionHover=this; }; |
||
ols[i].onmouseout = |
ols[i].onmouseout = |
||
function () { this.style.borderBottom = '' }; |
function () { this.style.borderBottom = ''; definitionHover=null }; |
||
ols[i].onclick = |
|||
⚫ | |||
} |
} |
||
} |
} |
||
Line 78: | Line 93: | ||
function addDefinition2(q,newdef) |
function addDefinition2(q,newdef) |
||
{ |
{ |
||
⚫ | |||
⚫ | |||
⚫ | |||
{ |
|||
⚫ | |||
⚫ | |||
ols[i].onclick = null; |
|||
} |
|||
q.style.borderBottom = ''; |
q.style.borderBottom = ''; |
||
definitionHover = null |
|||
⚫ | |||
tempDefinitionText = null |
|||
var qq = newNode('li', newNode('span')); |
var qq = newNode('li', newNode('span')); |
||
Line 125: | Line 133: | ||
},qq); |
},qq); |
||
⚫ | |||
} |
} |
||
Revision as of 05:10, 17 September 2010
// This script adds "Add definition" and "Add image" buttons to the toolbox section of the sidebar.
// Bug: Undoing multiple image additions breaks the script.
var bodyContent;
if(wgNamespaceNumber == 0 && wgAction == "view")
{
addOnloadHook(function()
{
bodyContent = document.getElementById('bodyContent');
addPortletLink('p-tb', 'javascript:addDefinition()', 'Add definition');
addPortletLink('p-tb', 'javascript:addImage()', 'Add image', 'newimagebutton');
});
}
var currentBoxToBeAdded;
var ccc;
var addingImage = 0;
var definitionHover
var tempDefinitionText
function setUpBoxToBeAdded()
{
document.body.appendChild(currentBoxToBeAdded);
document.onmousemove =
function (e)
{
e = e || event;
currentBoxToBeAdded.style.left = e.clientX + 2 + 'px';
currentBoxToBeAdded.style.top = e.clientY + 2 + 'px';
};
document.body.style.cursor = 'move';
}
function addDefinition()
{
if(! document.onmousemove)
{
currentBoxToBeAdded =
(
newNode
(
'div',
{
style:
'border: 1px solid #000000;' +
'position:fixed; left:200px; top:500px; z-index:5;' +
'padding:10px; background-color:#FFFFFF;'
},
newNode
(
'nobr',
'Definition: ',
newNode
(
'input',
{
size: 100,
blur: function(){
if(definitionHover)
{addDefinition2(definitionHover, currentBoxToBeAdded.lastChild.lastChild.value)}
else
{tempDefinitionText=currentBoxToBeAdded.lastChild.lastChild.value;}
document.body.removeChild(currentBoxToBeAdded);
document.onmousemove = null;
document.body.style.cursor = '';
var ols = document.getElementsByTagName('ol');
for(var i = 0; i < ols.length; i++)
{
ols[i].onmouseover = null;
ols[i].onmouseout = null;
}
},
value: tempDefinitionText || ""
}
)
)
)
);
setUpBoxToBeAdded();
currentBoxToBeAdded.lastChild.lastChild.focus();
var ols = document.getElementsByTagName('ol');
for(var i = 0; i < ols.length; i++)
{
ols[i].onmouseover =
function () { this.style.borderBottom = '1px solid #000000'; definitionHover=this; };
ols[i].onmouseout =
function () { this.style.borderBottom = ''; definitionHover=null };
}
}
}
function addDefinition2(q,newdef)
{
q.style.borderBottom = '';
definitionHover = null
tempDefinitionText = null
var qq = newNode('li', newNode('span'));
JsMwApi().page(wgPageName).parseFragment(newdef, function (res) { qq.lastChild.innerHTML = res; });
function addDefinition3(wikitext)
{
prevheader = q;
while(! prevheader.nodeName.match(/^h\d$/i))
{ prevheader = prevheader.previousSibling; }
var findNumberOfHeaders =
Number(prevheader.firstChild.getElementsByTagName('a')[0].href.match(/\d*$/));
wikitext =
(
wikitext.replace
(
new RegExp("((?:(^|\n)=[\\s\\S]*?){" + findNumberOfHeaders + "}[\\s\\S]*?\n#[\\s\\S]*?)(\n(?!#)|$)"),
'$1\n# ' + newdef + '\n'
)
);
ccc = wikitext;
return wikitext;
}
var editor=new Editor()
editor.addEdit({
edit: addDefinition3,
redo: function ()
{
q.appendChild(qq);
if(window.makedefsidebox && qq.childNodes.length == 1)
{ makedefsidebox(qq); } // User:Yair_rand/editor.js stuff
},
undo: function () { q.removeChild(qq); },
summary: "+def: " +newdef
},qq);
}
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
(
'[[Image:' + values.filename + '|thumb|' + values.caption + ']]',
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;
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[' + '[Image:' + q + '|thumb|' + qq + ']]\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: "+["+"[Image:"+q+"]]"
},qqqq);
}