-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateInsect.js
More file actions
182 lines (177 loc) · 6.28 KB
/
Copy pathcreateInsect.js
File metadata and controls
182 lines (177 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
(function () {
var weAPI = mw.config.get('wgServer') + '/api.php';
function escapeWikitext(s) {
// FIXME allow templates within the textarea?
s = s.replace(/[{}]/g, '');
return s.replace(/\|/g, '{{!}}');
}
function mwAPI(data, success, failure) {
if (!data.hasOwnProperty('format')) {
data.format = 'json';
}
$.ajax({
url: weAPI,
data: data,
async: false,
dataType: 'json',
type: 'POST',
success: success,
failure: failure || function() {} // silently fail!
});
}
function showForm() {
var normName = '', token = '';
var tp = 'Template:';
var form = '<div style="float: left;"><form action="#">' +
'<div style="clear: both; padding: 5px 0px 1.5em;">' +
'<span class="weLabel">' +
'<label for="name"><span style="color: red;">*</span> Name:' +
'</label></span>' +
'<input id="name" type="text" size="40" value="Template:" />' +
'<span id="nameError" style="margin-left: 1em; color: red;"></span>' +
'</div>' +
'<div style="clear: both; padding-top: 5px">' +
'<span class="weLabel">' +
'<label for="order">Order:' +
'</label></span>' +
'<input id="order" type="text" size="40" />' +
'</div>' +
'<div style="clear: both; padding-top: 5px">' +
'<span class="weLabel">' +
'<label for="family">Family: </label></span>' +
'<input id="family" type="text" size="40" />' +
'</div>' +
'<div style="clear: both; padding-top: 5px">' +
'<span class="weLabel">' +
'<label for="genus">Genus: </label></span>' +
'<input id="genus" type="text" size="40" />' +
'</div>' +
'<div style="clear: both; padding-top: 5px">' +
'<span class="weLabel">' +
'<label for="link">Link: </label></span>' +
'<input id="link" type="text" size="40" />' +
'</div>' +
'<div style="clear: both; padding-top: 5px">' +
'<span class="weLabel">' +
'<label for="image">Image: </label></span>' +
'<input id="image" type="text" size="40" />' +
'</div>' +
'<div style="clear: both; padding-top: 5px">' +
'<span class="weLabel">' +
'<label for="text">Text: </label></span>' +
'<textarea id="text" type="textarea" rows="10" cols="40" ' +
' style="width: 500px;">' +
'</textarea></div>' +
'<div style="clear: both; padding-top: 5px">' +
'<span class="weLabel" style="color: red; font-size: smaller">' +
'* required</span>' +
'<input id="weInsectButton" type="submit" value="Create" ' +
' disabled />' +
'</div>' +
'</form></div>';
$('.weAddInsectButtonSpinner').hide();
$('.weAddInsectButton').hide();
$('<div id="weDialog" style="display:none">' + form + "</div>\n")
.appendTo('body');
$('span.weLabel').css({'width': '120px', 'padding-right': '15px',
'float': 'left', 'text-align': 'right'});
$('#name').blur(function() {
var n = $.trim($(this).val());
n = n.charAt(0).toUpperCase() + n.slice(1);
$(this).val(n);
if ((n === '') || (n === 'Template:')) {
return;
}
mwAPI({
action: 'query',
prop: 'info',
intoken: 'edit',
titles: n
}, function(d) {
if (d.hasOwnProperty('query') &&
d.query.hasOwnProperty('pages') &&
d.query.pages.hasOwnProperty('-1')) {
token = d.query.pages[-1].edittoken;
// check if we are told a normalized name
if (d.query.hasOwnProperty('normalized')) {
normName = d.query.normalized[0].to;
} else {
normName = n;
}
$('#weInsectButton').val('Create ' + normName);
$('#weInsectButton').removeAttr('disabled');
$('#nameError').text('');
$('#name').css('background', '');
} else {
token = ''; // revoke the token, just to be sure
$('#nameError').text('already exists');
$('#name').css('background', 'lightcoral');
}
});
if (n.indexOf(tp) === 0) {
n = n.substr(tp.length);
n = n.charAt(0).toUpperCase() + n.slice(1);
$(this).val(tp + n);
}
if ((n.length !== 0) && ($('#genus').val() === '')) {
$('#genus').val(n.toLowerCase());
}
}).focus(function() {
// disable the button, name is changing
$('#weInsectButton').val('Create').attr('disabled', 'disabled');
});
$('#name').val($('#name').val());
$('#name').focus();
$('#weDialog').dialog({height: 580, width: 720, modal: true,
resizable: true,
title: 'Create a new insect template'});
$('#weInsectButton').click(function() {
var n = $.trim($('#name').val());
n = n.charAt(0).toUpperCase() + n.slice(1);
if ((n === '') || (n === tp)) {
return;
}
$(this).attr('disabled', 'disabled');
$(this).replaceWith('<img src="/skins/common/images/Ajax-loader.gif">');
var params = ['name', 'order', 'family', 'genus', 'image',
'link', 'text'];
var data = "{{InsectSection\n";
for (var p in params) {
if (params.hasOwnProperty(p)){
data += '|' + params[p] + '=';
var v = $.trim($('#' + params[p]).val());
if (v !== '') {
data += escapeWikitext(v);
}
data += "\n";
}
}
data += "}}\n";
mwAPI({
action: 'edit',
title: normName,
summary: 'new insect template',
text: data,
token: token,
watchlist: 'watch'
}, function (d) {
$('#weDialog').dialog('close');
window.location = mw.config.get('wgServer') + '/' + encodeURIComponent(normName);
}, function(hdr, stat, err) {
alert("Unable to save new insect!\n" + stat);
});
});
}
var addButton = '<input type="submit" ' +
'value="You must be logged in to add an insect" disabled="disabled">';
if (mw.config.get('wgUserName') !== null) {
addButton = '<input type="submit" class="weAddInsectButton" ' +
'value="Add an Insect" />';
}
$('.weInsectAdd').append(addButton);
$('.weAddInsectButton').click(function() {
$('.weAddInsectButton').attr('disabled', 'disabled');
showForm();
});
}());