| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ve.dm.InternalList = function VeDmInternalList( doc ) { |
| 17 |
|
| 18 |
OO.EventEmitter.call( this ); |
| 19 |
|
| 20 |
|
| 21 |
this.document = doc; |
| 22 |
this.itemHtmlQueue = []; |
| 23 |
this.listNode = null; |
| 24 |
this.nodes = {}; |
| 25 |
this.groupsChanged = []; |
| 26 |
this.keyIndexes = {}; |
| 27 |
this.keys = []; |
| 28 |
|
| 29 |
|
| 30 |
doc.connect( this, { transact: 'onTransact' } ); |
| 31 |
}; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
OO.mixinClass( ve.dm.InternalList, OO.EventEmitter ); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
ve.dm.InternalList.prototype.queueItemHtml = function ( groupName, key, html ) { |
| 59 |
let isNew = false, |
| 60 |
index = this.getKeyIndex( groupName, key ); |
| 61 |
|
| 62 |
if ( index === undefined ) { |
| 63 |
index = this.itemHtmlQueue.length; |
| 64 |
this.keyIndexes[ groupName + '/' + key ] = index; |
| 65 |
this.itemHtmlQueue.push( html ); |
| 66 |
isNew = true; |
| 67 |
} else if ( this.itemHtmlQueue[ index ] === '' ) { |
| 68 |
|
| 69 |
this.itemHtmlQueue[ index ] = html; |
| 70 |
isNew = true; |
| 71 |
} |
| 72 |
return { |
| 73 |
index: index, |
| 74 |
isNew: isNew |
| 75 |
}; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
ve.dm.InternalList.prototype.getItemHtmlQueue = function () { |
| 84 |
return this.itemHtmlQueue; |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
ve.dm.InternalList.prototype.getDocument = function () { |
| 93 |
return this.document; |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
ve.dm.InternalList.prototype.getListNode = function () { |
| 102 |
|
| 103 |
if ( !this.listNode || !this.listNode.doc ) { |
| 104 |
const nodes = this.getDocument().getDocumentNode().children; |
| 105 |
for ( let i = nodes.length; i >= 0; i-- ) { |
| 106 |
if ( nodes[ i ] instanceof ve.dm.InternalListNode ) { |
| 107 |
this.listNode = nodes[ i ]; |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
return this.listNode; |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
ve.dm.InternalList.prototype.getItemNodeCount = function () { |
| 121 |
return this.getListNode().children.length; |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
ve.dm.InternalList.prototype.getItemNode = function ( index ) { |
| 131 |
return this.getListNode().children[ index ]; |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
ve.dm.InternalList.prototype.getNodeGroups = function () { |
| 140 |
return this.nodes; |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
ve.dm.InternalList.prototype.getNodeGroup = function ( groupName ) { |
| 150 |
return this.nodes[ groupName ]; |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
ve.dm.InternalList.prototype.getUniqueListKey = function ( groupName, oldListKey, prefix ) { |
| 166 |
const group = this.getNodeGroup( groupName ); |
| 167 |
|
| 168 |
if ( group.uniqueListKeys[ oldListKey ] !== undefined ) { |
| 169 |
return group.uniqueListKeys[ oldListKey ]; |
| 170 |
} |
| 171 |
|
| 172 |
let num = 0; |
| 173 |
while ( group.keyedNodes[ prefix + num ] || group.uniqueListKeysInUse[ prefix + num ] ) { |
| 174 |
num++; |
| 175 |
} |
| 176 |
|
| 177 |
group.uniqueListKeys[ oldListKey ] = prefix + num; |
| 178 |
group.uniqueListKeysInUse[ prefix + num ] = true; |
| 179 |
return prefix + num; |
| 180 |
}; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
ve.dm.InternalList.prototype.getNextUniqueNumber = function () { |
| 188 |
const doc = this.getDocument(); |
| 189 |
const number = ( doc.getStorage( 'internallist-counter' ) || 0 ); |
| 190 |
doc.setStorage( 'internallist-counter', number + 1 ); |
| 191 |
return number; |
| 192 |
}; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
ve.dm.InternalList.prototype.convertToData = function ( converter, doc ) { |
| 207 |
const itemHtmlQueue = this.getItemHtmlQueue(); |
| 208 |
|
| 209 |
const list = []; |
| 210 |
list.push( { type: 'internalList' } ); |
| 211 |
for ( let i = 0, length = itemHtmlQueue.length; i < length; i++ ) { |
| 212 |
if ( itemHtmlQueue[ i ] !== '' ) { |
| 213 |
const div = doc.createElement( 'div' ); |
| 214 |
div.innerHTML = itemHtmlQueue[ i ]; |
| 215 |
const itemData = [].concat( |
| 216 |
{ type: 'internalItem' }, |
| 217 |
converter.getDataFromDomSubtree( div ), |
| 218 |
{ type: '/internalItem' } |
| 219 |
); |
| 220 |
if ( !converter.isFromClipboard() ) { |
| 221 |
itemData[ 0 ].attributes = { originalHtml: itemHtmlQueue[ i ] }; |
| 222 |
} |
| 223 |
ve.batchPush( list, itemData ); |
| 224 |
} else { |
| 225 |
list.push( { type: 'internalItem' }, { type: '/internalItem' } ); |
| 226 |
} |
| 227 |
} |
| 228 |
list.push( { type: '/internalList' } ); |
| 229 |
|
| 230 |
this.itemHtmlQueue = []; |
| 231 |
return list; |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
ve.dm.InternalList.prototype.getItemInsertion = function ( groupName, key, data ) { |
| 244 |
let index = this.getKeyIndex( groupName, key ); |
| 245 |
|
| 246 |
let tx; |
| 247 |
if ( index === undefined ) { |
| 248 |
index = this.getItemNodeCount(); |
| 249 |
this.keyIndexes[ groupName + '/' + key ] = index; |
| 250 |
|
| 251 |
const itemData = [].concat( { type: 'internalItem' }, data, { type: '/internalItem' } ); |
| 252 |
tx = ve.dm.TransactionBuilder.static.newFromInsertion( |
| 253 |
this.getDocument(), |
| 254 |
this.getListNode().getRange().end, |
| 255 |
itemData |
| 256 |
); |
| 257 |
} else { |
| 258 |
tx = null; |
| 259 |
} |
| 260 |
|
| 261 |
return { |
| 262 |
transaction: tx, |
| 263 |
index: index |
| 264 |
}; |
| 265 |
}; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
ve.dm.InternalList.prototype.getIndexPosition = function ( groupName, key ) { |
| 275 |
return this.nodes[ groupName ].indexOrder.indexOf( key ); |
| 276 |
}; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
ve.dm.InternalList.prototype.getKeyIndex = function ( groupName, key ) { |
| 286 |
return this.keyIndexes[ groupName + '/' + key ]; |
| 287 |
}; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
ve.dm.InternalList.prototype.addNode = function ( groupName, key, index, node ) { |
| 298 |
let group = this.nodes[ groupName ]; |
| 299 |
|
| 300 |
if ( group === undefined ) { |
| 301 |
group = this.nodes[ groupName ] = { |
| 302 |
keyedNodes: {}, |
| 303 |
firstNodes: [], |
| 304 |
indexOrder: [], |
| 305 |
uniqueListKeys: {}, |
| 306 |
uniqueListKeysInUse: {} |
| 307 |
}; |
| 308 |
} |
| 309 |
let keyedNodes = group.keyedNodes[ key ]; |
| 310 |
this.keys[ index ] = key; |
| 311 |
|
| 312 |
if ( keyedNodes === undefined ) { |
| 313 |
keyedNodes = group.keyedNodes[ key ] = []; |
| 314 |
} |
| 315 |
if ( node.getDocument().buildingNodeTree ) { |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
keyedNodes.push( node ); |
| 320 |
if ( keyedNodes.length === 1 ) { |
| 321 |
group.firstNodes[ index ] = node; |
| 322 |
} |
| 323 |
} else { |
| 324 |
|
| 325 |
const start = node.getRange().start; |
| 326 |
let i, len; |
| 327 |
for ( i = 0, len = keyedNodes.length; i < len; i++ ) { |
| 328 |
if ( start < keyedNodes[ i ].getRange().start ) { |
| 329 |
break; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
keyedNodes.splice( i, 0, node ); |
| 334 |
if ( i === 0 ) { |
| 335 |
group.firstNodes[ index ] = node; |
| 336 |
} |
| 337 |
} |
| 338 |
if ( group.indexOrder.indexOf( index ) === -1 ) { |
| 339 |
group.indexOrder.push( index ); |
| 340 |
} |
| 341 |
this.markGroupAsChanged( groupName ); |
| 342 |
}; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
ve.dm.InternalList.prototype.markGroupAsChanged = function ( groupName ) { |
| 350 |
if ( this.groupsChanged.indexOf( groupName ) === -1 ) { |
| 351 |
this.groupsChanged.push( groupName ); |
| 352 |
} |
| 353 |
}; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
ve.dm.InternalList.prototype.onTransact = function () { |
| 361 |
if ( this.groupsChanged.length > 0 ) { |
| 362 |
|
| 363 |
for ( let i = 0; i < this.groupsChanged.length; i++ ) { |
| 364 |
this.sortGroupIndexes( this.nodes[ this.groupsChanged[ i ] ] ); |
| 365 |
} |
| 366 |
this.emit( 'update', this.groupsChanged ); |
| 367 |
this.groupsChanged = []; |
| 368 |
} |
| 369 |
}; |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
ve.dm.InternalList.prototype.removeNode = function ( groupName, key, index, node ) { |
| 380 |
const group = this.nodes[ groupName ]; |
| 381 |
const keyedNodes = group.keyedNodes[ key ]; |
| 382 |
for ( let i = 0, len = keyedNodes.length; i < len; i++ ) { |
| 383 |
if ( keyedNodes[ i ] === node ) { |
| 384 |
keyedNodes.splice( i, 1 ); |
| 385 |
if ( i === 0 ) { |
| 386 |
group.firstNodes[ index ] = keyedNodes[ 0 ]; |
| 387 |
} |
| 388 |
break; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
if ( keyedNodes.length === 0 ) { |
| 394 |
delete group.keyedNodes[ key ]; |
| 395 |
delete group.firstNodes[ index ]; |
| 396 |
const j = group.indexOrder.indexOf( index ); |
| 397 |
group.indexOrder.splice( j, 1 ); |
| 398 |
} |
| 399 |
this.markGroupAsChanged( groupName ); |
| 400 |
}; |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
ve.dm.InternalList.prototype.sortGroupIndexes = function ( group ) { |
| 412 |
|
| 413 |
group.indexOrder.sort( ( index1, index2 ) => { |
| 414 |
|
| 415 |
if ( !group.firstNodes[ index1 ] ) { |
| 416 |
return !group.firstNodes[ index2 ] ? 0 : 1; |
| 417 |
} |
| 418 |
|
| 419 |
if ( group.firstNodes[ index1 ].getAttribute( 'placeholder' ) ) { |
| 420 |
return group.firstNodes[ index2 ].getAttribute( 'placeholder' ) ? 0 : 1; |
| 421 |
} |
| 422 |
return group.firstNodes[ index1 ].getRange().start - group.firstNodes[ index2 ].getRange().start; |
| 423 |
} ); |
| 424 |
}; |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
ve.dm.InternalList.prototype.clone = function ( doc ) { |
| 433 |
const clone = new this.constructor( doc || this.getDocument() ); |
| 434 |
|
| 435 |
|
| 436 |
clone.itemHtmlQueue = ve.copy( this.itemHtmlQueue ); |
| 437 |
return clone; |
| 438 |
}; |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
ve.dm.InternalList.prototype.merge = function ( list, commonLength ) { |
| 453 |
const listLen = list.getItemNodeCount(), |
| 454 |
newItemRanges = [], |
| 455 |
mapping = {}; |
| 456 |
let nextIndex = this.getItemNodeCount(); |
| 457 |
|
| 458 |
for ( let i = 0; i < commonLength; i++ ) { |
| 459 |
mapping[ i ] = i; |
| 460 |
} |
| 461 |
for ( let i = commonLength; i < listLen; i++ ) { |
| 462 |
|
| 463 |
let key = null; |
| 464 |
for ( const k in list.keyIndexes ) { |
| 465 |
if ( list.keyIndexes[ k ] === i ) { |
| 466 |
key = k; |
| 467 |
break; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
if ( this.keyIndexes[ key ] !== undefined ) { |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
mapping[ i ] = this.keyIndexes[ key ]; |
| 478 |
} else { |
| 479 |
mapping[ i ] = nextIndex; |
| 480 |
if ( key !== null ) { |
| 481 |
this.keyIndexes[ key ] = nextIndex; |
| 482 |
} |
| 483 |
nextIndex++; |
| 484 |
newItemRanges.push( list.getItemNode( i ).getOuterRange() ); |
| 485 |
} |
| 486 |
} |
| 487 |
return { |
| 488 |
mapping: mapping, |
| 489 |
newItemRanges: newItemRanges |
| 490 |
}; |
| 491 |
}; |