| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
ve.ui.Context = function VeUiContext( surface, config ) { |
| 21 |
|
| 22 |
ve.ui.Context.super.call( this, config ); |
| 23 |
|
| 24 |
|
| 25 |
OO.EventEmitter.call( this ); |
| 26 |
OO.ui.mixin.GroupElement.call( this, config ); |
| 27 |
|
| 28 |
|
| 29 |
this.surface = surface; |
| 30 |
this.visible = false; |
| 31 |
this.choosing = false; |
| 32 |
|
| 33 |
this.$focusTrapBefore = $( '<div>' ).prop( 'tabIndex', 0 ); |
| 34 |
this.$focusTrapAfter = $( '<div>' ).prop( 'tabIndex', 0 ); |
| 35 |
this.$focusTrapBefore.add( this.$focusTrapAfter ).on( 'focus', () => { |
| 36 |
surface.getView().activate(); |
| 37 |
} ); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
this.$group.addClass( 've-ui-context-menu' ); |
| 43 |
this.$element |
| 44 |
.addClass( 've-ui-context ve-ui-context-hidden' ) |
| 45 |
.append( this.$focusTrapBefore, this.$group, this.$focusTrapAfter ); |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
OO.inheritClass( ve.ui.Context, OO.ui.Element ); |
| 51 |
|
| 52 |
OO.mixinClass( ve.ui.Context, OO.EventEmitter ); |
| 53 |
|
| 54 |
OO.mixinClass( ve.ui.Context, OO.ui.mixin.GroupElement ); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
ve.ui.Context.static.isMobile = false; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
ve.ui.Context.prototype.isMobile = function () { |
| 81 |
return this.constructor.static.isMobile; |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
ve.ui.Context.prototype.isVisible = function () { |
| 90 |
return this.visible; |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
ve.ui.Context.prototype.getRelatedSources = null; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
ve.ui.Context.prototype.getSurface = function () { |
| 113 |
return this.surface; |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
ve.ui.Context.prototype.hide = function () { |
| 123 |
const surfaceModel = this.surface.getModel(); |
| 124 |
this.toggleMenu( false ); |
| 125 |
this.toggle( false ); |
| 126 |
|
| 127 |
|
| 128 |
surfaceModel.once( 'select', () => { |
| 129 |
surfaceModel.emitContextChange(); |
| 130 |
} ); |
| 131 |
|
| 132 |
|
| 133 |
this.getSurface().getView().contexedAnnotations = []; |
| 134 |
}; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
ve.ui.Context.prototype.toggleMenu = function ( show ) { |
| 144 |
show = show === undefined ? !this.choosing : !!show; |
| 145 |
|
| 146 |
if ( show !== this.choosing ) { |
| 147 |
this.choosing = show; |
| 148 |
this.$element.toggleClass( 've-ui-context-choosing', show ); |
| 149 |
if ( show ) { |
| 150 |
this.setupMenuItems(); |
| 151 |
} else { |
| 152 |
this.teardownMenuItems(); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return this; |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
ve.ui.Context.prototype.setupMenuItems = function ( previousItems ) { |
| 169 |
const sources = this.getRelatedSources(), |
| 170 |
items = []; |
| 171 |
|
| 172 |
let i, len; |
| 173 |
for ( i = 0, len = sources.length; i < len; i++ ) { |
| 174 |
const source = sources[ i ]; |
| 175 |
if ( source.type === 'item' ) { |
| 176 |
items.push( ve.ui.contextItemFactory.create( |
| 177 |
sources[ i ].name, this, sources[ i ].model |
| 178 |
) ); |
| 179 |
} else if ( source.type === 'tool' ) { |
| 180 |
items.push( new ve.ui.ToolContextItem( |
| 181 |
this, sources[ i ].model, ve.ui.toolFactory.lookup( sources[ i ].name ) |
| 182 |
) ); |
| 183 |
} else if ( source.type === 'persistent' ) { |
| 184 |
items.push( ve.ui.contextItemFactory.create( |
| 185 |
sources[ i ].name, this, sources[ i ].data |
| 186 |
) ); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
items.sort( ( a, b ) => a.constructor.static.sortOrder - b.constructor.static.sortOrder ); |
| 191 |
|
| 192 |
this.addItems( items ); |
| 193 |
for ( i = 0, len = items.length; i < len; i++ ) { |
| 194 |
const item = items[ i ]; |
| 195 |
const isRefreshing = previousItems && previousItems.some( |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
( oldItem ) => oldItem.equals( item ) || |
| 200 |
oldItem.getFragment().isNull() || |
| 201 |
item.getFragment().isNull() |
| 202 |
); |
| 203 |
item.connect( this, { command: 'onContextItemCommand' } ); |
| 204 |
item.setup( isRefreshing ); |
| 205 |
} |
| 206 |
|
| 207 |
return this; |
| 208 |
}; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
ve.ui.Context.prototype.teardownMenuItems = function () { |
| 218 |
for ( let i = 0, len = this.items.length; i < len; i++ ) { |
| 219 |
this.items[ i ].teardown(); |
| 220 |
} |
| 221 |
this.clearItems(); |
| 222 |
|
| 223 |
return this; |
| 224 |
}; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
ve.ui.Context.prototype.onContextItemCommand = function () {}; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
ve.ui.Context.prototype.toggle = function ( show ) { |
| 239 |
show = show === undefined ? !this.visible : !!show; |
| 240 |
if ( show !== this.visible ) { |
| 241 |
this.visible = show; |
| 242 |
this.$element.toggleClass( 've-ui-context-hidden', !this.visible ); |
| 243 |
} |
| 244 |
this.emit( 'resize' ); |
| 245 |
return ve.createDeferred().resolve().promise(); |
| 246 |
}; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
ve.ui.Context.prototype.updateDimensions = function () { |
| 256 |
|
| 257 |
this.emit( 'resize' ); |
| 258 |
return this; |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
ve.ui.Context.prototype.destroy = function () { |
| 268 |
|
| 269 |
this.surface.getModel().disconnect( this ); |
| 270 |
|
| 271 |
this.$element.remove(); |
| 272 |
return this; |
| 273 |
}; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
ve.ui.Context.prototype.getSurfacePadding = function () { |
| 284 |
return null; |
| 285 |
}; |