| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
ve.ui.MWEditSummaryWidget = function VeUiMWEditSummaryWidget( config ) { |
| 20 |
config = config || {}; |
| 21 |
|
| 22 |
|
| 23 |
ve.ui.MWEditSummaryWidget.super.call( this, ve.extendObject( { |
| 24 |
autosize: true, |
| 25 |
maxRows: 15, |
| 26 |
allowLinebreaks: false |
| 27 |
}, config ) ); |
| 28 |
|
| 29 |
|
| 30 |
OO.ui.mixin.LookupElement.call( this, ve.extendObject( { |
| 31 |
showPendingRequest: false, |
| 32 |
showSuggestionsOnFocus: false, |
| 33 |
allowSuggestionsWhenEmpty: false, |
| 34 |
highlightFirst: false |
| 35 |
}, config ) ); |
| 36 |
|
| 37 |
this.limit = config.limit || 6; |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
OO.inheritClass( ve.ui.MWEditSummaryWidget, OO.ui.MultilineTextInputWidget ); |
| 43 |
|
| 44 |
OO.mixinClass( ve.ui.MWEditSummaryWidget, OO.ui.mixin.LookupElement ); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
ve.ui.MWEditSummaryWidget.static.summarySplitter = /^(\/\*.*?\*\/\s*)?([^]*)$/; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
ve.ui.MWEditSummaryWidget.static.splitSummary = function ( summary ) { |
| 59 |
const result = summary.match( this.summarySplitter ); |
| 60 |
return { |
| 61 |
section: result[ 1 ] || '', |
| 62 |
comment: result[ 2 ] |
| 63 |
}; |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
ve.ui.MWEditSummaryWidget.static.getMatchingSummaries = function ( summaries, query ) { |
| 74 |
const summaryPrefixMatches = [], wordPrefixMatches = [], otherMatches = [], |
| 75 |
lowerQuery = query.toLowerCase(); |
| 76 |
|
| 77 |
if ( !query.trim() ) { |
| 78 |
|
| 79 |
return []; |
| 80 |
} |
| 81 |
|
| 82 |
summaries.forEach( ( summary ) => { |
| 83 |
const lowerSummary = summary.toLowerCase(), |
| 84 |
index = lowerSummary.indexOf( lowerQuery ); |
| 85 |
if ( index === 0 ) { |
| 86 |
|
| 87 |
if ( lowerQuery !== summary ) { |
| 88 |
summaryPrefixMatches.push( summary ); |
| 89 |
} |
| 90 |
} else if ( index !== -1 ) { |
| 91 |
if ( /^\s/.test( lowerSummary.charAt( index - 1 ) ) ) { |
| 92 |
|
| 93 |
wordPrefixMatches.push( summary ); |
| 94 |
} else { |
| 95 |
otherMatches.push( summary ); |
| 96 |
} |
| 97 |
} |
| 98 |
} ); |
| 99 |
return summaryPrefixMatches.concat( wordPrefixMatches, otherMatches ); |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
ve.ui.MWEditSummaryWidget.prototype.adjustSize = function () { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
const scrollContainer = this.getClosestScrollableElementContainer(); |
| 114 |
const hasScrollbar = scrollContainer.offsetWidth > scrollContainer.scrollWidth; |
| 115 |
let overflowY; |
| 116 |
if ( !hasScrollbar ) { |
| 117 |
overflowY = scrollContainer.style.overflowY; |
| 118 |
scrollContainer.style.overflowY = 'hidden'; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
ve.ui.MWEditSummaryWidget.super.prototype.adjustSize.apply( this, arguments ); |
| 123 |
|
| 124 |
if ( !hasScrollbar ) { |
| 125 |
scrollContainer.style.overflowY = overflowY; |
| 126 |
} |
| 127 |
|
| 128 |
return this; |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
ve.ui.MWEditSummaryWidget.prototype.getSummaries = function () { |
| 137 |
const splitSummary = this.constructor.static.splitSummary.bind( this.constructor.static ); |
| 138 |
if ( !this.getSummariesPromise ) { |
| 139 |
if ( mw.user.isAnon() ) { |
| 140 |
this.getSummariesPromise = ve.createDeferred().resolve( [] ).promise(); |
| 141 |
} else { |
| 142 |
|
| 143 |
|
| 144 |
this.getSummariesPromise = ve.init.target.getLocalApi().get( { |
| 145 |
action: 'query', |
| 146 |
list: 'usercontribs', |
| 147 |
ucuser: mw.user.getName(), |
| 148 |
ucprop: 'comment', |
| 149 |
uclimit: 500 |
| 150 |
} ).then( ( response ) => { |
| 151 |
const usedComments = {}, |
| 152 |
changes = ve.getProp( response, 'query', 'usercontribs' ) || []; |
| 153 |
|
| 154 |
return changes |
| 155 |
|
| 156 |
.filter( ( change ) => Object.prototype.hasOwnProperty.call( change, 'comment' ) ) |
| 157 |
|
| 158 |
.map( ( change ) => splitSummary( change.comment ).comment.trim() ) |
| 159 |
|
| 160 |
.filter( ( comment ) => { |
| 161 |
if ( !comment || Object.prototype.hasOwnProperty.call( usedComments, comment ) ) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
usedComments[ comment ] = true; |
| 165 |
return true; |
| 166 |
} ) |
| 167 |
.sort(); |
| 168 |
} ); |
| 169 |
} |
| 170 |
} |
| 171 |
return this.getSummariesPromise; |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
ve.ui.MWEditSummaryWidget.prototype.getLookupRequest = function () { |
| 178 |
const query = this.constructor.static.splitSummary( this.value ), |
| 179 |
limit = this.limit; |
| 180 |
|
| 181 |
return this.getSummaries().then( ( allSummaries ) => { |
| 182 |
const matchingSummaries = this.constructor.static.getMatchingSummaries( allSummaries, query.comment ); |
| 183 |
if ( matchingSummaries.length > limit ) { |
| 184 |
|
| 185 |
matchingSummaries.length = limit; |
| 186 |
} |
| 187 |
return { summaries: matchingSummaries, section: query.section }; |
| 188 |
} ).promise( { abort: () => {} } ); |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
ve.ui.MWEditSummaryWidget.prototype.getLookupCacheDataFromResponse = function ( response ) { |
| 195 |
return response; |
| 196 |
}; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
ve.ui.MWEditSummaryWidget.prototype.getLookupMenuOptionsFromData = function ( data ) { |
| 202 |
return data.summaries.map( ( item ) => new OO.ui.MenuOptionWidget( { |
| 203 |
label: item, |
| 204 |
data: data.section + item |
| 205 |
} ) ); |
| 206 |
}; |