Pengguna:Hidayatsrf/hdedit.js
Catatan: Setelah menyimpan, Anda harus memintas tembolok (cache) peramban Anda untuk melihat perubahannya. Google Chrome, Firefox, Microsoft Edge dan Safari: Tahan tombol ⇧ Shift dan klik Muat ulang (Reload) di tombol bilah alat. Untuk detail dan instruksi tentang peramban lain, lihat halaman menghapus singgahan (Inggris).
/**
* @author Janko Hoener (The Evil IP address)
* https://en.wiki-indonesia.club/wiki/User:The_Evil_IP_address/hdedit.js
* [[en:User:The Evil IP address/hdedit.js]]
* Memungkinkan satu klik pada judul bagian untuk mengeditnya secara langsung.
* Tambahkan importScript('User:Hidayatsrf/hdedit.js');[[User:Hidayatsrf/hdedit]] ke halaman <skin>.js Anda untuk menggunakan skrip ini.
*/
mw.loader.load('mediawiki.api');
window.hdedit = {
openeditform: function ($e) {
hdedit.anchor = $e.attr('id');
hdedit.pagename = mw.config.get('wgPageName');
hdedit.api = new mw.Api();
hdedit.api.get( {
action: 'parse',
page: this.pagename,
prop: 'sections',
format: 'json'
}).done( function (data) {
error = false;
error_msg = '';
$.each(data.parse.sections, function (i, v) {
if (v.anchor == hdedit.anchor) {
if (v.index !== '') {
hdedit.index = v.index;
}
else {
error = true;
error_msg = 'This section cannot be edited automatically ' +
'because it uses HTML formatting.';
}
if (v.fromtitle && (v.fromtitle != hdedit.pagename)) {
error = true;
error_msg += 'This section cannot be edited automatically ' +
'because it is transcluded from a template.';
}
return false;
}
});
if (error) {
hdedit.seterror(error_msg);
return;
}
if (hdedit.$e) {
hdedit.cancel();
}
hdedit.$e = $e;
hdedit.api.get({
action: 'parse',
page: hdedit.pagename,
section: hdedit.index,
prop: 'wikitext',
format: 'json'
}).done( function (obj) {
hdedit.wikitext = obj.parse.wikitext['*'];
hdedit.section_wikitext = hdedit.wikitext.replace(/^(=+)\s*(.+?)\s*\1[\s\S]*$/, '$2');
hdedit.inputsize = hdedit.section_wikitext.length*1.5;
var form = $('<form>').css('display', 'inline').submit(hdedit.save);
var input = $('<input>').attr('id', 'hdedit_input').attr('size', hdedit.inputsize).val(hdedit.section_wikitext);
var button1 = $('<button>').attr('id', 'hdedit_submit').attr('type', 'submit').text('Simpan');
var button2 = $('<button>').attr('type', 'button').attr('id', 'hdedit_cancel').text('Batal').click(hdedit.cancel);
$(form).append(input).append(button1).append(button2);
hdedit.form = form;
$e.after(form);
$e.hide();
}
);
}
);
},
save: function () {
hdedit.newheading = $(this).parent().find('input').val();
if (hdedit.newheading == hdedit.section_wikitext) {
return false;
}
$('#hdedit_input, #hdedit_submit, #hdedit_cancel').attr('disabled', 'disabled');
hdedit.api.edit(hdedit.pagename, function () {
return {
action: 'edit',
format: 'json',
section: hdedit.index,
minor: true,
summary: 'Perubahan judul subbagian: ' + hdedit.section_wikitext + ' → ' + hdedit.newheading
+ ' menggunakan [[Pengguna:Hidayatsrf/hdedit|HdEdit]]',
text: hdedit.wikitext.replace(/^(=+)(\s*).+?(\s*)\1(\s*)$/m, '$1$2' + hdedit.newheading
+ '$3$1$4'),
};
}).then(
function (resp) {
if (resp.result == 'Success') {
window.location.reload();
}
else if (resp.result == 'Failure') {
hdedit.seterror('API returned error code ' + data.error.code + ': ' + data.error.info);
}
else {
hdedit.seterror('Unknown API error.');
}
});
return false;
},
cancel: function () {
hdedit.$e.show();
hdedit.form.remove();
},
seterror: function (msg) {
mw.notify(msg + '\nPlease edit the section heading manually.', {title: 'Error!', autoHide: false});
},
getpref: function (name) {
if (window.hdedit_prefs !== undefined && name in window.hdedit_prefs) {
return window.hdedit_prefs[name];
}
else {
return hdedit.defaultprefs[name];
}
},
defaultprefs: {
'eventToActivate': 'click'
},
};
if (mw.config.get('wgNamespaceNumber') >= 0
&& mw.config.get('wgAction') == 'view'
&& mw.config.get('wgIsProbablyEditable')) {
var eventname = hdedit.getpref('eventToActivate');
$('h1 span.mw-headline, h2 span.mw-headline, h3 span.mw-headline, h4 span.mw-headline, h5 span.mw-headline, h6 span.mw-headline').bind(eventname,
function () {
hdedit.openeditform($(this));
}
);
}