SemanticMediaWiki /
SemanticCite
| 1 | /** |
||
| 2 | * qTip Javascript handler for the scite extension |
||
| 3 | */ |
||
| 4 | |||
| 5 | /*global jQuery, mediaWiki, onoi, QTip */ |
||
| 6 | /*global confirm */ |
||
| 7 | |||
| 8 | ( function ( $, mw, onoi ) { |
||
| 9 | |||
| 10 | 'use strict'; |
||
| 11 | |||
| 12 | $( function ( $ ) { |
||
| 13 | |||
| 14 | var configuration = mw.config.get( 'ext.scite.config' ); |
||
| 15 | var blobstore = new onoi.blobstore( |
||
| 16 | 'scite' + ':' + |
||
| 17 | mw.config.get( 'wgCookiePrefix' ) + ':' + |
||
| 18 | mw.config.get( 'wgUserLanguage' ) |
||
| 19 | ); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * API instance |
||
| 23 | * |
||
| 24 | * @since 1.0 |
||
| 25 | */ |
||
| 26 | var doApiRequestFor = function( reference, QTip ) { |
||
| 27 | var api = new mw.Api(); |
||
| 28 | |||
| 29 | api.get( { |
||
| 30 | action: 'ask', |
||
| 31 | format: 'json', |
||
| 32 | query: '[[Citation key::' + reference + ']]|?Citation text|limit=1' |
||
| 33 | } ).done ( function ( content ) { |
||
| 34 | |||
| 35 | var citationText = ''; |
||
| 36 | |||
| 37 | // Retrieve the text from the request content |
||
| 38 | // The query only ask for "Citation text" as printout therefore no |
||
| 39 | // further verification is done here |
||
| 40 | $.each( content.query.results, function( subjectName, subject ) { |
||
| 41 | if ( $.inArray( 'printouts', subject ) ) { |
||
| 42 | $.each ( subject.printouts, function( property, values ) { |
||
| 43 | // https://github.com/SemanticMediaWiki/SemanticMediaWiki/issues/1208 |
||
| 44 | citationText = $.type( values ) === "array" ? values.toString() : values[0]; |
||
| 45 | } ); |
||
| 46 | }; |
||
| 47 | } ); |
||
| 48 | |||
| 49 | if ( citationText === '' ) { |
||
| 50 | var msgKey = content.hasOwnProperty( 'query-continue-offset' ) ? 'sci-tooltip-citation-lookup-failure-multiple' : 'sci-tooltip-citation-lookup-failure'; |
||
| 51 | |||
| 52 | QTip.set( |
||
| 53 | 'content.text', |
||
| 54 | mw.msg( msgKey, reference ) |
||
| 55 | ); |
||
| 56 | return null; |
||
| 57 | }; |
||
| 58 | |||
| 59 | // Parse the raw text to ensure that links are correctly |
||
| 60 | // displayed |
||
| 61 | api.parse( '<div class="scite-api-parse">' + citationText + '</div>' ) |
||
| 62 | .done( function ( html ) { |
||
| 63 | // Find only relevant details |
||
| 64 | html = $( html ).find( ".scite-api-parse" ).html(); |
||
| 65 | |||
| 66 | if ( html === undefined ) { |
||
| 67 | html = citationText; |
||
| 68 | }; |
||
| 69 | |||
| 70 | blobstore.set( |
||
| 71 | reference, |
||
| 72 | html, |
||
| 73 | configuration.tooltipRequestCacheTTL |
||
| 74 | ) |
||
| 75 | |||
| 76 | QTip.set( |
||
| 77 | 'content.text', |
||
| 78 | html |
||
| 79 | ); |
||
| 80 | } ); |
||
|
0 ignored issues
–
show
Best Practice
introduced
by
Loading history...
|
|||
| 81 | } ).fail ( function( xhr, status, error ) { |
||
| 82 | // Upon failure... set the tooltip content to error |
||
| 83 | QTip.set( 'content.text', status + ': ' + error ); |
||
| 84 | } ); |
||
| 85 | }; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * qTip tooltip instance |
||
| 89 | * |
||
| 90 | * @since 1.0 |
||
| 91 | */ |
||
| 92 | var tooltip = function () { |
||
| 93 | |||
| 94 | var reference = $( this ).data( 'reference' ); |
||
| 95 | |||
| 96 | // Only act on a href link |
||
| 97 | $( this ).find( 'a' ).qtip( { |
||
| 98 | content: { |
||
| 99 | title : reference, |
||
| 100 | text : function( event, QTip ) { |
||
| 101 | |||
| 102 | // Async process |
||
| 103 | blobstore.get( reference, function( value ) { |
||
| 104 | if ( configuration.tooltipRequestCacheTTL == 0 || value === null ) { |
||
|
0 ignored issues
–
show
|
|||
| 105 | doApiRequestFor( reference, QTip ); |
||
| 106 | } else { |
||
| 107 | // console.log( reference ); |
||
| 108 | QTip.set( 'content.title', '<span>' + reference + '</span><div class="scite-tooltip-cache-indicator scite-tooltip-cache-browser"></div>' ); |
||
| 109 | QTip.set( 'content.text', value ); |
||
| 110 | } |
||
| 111 | } ); |
||
| 112 | |||
| 113 | // Show a loading image while waiting on the request result |
||
| 114 | return $( '<div>' ) |
||
| 115 | .addClass( 'scite-tooltip' ) |
||
| 116 | .append( $( '<span>' ).addClass( 'scite-tooltip-loading' ).prop( 'alt', 'Loading...' ) ); |
||
| 117 | } |
||
| 118 | }, |
||
| 119 | position: { |
||
| 120 | viewport: $( window ), |
||
| 121 | my: 'bottom left', |
||
| 122 | at: 'top middle' |
||
| 123 | }, |
||
| 124 | hide : { |
||
| 125 | fixed: true, |
||
| 126 | delay: 300 |
||
| 127 | }, |
||
| 128 | style : { |
||
| 129 | classes: $( this ).attr( 'class' ) + ' qtip-default qtip-light qtip-shadow', |
||
| 130 | def : false |
||
| 131 | } |
||
| 132 | } ); |
||
| 133 | }; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @since 1.0 |
||
| 137 | */ |
||
| 138 | $.map( configuration.showTooltipForCitationReference, function( selector, i ) { |
||
|
0 ignored issues
–
show
|
|||
| 139 | |||
| 140 | switch( selector ) { |
||
| 141 | case 2: |
||
| 142 | selector = '.scite-citeref-key'; |
||
| 143 | break; |
||
| 144 | case 1: |
||
| 145 | default: |
||
| 146 | selector = '.scite-citeref-number'; |
||
| 147 | } |
||
| 148 | |||
| 149 | $( selector ).each( tooltip ); |
||
| 150 | } ); |
||
| 151 | |||
| 152 | } ); |
||
| 153 | }( jQuery, mediaWiki, onoi ) ); |
||
| 154 |