Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/dm/ve.dm.example.StubReferenceNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel StubReferenceNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license MIT
6 */
7
8 /**
9 * @class
10 * @extends ve.dm.LeafNode
11 * @mixin ve.dm.FocusableNode
12 *
13 * @constructor
14 * @param {Object} [element] Reference to element in linear model
15 */
16 ve.dm.example.StubReferenceNode = function VeDmStubReferenceNode() {
17 // Parent constructor
18 ve.dm.example.StubReferenceNode.super.apply( this, arguments );
19
20 // Mixin constructors
21 ve.dm.FocusableNode.call( this );
22
23 // Event handlers
24 this.connect( this, {
25 root: 'onRoot',
26 unroot: 'onUnroot',
27 attributeChange: 'onAttributeChange'
28 } );
29 };
30
31 /* Inheritance */
32
33 OO.inheritClass( ve.dm.example.StubReferenceNode, ve.dm.LeafNode );
34
35 OO.mixinClass( ve.dm.example.StubReferenceNode, ve.dm.FocusableNode );
36
37 /* Static members */
38
39 ve.dm.example.StubReferenceNode.static.name = 'stubReference';
40
41 ve.dm.example.StubReferenceNode.static.matchTagNames = [ 'ref' ];
42
43 ve.dm.example.StubReferenceNode.static.isContent = true;
44
45 ve.dm.example.StubReferenceNode.static.handlesOwnChildren = true;
46
47 /**
48 * Regular expression for parsing the listKey attribute
49 *
50 * Use [\s\S]* instead of .* to catch esoteric whitespace (T263698)
51 *
52 * @static
53 * @property {RegExp}
54 * @inheritable
55 */
56 ve.dm.example.StubReferenceNode.static.listKeyRegex = /^(auto|literal)\/([\s\S]*)$/;
57
58 ve.dm.example.StubReferenceNode.static.toDataElement = function ( domElements, converter ) {
59 const refElement = domElements[ 0 ];
60 const body = refElement.innerHTML;
61 const refGroup = refElement.getAttribute( 'group' ) || '';
62 const listGroup = this.name + '/' + refGroup;
63 const name = refElement.getAttribute( 'name' );
64 const listKey = name ?
65 'literal/' + name :
66 'auto/' + converter.internalList.getNextUniqueNumber();
67 const queueResult = converter.internalList.queueItemHtml( listGroup, listKey, body );
68 const listIndex = queueResult.index;
69 const contentsUsed = ( body !== '' && queueResult.isNew );
70
71 const dataElement = {
72 type: this.name,
73 attributes: {
74 listIndex: listIndex,
75 listGroup: listGroup,
76 listKey: listKey,
77 refGroup: refGroup,
78 contentsUsed: contentsUsed
79 }
80 };
81 return dataElement;
82 };
83
84 ve.dm.example.StubReferenceNode.static.toDomElements = function ( dataElements, doc ) {
85 return [ doc.createElement( 'ref' ) ];
86 };
87
88 /**
89 * Handle the node being attached to the root
90 */
91 ve.dm.example.StubReferenceNode.prototype.onRoot = function () {
92 this.addToInternalList();
93 };
94
95 /**
96 * Handle the node being detached from the root
97 *
98 * @param {ve.dm.DocumentNode} oldRoot Old document root
99 */
100 ve.dm.example.StubReferenceNode.prototype.onUnroot = function ( oldRoot ) {
101 if ( this.getDocument().getDocumentNode() === oldRoot ) {
102 this.removeFromInternalList();
103 }
104 };
105
106 /**
107 * Register the node with the internal list
108 */
109 ve.dm.example.StubReferenceNode.prototype.addToInternalList = function () {
110 if ( this.getRoot() === this.getDocument().getDocumentNode() ) {
111 this.registeredListGroup = this.element.attributes.listGroup;
112 this.registeredListKey = this.element.attributes.listKey;
113 this.registeredListIndex = this.element.attributes.listIndex;
114 this.getDocument().getInternalList().addNode(
115 this.registeredListGroup,
116 this.registeredListKey,
117 this.registeredListIndex,
118 this
119 );
120 }
121 };
122
123 /**
124 * Unregister the node from the internal list
125 */
126 ve.dm.example.StubReferenceNode.prototype.removeFromInternalList = function () {
127 if ( !this.registeredListGroup ) {
128 // Don't try to remove if we haven't been added in the first place.
129 return;
130 }
131 this.getDocument().getInternalList().removeNode(
132 this.registeredListGroup,
133 this.registeredListKey,
134 this.registeredListIndex,
135 this
136 );
137 };
138
139 ve.dm.example.StubReferenceNode.prototype.onAttributeChange = function ( key, from, to ) {
140 if (
141 ( key !== 'listGroup' && key !== 'listKey' ) ||
142 ( key === 'listGroup' && this.registeredListGroup === to ) ||
143 ( key === 'listKey' && this.registeredListKey === to )
144 ) {
145 return;
146 }
147
148 // Need the old list keys and indexes, so we register them in addToInternalList
149 // They've already been updated in this.element.attributes before this code runs
150 this.removeFromInternalList();
151 this.addToInternalList();
152 };
153
154 /* Registration */
155
156 ve.dm.modelRegistry.register( ve.dm.example.StubReferenceNode );