MediaWiki:Wikia.js/referenceForm.js

/*
 * @name        Reference Form
 * @author       T3CHNOCIDE
 * @description Add reference buttons to the editor which allow users to add references to an article by filling out forms.
 * @notes        Buttons do not always append to RTE, requires further refinement.
 * @license      CC-BY-SA - http://creativecommons.org/licenses/by-sa/3.0/

//Places buttons into editor $( document ).ready(function { if ( mw.config.get( 'wgAction' ) === 'edit') {	$(".cke_buttons.cke_toolbar_insert").append(' Reference  ');	$(".cke_toolbar_source").prepend('');	$(".cke_buttons.cke_toolbar_format").append('  Source/Reference  ') } });

//Forces buttons into RTE with while loop $( document ).ready(function {	if ( mw.config.get( 'wgAction' ) === 'edit' && ($('.module_insert').length == 1)) {		while (/Reference/i.test($('.module_insert').text) == false) {			$(".cke_buttons.cke_toolbar_insert").append(' Reference </a> ');			$(".cke_buttons.cke_toolbar_format").append('<span class="cke_separator"> <span class="cke_button cke_button_undo cke_off"><span style="background-image:url(\'https://images.wikia.nocookie.net/operation-neogenesis/images/8/8a/Reference_Button_Visual_Mode.png\') !important; background-position:0px 0px !important;" class="cke_icon"> <span id="refbuttonvisual" class="cke_label">Source/Reference </a> ') }	} });

//Sets access date var date = new Date; var day = date.getDate; var month = date.getMonth+1; //January is 0! var year = date.getFullYear; var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December"); if(day<10){day='0'+day} var month = months[month] var date = day+' '+month+' '+year;

//Detects editor by UltimateSupreme //http://naruto.wikia.com/wiki/User:UltimateSupreme var editor = window.WikiaEditor && window.WikiaEditor.getInstance('wpTextbox1'); if (editor && editor.ck) { if (editor.ck.document) { //Visual Editor var editorVersion = 'Visual' } else if (editor.ck.textarea) { //Source Editor var editorVersion = 'Source' } } else { var editorVersion = 'Source' }

//Reference options function RefOptions { $.showCustomModal("References <span style=\"font-size:small; color:white;\">What is the source of your information? ", '<div style=\"width:460px; text-align:center;\"> ', {		id: "referenceOptions",		width: 460	}); }

//Reference code popup function referenceCode(code) { $.showCustomModal( "Reference Code", "Paste the following code after your sourced information in order to cite it: <div style='background:rgba(0,0,0,0.5); border:1px solid rgba(56,75,107,0.6);'>" +code+ " ", {	id: "referenceCode",	width: 472,	buttons: [		{			id: "cancelCode",			message: "Close",			handler: function {			$("#referenceCode").closeModal;			}		}	]	}); }

//Game source form function GameSource { $("#referenceOptions").closeModal; $.showCustomModal( "Game Citation Form", "    Edition: <td colspan=\"3\" class='toolTip' data-tooltip='Edition of game (e.g. Standard or Game of the Year)'><input type='text' id='GameEdition' style='margin-left:2px; width:384px;'>    Developer: <td class='toolTip' data-tooltip='Developer of the game'><input type='text' id='GameDeveloper' value='Bungie'>  Publisher: <td class='toolTip' data-tooltip='Publisher of the game'><input type='text' id='GamePublisher' value='Activision Blizzard'>   ", {	id: "referenceGame",	width: 472,	buttons: [		{			id: "returnReferenceMenu",			message: "Back",			handler: function  {			$("#referenceGame").closeModal;			RefOptions			}		},		{			id: "cancelReference",			message: "Cancel",			handler: function  {			$("#referenceGame").closeModal;			}		},		{			id: "addGameReference",			defaultButton: true,			message: "Add Source",			handler: function  {			addGameReference; }		}	]	}); }

//Book source form function BookSource { $("#referenceOptions").closeModal; $.showCustomModal( "Book Citation Form", "  ISBN: <td class='toolTip' data-tooltip='ISBN number of the book'><input type='text' id='BookISBN'>   ", {	id: "referenceBook",	width: 472,	buttons: [		{			id: "returnReferenceMenu",			message: "Back",			handler: function  {			$("#referenceBook").closeModal;			RefOptions			}		},		{			id: "cancelReference",			message: "Cancel",			handler: function  {			$("#referenceBook").closeModal;			}		},		{			id: "addBookReference",			defaultButton: true,			message: "Add Source",			handler: function  {			addBookReference;			}		}	]	}); }

//Video source form function VideoSource { $("#referenceOptions").closeModal; $.showCustomModal( "Video Citation Form", "   ", {	id: "referenceVideo",	width: 472,	buttons: [		{			id: "returnReferenceMenu",			message: "Back",			handler: function  {			$("#referenceVideo").closeModal;			RefOptions			}		},		{			id: "cancelReference",			message: "Cancel",			handler: function  {			$("#referenceVideo").closeModal;			}		},		{			id: "addVideoReference",			defaultButton: true,			message: "Add Source",			handler: function  {			addVideoReference;			}		}	]	}); }

//Article source form function ArticleSource { $("#referenceOptions").closeModal; $.showCustomModal( "Article Citation Form", "   ", {	id: "referenceArticle",	width: 472,	buttons: [		{			id: "returnReferenceMenu",			message: "Back",			handler: function  {			$("#referenceArticle").closeModal;			RefOptions			}		},		{			id: "cancelReference",			message: "Cancel",			handler: function  {			$("#referenceArticle").closeModal;			}		},		{			id: "addArticleReference",			message: "Add Source",			handler: function  {			addArticleReference;			}		}	]	}); }

//Website source form function WebsiteSource { $("#referenceOptions").closeModal; $.showCustomModal( "Website Citation Form", "   ", {	id: "referenceWebsite",	width: 472,	buttons: [		{			id: "returnReferenceMenu",			message: "Back",			handler: function  {			$("#referenceWebsite").closeModal;			RefOptions			}		},		{			id: "cancelReference",			message: "Cancel",			handler: function  {			$("#referenceWebsite").closeModal;			}		},		{			id: "addWebsiteReference",			defaultButton: true,			message: "Add Source",			handler: function  {			addWebsiteReference;			}		}	]	}); }

//Adding sources function addGameReference { var gameDeveloperInput = document.getElementById("GameDeveloper").value; var dayValue = $("#publishDay").val; var monthValue = $("#publishMonth").val; var yearValue = $("#publishYear").val; var gameDateInput = yearValue+'-'+monthValue+'-'+dayValue var gameNameInput = document.getElementById("GameName").value; var gameEditionInput = document.getElementById("GameEdition").value; var gameMissionInput = document.getElementById("GameMission").value; var gameConsoleInput = document.getElementById("GameConsole").value; var gamePublisherInput = document.getElementById("GamePublisher").value; $("#referenceGame").closeModal; if (editorVersion == 'Source') { $("textarea.cke_source").insertAtCaret(" "); $("#editform, #toolbar, #wpTextbox1").insertAtCaret(" "); } else if (editorVersion == 'Visual') { pasteHtmlAtCaret("&lt;ref name=\"" +gameConsoleInput+ " - " +gameNameInput+ ": " +gameMissionInput+ "\"&gt;&lt;/ref&gt;",iframeWindow); } }

function addBookReference { var BookFirstNameInput = document.getElementById("BookFirstName").value; var BookLastNameInput = document.getElementById("BookLastName").value; var BookCoAuthorsInput = document.getElementById("BookCoAuthors").value; var dayValue = $("#publishDay").val; var monthValue = $("#publishMonth").val; var yearValue = $("#publishYear").val; var BookDateInput = yearValue+'-'+monthValue+'-'+dayValue var BookTitleInput = document.getElementById("BookTitle").value; var BookPublisherInput = document.getElementById("BookPublisher").value; var BookCityInput = document.getElementById("BookCity").value; var BookPagesInput = document.getElementById("BookPages").value; var BookISBNInput = document.getElementById("BookISBN").value; $("#referenceBook").closeModal; if (editorVersion == 'Source') { $("textarea.cke_source").insertAtCaret(" "); $("#editform, #toolbar, #wpTextbox1").insertAtCaret(" "); } else if (editorVersion == 'Visual') { pasteHtmlAtCaret("&lt;ref name=\"" +BookTitleInput+ ": " +BookPagesInput+ "\"&gt;&lt;/ref&gt;",iframeWindow); } }

function addVideoReference { var VideoUsernameInput = document.getElementById("VideoUsername").value; var dayValue = $("#publishDay").val; var monthValue = $("#publishMonth").val; var yearValue = $("#publishYear").val; var VideoDateInput = yearValue+'-'+monthValue+'-'+dayValue var VideoHostInput = document.getElementById("VideoHost").value; var VideoURLInput = document.getElementById("VideoURL").value; var VideoNameInput = document.getElementById("VideoName").value; var VideoTimeInput = document.getElementById("VideoTime").value; $("#referenceVideo").closeModal; if (editorVersion == 'Source') { $("textarea.cke_source").insertAtCaret(" "); $("#editform, #toolbar, #wpTextbox1").insertAtCaret(" "); } else if (editorVersion == 'Visual') { pasteHtmlAtCaret("&lt;ref name=\"" +VideoHostInput+ ": " +VideoUsernameInput+ " - " +VideoNameInput+ "\"&gt;&lt;/ref&gt;",iframeWindow); } }

function addArticleReference { var ArticleFirstNameInput = document.getElementById("ArticleFirstName").value; var ArticleLastNameInput = document.getElementById("ArticleLastName").value; var ArticleCoAuthorsInput = document.getElementById("ArticleCoAuthors").value; var dayValue = $("#publishDay").val; var monthValue = $("#publishMonth").val; var yearValue = $("#publishYear").val; var ArticleDateInput = yearValue+'-'+monthValue+'-'+dayValue var ArticleURLInput = document.getElementById("ArticleURL").value; var ArticleNameInput = document.getElementById("ArticleName").value; var ArticlePublisherInput = document.getElementById("ArticlePublisher").value; $("#referenceArticle").closeModal; if (editorVersion == 'Source') { $("textarea.cke_source").insertAtCaret(" "); $("#editform, #toolbar, #wpTextbox1").insertAtCaret(" "); } else if (editorVersion == 'Visual') { pasteHtmlAtCaret("&lt;ref name=\"" +ArticlePublisherInput+ ": " +ArticleNameInput+ " - " +ArticleLastNameInput+ "\"&gt;&lt;/ref&gt;",iframeWindow); } }

function addWebsiteReference { var WebsiteWebsiteInput = document.getElementById("WebsiteWebsite").value; var WebsiteTitleInput = document.getElementById("WebsiteTitle").value var WebsiteURLInput = document.getElementById("WebsiteURL").value; var WebsitePublisherInput = document.getElementById("WebsitePublisher").value; var dayValue = $("#publishDay").val; var monthValue = $("#publishMonth").val; var yearValue = $("#publishYear").val; var WebsiteDateInput = yearValue+'-'+monthValue+'-'+dayValue $("#referenceWebsite").closeModal; if (editorVersion == 'Source') { $("textarea.cke_source").insertAtCaret(" "); $("#editform, #toolbar, #wpTextbox1").insertAtCaret(" "); } else if (editorVersion == 'Visual') { pasteHtmlAtCaret("&lt;ref name=\"" +WebsiteWebsiteInput+ ": " +WebsiteTitleInput+ " - " +WebsitePublisherInput+ "\"&gt;&lt;/ref&gt;",iframeWindow); } }

//Post text at caret by ob (textarea) - http://stackoverflow.com/users/56829/ob //http://stackoverflow.com/questions/4456545/how-to-insert-text-at-the-current-caret-position-in-a-textarea $.fn.insertAtCaret = function(text) { return this.each(function {       if (document.selection && this.tagName == 'TEXTAREA') {            //IE textarea support            this.focus;            sel = document.selection.createRange;            sel.text = text;            this.focus;        } else if (this.selectionStart || this.selectionStart == '0') {            //MOZILLA/NETSCAPE support            startPos = this.selectionStart;            endPos = this.selectionEnd;            scrollTop = this.scrollTop;            this.value = this.value.substring(0, startPos) + text + this.value.substring(endPos, this.value.length);            this.focus;            this.selectionStart = startPos + text.length;            this.selectionEnd = startPos + text.length;            this.scrollTop = scrollTop;        } else {            // IE input[type=text] and other browsers            this.value += text;            this.focus; this.value = this.value;   // forces cursor to end }   }); };

//Post text at caret (iframe) by Shreyas - http://stackoverflow.com/users/113865/shreyas //http://stackoverflow.com/questions/20002063/javascript-pasting-text-at-caret-in-wysiwyg-editor if (editorVersion == 'Visual') { var iframeWindow = document.getElementById('cke_contents_wpTextbox1').getElementsByTagName('iframe')[0].contentWindow; function pasteHtmlAtCaret(html,windo) { windo = windo || window; var sel, range; if (windo.getSelection) { // IE9 and non-IE sel = windo.getSelection; console.log(sel); if (sel.getRangeAt && sel.rangeCount) { range = sel.getRangeAt(0); console.log(range); range.deleteContents;

// Range.createContextualFragment would be useful here but is				// non-standard and not supported in all browsers (IE9, for one) var el = windo.document.createElement("div"); el.innerHTML = html; var frag = windo.document.createDocumentFragment, node, lastNode; while ( (node = el.firstChild) ) { lastNode = frag.appendChild(node); }				range.insertNode(frag);

// Preserve the selection if (lastNode) { range = range.cloneRange; range.setStartAfter(lastNode); range.collapse(true); sel.removeAllRanges; sel.addRange(range); }			}		} else if (windo.document.selection && windo.document.selection.type != "Control") { // IE < 9 windo.document.selection.createRange.pasteHTML(html); }	} }