| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
ve.dm.MWReferenceModel = function VeDmMWReferenceModel( parentDoc ) { |
| 23 |
|
| 24 |
OO.EventEmitter.call( this ); |
| 25 |
|
| 26 |
|
| 27 |
this.extendsRef = null; |
| 28 |
this.listKey = ''; |
| 29 |
this.listGroup = ''; |
| 30 |
this.listIndex = null; |
| 31 |
this.group = ''; |
| 32 |
if ( parentDoc ) { |
| 33 |
this.doc = () => parentDoc.cloneWithData( [ |
| 34 |
{ type: 'paragraph', internal: { generated: 'wrapper' } }, |
| 35 |
{ type: '/paragraph' }, |
| 36 |
{ type: 'internalList' }, |
| 37 |
{ type: '/internalList' } |
| 38 |
] ); |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
OO.mixinClass( ve.dm.MWReferenceModel, OO.EventEmitter ); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
ve.dm.MWReferenceModel.static.newFromReferenceNode = function ( node ) { |
| 55 |
const doc = node.getDocument(); |
| 56 |
const internalList = doc.getInternalList(); |
| 57 |
const attributes = node.getAttributes(); |
| 58 |
const ref = new ve.dm.MWReferenceModel(); |
| 59 |
|
| 60 |
ref.extendsRef = attributes.extendsRef; |
| 61 |
ref.listKey = attributes.listKey; |
| 62 |
ref.listGroup = attributes.listGroup; |
| 63 |
ref.listIndex = attributes.listIndex; |
| 64 |
ref.group = attributes.refGroup; |
| 65 |
ref.doc = function () { |
| 66 |
|
| 67 |
return doc.cloneFromRange( internalList.getItemNode( attributes.listIndex ).getRange() ); |
| 68 |
}; |
| 69 |
|
| 70 |
return ref; |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
ve.dm.MWReferenceModel.prototype.findInternalItem = function ( surfaceModel ) { |
| 82 |
if ( this.listIndex !== null ) { |
| 83 |
return surfaceModel.getDocument().getInternalList().getItemNode( this.listIndex ); |
| 84 |
} |
| 85 |
return null; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
ve.dm.MWReferenceModel.prototype.insertInternalItem = function ( surfaceModel ) { |
| 97 |
|
| 98 |
const doc = surfaceModel.getDocument(); |
| 99 |
const internalList = doc.getInternalList(); |
| 100 |
|
| 101 |
|
| 102 |
this.listKey = 'auto/' + internalList.getNextUniqueNumber(); |
| 103 |
this.listGroup = 'mwReference/' + this.group; |
| 104 |
|
| 105 |
|
| 106 |
const item = internalList.getItemInsertion( this.listGroup, this.listKey, [] ); |
| 107 |
surfaceModel.change( item.transaction ); |
| 108 |
this.listIndex = item.index; |
| 109 |
|
| 110 |
|
| 111 |
surfaceModel.change( |
| 112 |
ve.dm.TransactionBuilder.static.newFromDocumentInsertion( |
| 113 |
doc, |
| 114 |
internalList.getItemNode( item.index ).getRange().start, |
| 115 |
this.getDocument() |
| 116 |
) |
| 117 |
); |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
ve.dm.MWReferenceModel.prototype.updateInternalItem = function ( surfaceModel ) { |
| 128 |
const doc = surfaceModel.getDocument(); |
| 129 |
const internalList = doc.getInternalList(); |
| 130 |
const listGroup = 'mwReference/' + this.group; |
| 131 |
|
| 132 |
|
| 133 |
if ( this.listGroup !== listGroup ) { |
| 134 |
|
| 135 |
const group = internalList.getNodeGroup( this.listGroup ); |
| 136 |
const refNodes = group.keyedNodes[ this.listKey ] ? |
| 137 |
group.keyedNodes[ this.listKey ].slice() : |
| 138 |
[ group.firstNodes[ this.listIndex ] ]; |
| 139 |
|
| 140 |
const keyIndex = internalList.getKeyIndex( this.listGroup, this.listKey ); |
| 141 |
if ( keyIndex !== undefined ) { |
| 142 |
|
| 143 |
this.listKey = 'auto/' + internalList.getNextUniqueNumber(); |
| 144 |
} |
| 145 |
|
| 146 |
const txs = []; |
| 147 |
for ( let i = 0, len = refNodes.length; i < len; i++ ) { |
| 148 |
txs.push( ve.dm.TransactionBuilder.static.newFromAttributeChanges( |
| 149 |
doc, |
| 150 |
refNodes[ i ].getOuterRange().start, |
| 151 |
{ refGroup: this.group, listGroup: listGroup } |
| 152 |
) ); |
| 153 |
} |
| 154 |
surfaceModel.change( txs ); |
| 155 |
this.listGroup = listGroup; |
| 156 |
} |
| 157 |
|
| 158 |
const itemNodeRange = internalList.getItemNode( this.listIndex ).getRange(); |
| 159 |
surfaceModel.change( |
| 160 |
ve.dm.TransactionBuilder.static |
| 161 |
.newFromRemoval( doc, itemNodeRange, true ) ); |
| 162 |
surfaceModel.change( |
| 163 |
ve.dm.TransactionBuilder.static |
| 164 |
.newFromDocumentInsertion( doc, itemNodeRange.start, this.getDocument() ) ); |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
ve.dm.MWReferenceModel.prototype.insertReferenceNode = function ( surfaceFragment, placeholder ) { |
| 174 |
const attributes = { |
| 175 |
extendsRef: this.extendsRef, |
| 176 |
listKey: this.listKey, |
| 177 |
listGroup: this.listGroup, |
| 178 |
listIndex: this.listIndex, |
| 179 |
refGroup: this.group |
| 180 |
}; |
| 181 |
if ( placeholder ) { |
| 182 |
attributes.placeholder = true; |
| 183 |
} |
| 184 |
surfaceFragment |
| 185 |
.insertContent( [ |
| 186 |
{ |
| 187 |
type: 'mwReference', |
| 188 |
attributes: attributes, |
| 189 |
|
| 190 |
originalDomElementsHash: Math.random() |
| 191 |
}, |
| 192 |
{ type: '/mwReference' } |
| 193 |
] ); |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
ve.dm.MWReferenceModel.prototype.getListKey = function () { |
| 202 |
return this.listKey; |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
ve.dm.MWReferenceModel.prototype.getListGroup = function () { |
| 211 |
return this.listGroup; |
| 212 |
}; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
ve.dm.MWReferenceModel.prototype.getListIndex = function () { |
| 220 |
return this.listIndex; |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
ve.dm.MWReferenceModel.prototype.getGroup = function () { |
| 229 |
return this.group; |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
ve.dm.MWReferenceModel.prototype.getDocument = function () { |
| 240 |
if ( typeof this.doc === 'function' ) { |
| 241 |
this.doc = this.doc(); |
| 242 |
} |
| 243 |
return this.doc; |
| 244 |
}; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
ve.dm.MWReferenceModel.prototype.setGroup = function ( group ) { |
| 252 |
this.group = group; |
| 253 |
}; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
ve.dm.MWReferenceModel.prototype.setDocument = function ( doc ) { |
| 261 |
this.doc = doc; |
| 262 |
}; |