| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
( function () { |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
mw.widgets.APIResultsProvider = function MwWidgetsAPIResultsProvider( apiurl, config ) { |
| 29 |
config = config || {}; |
| 30 |
|
| 31 |
this.setAPIurl( apiurl ); |
| 32 |
this.setDefaultFetchLimit( config.fetchLimit || 30 ); |
| 33 |
this.setLang( config.lang ); |
| 34 |
this.setOffset( config.offset || 0 ); |
| 35 |
this.setAjaxSettings( config.ajaxSettings || {} ); |
| 36 |
|
| 37 |
this.staticParams = config.staticParams || {}; |
| 38 |
this.userParams = config.userParams || {}; |
| 39 |
|
| 40 |
this.toggleDepleted( false ); |
| 41 |
|
| 42 |
|
| 43 |
OO.EventEmitter.call( this ); |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
OO.mixinClass( mw.widgets.APIResultsProvider, OO.EventEmitter ); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
mw.widgets.APIResultsProvider.prototype.getResults = function () { |
| 59 |
const deferred = $.Deferred(), |
| 60 |
allParams = Object.assign( {}, this.getStaticParams(), this.getUserParams() ); |
| 61 |
|
| 62 |
const xhr = $.getJSON( this.getAPIurl(), allParams ) |
| 63 |
.done( ( data ) => { |
| 64 |
if ( Array.isArray( data ) && data.length ) { |
| 65 |
deferred.resolve( data ); |
| 66 |
} else { |
| 67 |
deferred.resolve(); |
| 68 |
} |
| 69 |
} ); |
| 70 |
return deferred.promise( { abort: xhr.abort } ); |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
mw.widgets.APIResultsProvider.prototype.setAPIurl = function ( apiurl ) { |
| 79 |
this.apiurl = apiurl; |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
mw.widgets.APIResultsProvider.prototype.getAPIurl = function () { |
| 88 |
return this.apiurl; |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
mw.widgets.APIResultsProvider.prototype.getStaticParams = function () { |
| 97 |
return this.staticParams; |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
mw.widgets.APIResultsProvider.prototype.getUserParams = function () { |
| 106 |
return this.userParams; |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
mw.widgets.APIResultsProvider.prototype.setUserParams = function ( params ) { |
| 115 |
|
| 116 |
if ( !OO.compare( params, this.userParams, true ) ) { |
| 117 |
this.userParams = Object.assign( {}, this.userParams, params ); |
| 118 |
this.reset(); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
mw.widgets.APIResultsProvider.prototype.reset = function () { |
| 126 |
|
| 127 |
this.setOffset( 0 ); |
| 128 |
|
| 129 |
this.toggleDepleted( false ); |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
mw.widgets.APIResultsProvider.prototype.getDefaultFetchLimit = function () { |
| 139 |
return this.limit; |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
mw.widgets.APIResultsProvider.prototype.setDefaultFetchLimit = function ( limit ) { |
| 148 |
this.limit = limit; |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
mw.widgets.APIResultsProvider.prototype.getLang = function () { |
| 157 |
return this.lang; |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
mw.widgets.APIResultsProvider.prototype.setLang = function ( lang ) { |
| 166 |
this.lang = lang; |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
mw.widgets.APIResultsProvider.prototype.getOffset = function () { |
| 175 |
return this.offset; |
| 176 |
}; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
mw.widgets.APIResultsProvider.prototype.setOffset = function ( offset ) { |
| 184 |
this.offset = offset; |
| 185 |
}; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
mw.widgets.APIResultsProvider.prototype.isDepleted = function () { |
| 194 |
return this.depleted; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
mw.widgets.APIResultsProvider.prototype.toggleDepleted = function ( isDepleted ) { |
| 203 |
this.depleted = isDepleted !== undefined ? isDepleted : !this.depleted; |
| 204 |
}; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
mw.widgets.APIResultsProvider.prototype.getAjaxSettings = function () { |
| 212 |
return this.ajaxSettings; |
| 213 |
}; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
mw.widgets.APIResultsProvider.prototype.setAjaxSettings = function ( settings ) { |
| 221 |
this.ajaxSettings = settings; |
| 222 |
}; |
| 223 |
}() ); |