//Template parameters wizard //Written by [[User:קיפודנחש]] if($.inArray(mw.config.get('wgAction'), ['edit', 'submit'])+1) $(function() { // template parameter is an object with the following fields: // desc: desciption string // defval: default value (optional) // options: object with optional fields: //// multiline: number of lines //// depends: another field's name //// required: boolean //// date: use JS date widget //// choices: array of legal values for the field var // templateParams is keyed by paramName. templateParams, paramsOrder, // which template are we working on template, // array of pairs - [paramName, inputField] dialogFields, // table rows keyed by paramName rowsBypName, // the fields, keyed by paramName fieldsBypName, // boolean, indicating we did not find "Parameters" page, so the parameters are extracted from template page itself. rawTemplate, rtl = $('body').is('.rtl'), // test to see if a string contains wikiCode and hence needs parsing, or cen be used as is. wikiCodeFinder = /[\[\]\{\}\<\>]/, globalExplanation = ''; function addParam(name) { if ($.inArray(name, paramsOrder) == -1) paramsOrder.push(name); } function paramsFromSelection() { var selection = $("#wpTextbox1").textSelection('getSelection').replace(/^\s*\{\{|\}\}\s*$/g, ''); //scrap the first {{ and last }} var specials = []; while (true) { //extract inner links, inner templates and inner params - we don't want to sptit those. var match = selection.match(/(\{\{[^\{\}\]\[]*\}\}|\[\[[^\{\}\]\[]*\]\]|\[[^\{\}\]\[]*\])/); if (! match || ! match.length) break; specials.push(match[0]); selection = selection.replace(match[0], "\0" + specials.length + "\0"); } var params = selection.split(/\s*\|\s*/); for (var i in params) { var param = params[i]; while (true) { var match = param.match(/\0(\d+)\0/); if (! match || ! match.length) break; param = param.replace(match[0], specials[parseInt(match[1], 10)-1]); } var paramPair = param.split("="); var name = $.trim(paramPair.shift()); if (name && paramPair.length) { templateParams[name] = templateParams[name] || {options: {notInParamPage: 1}}; addParam(name); $.extend(templateParams[name].options, {'defval': paramPair.join('=')}); } } } function buildParamsRaw(data) { var paramExtractor = /{{3,}(.*?)[\|}]/mg, m; while (m = paramExtractor.exec(data)) { templateParams[m[1]] = {desc: '', options: {multiline: 5}}; addParam(m[1]); } } function buildParams(data) { var lines = data.split("\n"), line; function extractGlobalExplanation() { line = line.replace(/[!\|][^\|]*\|/, ''); if (wikiCodeFinder.test(line)) $.post( mw.util.wikiScript('api'), {action: 'parse', text: line, disablepp: 1, format: 'json'}, function(data) { var html = data.parse.text['*']; globalExplanation = html; $('#tpw_globalExplanation').html(html).find('a').attr({target: '_blank'}); } ); else globalExplanation = line; } while (lines && lines.length) { line = lines.shift(); if (!(/^\|-/.test(line))) // look for |- this is wikitext for table row. continue; line = lines.shift(); if (line.indexOf('globalExplanation') + 1) { extractGlobalExplanation() continue; } if (! line || ! (/^\|/.test(line))) //wikitext for column continue; line = line.substr(1); // get rid of the leading | var fields = line.split('||'); if (fields.length < 2) continue; var name = $.trim(fields[0]); if (! name) continue; var desc = $.trim(fields[1]); var pAttribs = {desc: desc}; if (fields.length > 2) pAttribs.options = analyzeOptions($.trim(fields[2])); templateParams[name] = pAttribs; addParam(name); } } function analyzeOptions(str) { var res = {}, avail = ['multiline', 'required', 'depends', 'defval', 'choices', 'date'], // maybe we'll have more in the future tavail = $.map(avail, i18n), options = str.split(/\s*;\s*/); for (var i in options) { var option = options[i].split(/\s*=\s*/); var ind = $.inArray(option[0], tavail); if (ind + 1) res[avail[ind]] = option.length > 1 ? option[1] : true; } return res; } function createWikiCode() { var par = [template], delim = $('#oneLineTemplate').prop('checked') ? '' : '\n'; for (var i in dialogFields) { var field = dialogFields[i], name = $.trim(field[0]), f = field[1], hidden = f.parents('.tpw_hidden').length, val = $.trim(f.val()); if(val=="" && f.attr('type') != 'checkbox') continue;//skip parameters with no value if (f.attr('type') == 'checkbox' && ! f.prop('checked')) val = ""; par.push(name + '=' + val); } return "{{" + par.join(delim + "|") + delim + "}}"; } function showPreview() { var temp = createWikiCode(); $.post(mw.util.wikiScript('api'), {action: 'parse', title: mw.config.get('wgPageName'), prop: 'text', text: temp, format: 'json' }, function(data) { if (data && data.parse && data.parse.text) { var buttons = [{text: i18n('close'), click: function() {$(this).dialog('close');}}], div = $('