تضامنًا مع حق الشعب الفلسطيني |
ميدياويكي:Gadget-auto-cite-shamela.js
اذهب إلى التنقل
اذهب إلى البحث
ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.
/**
* Auto Cite Shamela
*
* الاستشهاد الآلي بالشاملة
*
* كتبها: حبيشان
* by [[:ar:User:حبيشان]]
*
* تتطلب [[:ar:Module:Cite Shamela]] [[:ar:Module:Cite Shamela/data.json]] [[:ar:قالب:استشهاد بالشاملة]]
*
*/
mw.hook( 've.activationComplete' ).add( function () {
ve.ui.CitoidInspector.prototype._performLookup = ve.ui.CitoidInspector.prototype.performLookup;
ve.ui.CitoidInspector.prototype.performLookup = function () {
var inspector = this;
// TODO: Add caching for requested urls
if ( this.lookupPromise ) {
// Abort existing lookup
this.lookupPromise.abort();
this.lookupPromise = null;
this.lookupInput.popPending();
}
// Set as pending
this.lookupButton.setDisabled( true );
this.lookupInput.setDisabled( true ).pushPending();
var search = this.lookupInput.getValue();
// Common case: pasting a URI into this field. Citoid expects
// minimally encoded input, so do some speculative decoding here to
// avoid 404 fetches. T146539
search = ve.safeDecodeURIComponent( search );
// We have to first set up a get response so we can have
// a proper xhr object with "abort" method, so we can
// hand off this abort method to the jquery promise
var xhr;
if (search.indexOf("shamela.ws")>-1 || /^«[^»]+» \((ص)?[0-9 \/]+([^0-9]+)?\)/.test(search))
{
var params = {
action: "expandtemplates",
text: "{{استشهاد بالشاملة|" + search + "|expand=json}}",
prop: "wikitext",
format: "json"
},
xhr = new mw.Api().get(params);
} else {
return inspector._performLookup();
}
this.lookupPromise = xhr
.then(
// Success
function ( searchResults ) {
// Build results
var rData = JSON.parse(searchResults.expandtemplates.wikitext)
if (rData.error) {
mw.notify( rData.error, { title: 'استشهاد بالشاملة', type:'error'});
return inspector._performLookup();
} else {
var renderPromises = [],
partPromises = [];
var transclusionModel = new ve.dm.MWTransclusionModel();
var result = {
templateName: rData.template.target.wt,
template: ve.dm.MWTemplateModel.newFromName( transclusionModel, rData.template.target.wt ),
transclusionModel: transclusionModel
};
for (var param in rData.template.params) {
result.template.addParameter(
new ve.dm.MWParameterModel(
result.template, param, rData.template.params[ param ].wt
)
);
};
inspector.results.push( result );
partPromises.push(
result.transclusionModel.addPart( result.template )
)
return ve.promiseAll( partPromises )
.then( function () {
console.log('after_partpromises')
var sources = [],
optionWidgets = [];
// Create option widgets
inspector.results.forEach( function ( result, i ) {
var refWidget = new ve.ui.CitoidReferenceWidget(
inspector.getFragment().getSurface().getDocument(),
result.transclusionModel,
{
data: i,
templateName: result.templateName,
citeTools: inspector.citeTools
} );
var template = result.template;
// T92428: Ignore empty templates
if ( ve.isEmptyObject( template.getParameters() ) ) {
console.log('empty');
return;
}
sources.push( result.source ); // source may be undefined or Array of strings
optionWidgets.push( refWidget );
renderPromises.push( refWidget.getRenderPromise() );
} );
if ( optionWidgets.length > 0 ) {
// Add citations to the select widget
inspector.previewSelectWidget.addItems( optionWidgets );
// Add credit for the first result only to the widget, currently for Zotero & WorldCat only
return ve.promiseAll( renderPromises ).then( function () {
inspector.setModePanel( 'auto', 'result' );
}, function () {
inspector.lookupFailed();
return $.Deferred().resolve();
} );
}
// failed, so go back
return $.Deferred().reject();
});
}
},
// Fail
function ( type, response ) {
if ( response && response.textStatus === 'abort' ) {
return $.Deferred().reject();
}
inspector.lookupFailed();
// Restore focus to the input field.
// Definitely don't do this on success and focusing a hidden input causes jQuery
// to prevent it from being focused the next time the inspector is opened (T285626)
inspector.lookupInput
.setDisabled( false ).focus();
return $.Deferred().resolve();
} )
.always( function () {
inspector.lookupInput
.setDisabled( false )
.popPending();
inspector.lookupButton.setDisabled( false );
} )
.promise( { abort: xhr.abort } );
return this.lookupPromise;
};
});