Mvolz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/154961

Change subject: using transclusion methods now instead of manual insertion
......................................................................

using transclusion methods now instead of manual insertion

Change-Id: Ie415fb82a06b8277d954c8e887e6550baf2a6e4c
---
M modules/ve.ui.CiteFromURLDialog.js
1 file changed, 273 insertions(+), 67 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Citoid 
refs/changes/61/154961/1

diff --git a/modules/ve.ui.CiteFromURLDialog.js 
b/modules/ve.ui.CiteFromURLDialog.js
index 1953304..123b1db 100644
--- a/modules/ve.ui.CiteFromURLDialog.js
+++ b/modules/ve.ui.CiteFromURLDialog.js
@@ -18,6 +18,209 @@
        ve.ui.CiteFromURLDialog.static.title = mw.msg( 
'citoid-citeFromURLDialog-title' );
 
        /**
+        * Insert filled template based on search results from citoid service
+        * @param  {[type]} fragment      The fragment where the citation needs 
to be inserted
+        * @param  {[type]} searchResults Results from citoid service (object)
+        * @return {[type]}               [description]
+        */
+       ve.ui.CiteFromURLDialog.prototype.insertTemplate = function ( 
searchResults ) {
+
+               var transclusion, template, templateName, fieldValue, 
partPromise,
+                       citation = $.parseJSON( JSON.stringify( searchResults ) 
)[0], //uses the first citation result for the time being
+                       templateTypeMap = $.parseJSON( mw.message( 
'citoid-template-type-map.json' ).plain() ),
+                       citationParams = {
+                               'first1': 'author1-first',
+                               'last1': 'author1-last',
+                               'first2': 'author2-first',
+                               'last2': 'author2-last',
+                               'first3': 'author3-first',
+                               'last3': 'author3-last',
+                               'first4': 'author4-first',
+                               'last4': 'author4-last',
+                       //      'accessdate': 'accessDate',
+                               'title': 'title',
+                               'url': 'url',
+                               'publisher': 'publisher',
+                               //a large number of Zotero types have the field 
publicationTitle
+                               //however, in setting journal to 
publicationTitle, the citation
+                               //will be formatted as a journal article, which 
may not always be
+                               //desirable.
+                               'journal': 'publicationTitle',
+                       //      'newspaper': 'publicationTitle',
+                               'date': 'date',
+                               'location': 'place',
+                               'issn': 'ISSN',
+                               'isbn': 'ISBN',
+                               'pages': 'pages',
+                               'volume': 'volume',
+                               'series': 'series',
+                               'issue': 'issue',
+                               'doi': 'DOI'
+                       },
+
+                       webParams = {
+                               'first1': 'author1-first',
+                               'last1': 'author1-last',
+                               'first2': 'author2-first',
+                               'last2': 'author2-last',
+                               'first3': 'author3-first',
+                               'last3': 'author3-last',
+                               'first4': 'author4-first',
+                               'last4': 'author4-last',
+                       //      'accessdate': 'accessDate',
+                               'title': 'title',
+                               'url': 'url',
+                               'date': 'date',
+                               'publisher': 'publisher',
+                               'website': 'publicationTitle'
+                       },
+
+                       newsParams = {
+                               'first1': 'author1-first',
+                               'last1': 'author1-last',
+                               'first2': 'author2-first',
+                               'last2': 'author2-last',
+                               'first3': 'author3-first',
+                               'last3': 'author3-last',
+                               'first4': 'author4-first',
+                               'last4': 'author4-last',
+                       //      'accessdate': 'accessDate',
+                               'title': 'title',
+                               'url': 'url',
+                               'publisher': 'publisher',
+                               'newspaper': 'publicationTitle',
+                               'date': 'date',
+                               'location': 'place',
+                               'issn': 'ISSN',
+                               'isbn': 'ISBN',
+                               'pages': 'pages',
+                               'volume': 'volume',
+                               'series': 'series',
+                               'issue': 'issue',
+                               'doi': 'DOI'
+                       },
+
+                       bookParams = {
+                               'first1': 'author1-first',
+                               'last1': 'author1-last',
+                               'first2': 'author2-first',
+                               'last2': 'author2-last',
+                               'first3': 'author3-first',
+                               'last3': 'author3-last',
+                               'first4': 'author4-first',
+                               'last4': 'author4-last',
+                       //      'accessdate': 'accessDate',
+                               'title': 'title',
+                               'url': 'url',
+                               'publisher': 'publisher',
+                               'journal': 'publicationTitle',
+                               'date': 'date',
+                               'location': 'place',
+                               'issn': 'ISSN',
+                               'isbn': 'ISBN',
+                               'pages': 'pages',
+                               'volume': 'volume',
+                               'series': 'series',
+                               'issue': 'issue',
+                               'doi': 'DOI'
+                       },
+
+                       journalParams = {
+                               'first1': 'author1-first',
+                               'last1': 'author1-last',
+                               'first2': 'author2-first',
+                               'last2': 'author2-last',
+                               'first3': 'author3-first',
+                               'last3': 'author3-last',
+                               'first4': 'author4-first',
+                               'last4': 'author4-last',
+                       //      'accessdate': 'accessDate',
+                               'title': 'title',
+                               'url': 'url',
+                               'publisher': 'publisher',
+                               'journal': 'publicationTitle',
+                               'date': 'date',
+                               'location': 'place',
+                               'issn': 'ISSN',
+                               'isbn': 'ISBN',
+                               'pages': 'pages',
+                               'volume': 'volume',
+                               'series': 'series',
+                               'issue': 'issue',
+                               'doi': 'DOI'
+                       },
+
+                       //format 'template name':parameter obj name
+                       templateParamMap = {
+                               'Citation': citationParams,
+                               'Cite web': webParams,
+                               'Cite news': newsParams,
+                               'Cite journal': journalParams,
+                               'Cite book': bookParams
+                       },
+
+                       item, fragment,
+                       that = this,
+                       surfaceModel = that.getFragment().getSurface(),
+                       doc = surfaceModel.getDocument(),
+                       internalList = doc.getInternalList();
+
+               //sets up referencemodel with blank stuff
+               if ( !that.referenceModel ) {
+                       // Collapse returns a new fragment, so update 
this.fragment
+                       that.fragment = that.getFragment().collapseRangeToEnd();
+                       that.referenceModel = new ve.dm.MWReferenceModel();
+                       that.referenceModel.insertInternalItem( surfaceModel );
+                       that.referenceModel.insertReferenceNode( 
that.getFragment() );
+               }
+               //gets bank stuff again
+               item = that.referenceModel.findInternalItem( surfaceModel );
+               if ( item ) {
+                       fragment = that.getFragment().clone( 
item.getChildren()[0].getRange());
+                       //actually inserts full transclusion model here!
+                       //fragment.insertContent(that.getPlainObject( result ) 
);
+                       //using transclusion methods...
+                       // Create an empty transclusion
+                       transclusion = new ve.dm.MWTransclusionModel();
+
+                       templateName = templateTypeMap[citation.itemType];
+                       // Create a template
+                       template = ve.dm.MWTemplateModel.newFromName( 
transclusion, templateName );
+
+                       // Add a template to a transclusion
+                       partPromise = transclusion.addPart( template );
+                       // Add parameters to the template
+                       //template.addParameter( new ve.dm.MWTemplateParameter( 
template, 'author', 'Randall Munroe' ) );
+                       //template.addParameter( new ve.dm.MWTemplateParameter( 
template, 'url', 'http://www.xkcd.com' ) );
+
+                       $.each( templateParamMap[templateName], function ( 
templateField, citoidField ) {
+                               //adds empty string to selected important 
parameters
+                               //so that template will reopen with those 
fields for manual entry
+                               fieldValue = citation[citoidField] !== 
undefined ? citation[citoidField] : '';
+
+                               template.addParameter ( new 
ve.dm.MWParameterModel( template, templateField, fieldValue ) );
+                       } );
+
+                       partPromise.done(function () {
+                               transclusion.insertTransclusionNode( fragment );
+                               // HACK: Scorch the earth - this is only needed 
because without it, the reference list won't
+                               // re-render properly, and can be removed once 
someone fixes that
+                               that.referenceModel.setDocument(
+                                       doc.cloneFromRange(
+                                               internalList.getItemNode( 
that.referenceModel.getListIndex() ).getRange()
+                                       )
+                               );
+
+                               that.referenceModel.updateInternalItem( 
surfaceModel );
+                               //hack- doesn't seem to be working in always
+                               that.popPending();
+                               that.close();
+                       });
+               }
+       };
+
+       /**When adding a template part, the template spec is automatically 
loaded from the TemplateData API, and cached for future use, this is the reason 
for loading and adding processes being asynchronous
+Adding parameters is synchronous, as the spec is already present
         * Returns full template given search results
         * @param  {Object} searchResults json response object
         * @return {Object}               filled in template
@@ -26,42 +229,6 @@
 
                var content, plainObject, d, templateHref, templateName,
                        citation = jQuery.parseJSON( JSON.stringify( 
searchResults ) )[0], //uses the first citation result for the time being
-
-               // templateTypeMap = {
-               //      book: 'Cite book',
-               //      bookSection: 'Cite book',
-               //      journalArticle: 'Cite journal',
-               //      magazineArticle: 'Cite news',
-               //      newspaperArticle: 'Cite news',
-               //      thesis: 'Cite journal',
-               //      letter: 'Citation',
-               //      manuscript: 'Cite book',
-               //      interview: 'Citation',
-               //      film: 'Citation',
-               //      artwork: 'Citation',
-               //      webpage: 'Cite web',
-               //      report: 'Cite journal',
-               //      bill: 'Citation',
-               //      hearing: 'Citation',
-               //      patent: 'Citation',
-               //      statute: 'Citation',
-               //      email: 'Cite web',
-               //      map: 'Citation',
-               //      blogPost: 'Cite web',
-               //      instantMessage: 'Citation',
-               //      forumPost: 'Cite web',
-               //      audioRecording: 'Citation',
-               //      presentation: 'Cite journal',
-               //      videoRecording: 'Citation',
-               //      tvBroadcast: 'Citation',
-               //      radioBroadcast: 'Citation',
-               //      podcast: 'Citation',
-               //      computerProgram: 'Citation',
-               //      conferencePaper: 'Cite journal',
-               //      'document': 'Citation',
-               //      encyclopediaArticle: 'Cite journal',
-               //      dictionaryEntry: 'Cite journal'
-               // },
 
                        //Parameter map for Template:Citation on en-wiki
                        //In the format citation-template-field:citoid-field
@@ -202,7 +369,43 @@
                try {
                        //Must use mw.message to avoid JSON being parsed as 
Wikitext
                        templateTypeMap = jQuery.parseJSON( mw.message( 
'citoid-template-type-map.json' ).plain() );
-               } catch ( e ) { }
+               } catch ( e ) { 
+                       templateTypeMap = {
+                               book: 'Cite book',
+                               bookSection: 'Cite book',
+                               journalArticle: 'Cite journal',
+                               magazineArticle: 'Cite news',
+                               newspaperArticle: 'Cite news',
+                               thesis: 'Cite journal',
+                               letter: 'Citation',
+                               manuscript: 'Cite book',
+                               interview: 'Citation',
+                               film: 'Citation',
+                               artwork: 'Citation',
+                               webpage: 'Cite web',
+                               report: 'Cite journal',
+                               bill: 'Citation',
+                               hearing: 'Citation',
+                               patent: 'Citation',
+                               statute: 'Citation',
+                               email: 'Cite web',
+                               map: 'Citation',
+                               blogPost: 'Cite web',
+                               instantMessage: 'Citation',
+                               forumPost: 'Cite web',
+                               audioRecording: 'Citation',
+                               presentation: 'Cite journal',
+                               videoRecording: 'Citation',
+                               tvBroadcast: 'Citation',
+                               radioBroadcast: 'Citation',
+                               podcast: 'Citation',
+                               computerProgram: 'Citation',
+                               conferencePaper: 'Cite journal',
+                               'document': 'Citation',
+                               encyclopediaArticle: 'Cite journal',
+                               dictionaryEntry: 'Cite journal'
+                       };
+               }
 
                templateName = templateTypeMap[citation.itemType];
 
@@ -310,40 +513,43 @@
                                        data: JSON.stringify( { url: 
this.searchInput.getValue() } ),
                                        dataType: 'json',
                                        success: function ( result ) {
-                                               that.close();
+                                               // that.close();
 
-                                               var item,
-                                                       surfaceModel = 
that.getFragment().getSurface(),
-                                                       doc = 
surfaceModel.getDocument(),
-                                                       internalList = 
doc.getInternalList();
+                                               // var item, fragment,
+                                               //      surfaceModel = 
that.getFragment().getSurface(),
+                                               //      doc = 
surfaceModel.getDocument(),
+                                               //      internalList = 
doc.getInternalList();
 
-                                               //sets up referencemodel with 
blank stuff
-                                               if ( !that.referenceModel ) {
-                                                       // Collapse returns a 
new fragment, so update this.fragment
-                                                       that.fragment = 
that.getFragment().collapseRangeToEnd();
-                                                       that.referenceModel = 
new ve.dm.MWReferenceModel();
-                                                       
that.referenceModel.insertInternalItem( surfaceModel );
-                                                       
that.referenceModel.insertReferenceNode( that.getFragment() );
-                                               }
-                                               //gets bank stuff again
-                                               item = 
that.referenceModel.findInternalItem( surfaceModel );
-                                               if ( item ) {
-                                                       //actually inserts full 
transclusion model here!
-                                                       
that.getFragment().clone( 
item.getChildren()[0].getRange()).insertContent(that.getPlainObject( result ) );
-                                               }
+                                               // //sets up referencemodel 
with blank stuff
+                                               // if ( !that.referenceModel ) {
+                                               //      // Collapse returns a 
new fragment, so update this.fragment
+                                               //      that.fragment = 
that.getFragment().collapseRangeToEnd();
+                                               //      that.referenceModel = 
new ve.dm.MWReferenceModel();
+                                               //      
that.referenceModel.insertInternalItem( surfaceModel );
+                                               //      
that.referenceModel.insertReferenceNode( that.getFragment() );
+                                               // }
+                                               // //gets bank stuff again
+                                               // item = 
that.referenceModel.findInternalItem( surfaceModel );
+                                               // if ( item ) {
+                                               //      fragment = 
that.getFragment().clone( item.getChildren()[0].getRange());
+                                               //      //actually inserts full 
transclusion model here!
+                                               //      
//fragment.insertContent(that.getPlainObject( result ) );
+                                               //      //using transclusion 
methods...
+                                               //      
that.insertTemplate(fragment, result);
+                                               // }
+                                               that.insertTemplate( result );
+                                               // // HACK: Scorch the earth - 
this is only needed because without it, the reference list won't
+                                               // // re-render properly, and 
can be removed once someone fixes that
+                                               // 
that.referenceModel.setDocument(
+                                               //      doc.cloneFromRange(
+                                               //              
internalList.getItemNode( that.referenceModel.getListIndex() ).getRange()
+                                               //      )
+                                               // );
 
-                                               // HACK: Scorch the earth - 
this is only needed because without it, the reference list won't
-                                               // re-render properly, and can 
be removed once someone fixes that
-                                               that.referenceModel.setDocument(
-                                                       doc.cloneFromRange(
-                                                               
internalList.getItemNode( that.referenceModel.getListIndex() ).getRange()
-                                                       )
-                                               );
+                                               // 
that.referenceModel.updateInternalItem( surfaceModel );
 
-                                               
that.referenceModel.updateInternalItem( surfaceModel );
-
-                                               //hack- doesn't seem to be 
working in always
-                                               that.popPending();
+                                               // //hack- doesn't seem to be 
working in always
+                                               // that.popPending();
                                        },
                                        error: function ( XMLHttpRequest, 
textStatus, errorThrown) {
                                                that.popPending();

-- 
To view, visit https://gerrit.wikimedia.org/r/154961
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie415fb82a06b8277d954c8e887e6550baf2a6e4c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Citoid
Gerrit-Branch: master
Gerrit-Owner: Mvolz <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to