File: ve.dm.example.StubReferenceNode.js

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (156 lines) | stat: -rw-r--r-- 4,263 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*!
 * VisualEditor DataModel StubReferenceNode class.
 *
 * @copyright See AUTHORS.txt
 * @license MIT
 */

/**
 * @class
 * @extends ve.dm.LeafNode
 * @mixin ve.dm.FocusableNode
 *
 * @constructor
 * @param {Object} [element] Reference to element in linear model
 */
ve.dm.example.StubReferenceNode = function VeDmStubReferenceNode() {
	// Parent constructor
	ve.dm.example.StubReferenceNode.super.apply( this, arguments );

	// Mixin constructors
	ve.dm.FocusableNode.call( this );

	// Event handlers
	this.connect( this, {
		root: 'onRoot',
		unroot: 'onUnroot',
		attributeChange: 'onAttributeChange'
	} );
};

/* Inheritance */

OO.inheritClass( ve.dm.example.StubReferenceNode, ve.dm.LeafNode );

OO.mixinClass( ve.dm.example.StubReferenceNode, ve.dm.FocusableNode );

/* Static members */

ve.dm.example.StubReferenceNode.static.name = 'stubReference';

ve.dm.example.StubReferenceNode.static.matchTagNames = [ 'ref' ];

ve.dm.example.StubReferenceNode.static.isContent = true;

ve.dm.example.StubReferenceNode.static.handlesOwnChildren = true;

/**
 * Regular expression for parsing the listKey attribute
 *
 * Use [\s\S]* instead of .* to catch esoteric whitespace (T263698)
 *
 * @static
 * @property {RegExp}
 * @inheritable
 */
ve.dm.example.StubReferenceNode.static.listKeyRegex = /^(auto|literal)\/([\s\S]*)$/;

ve.dm.example.StubReferenceNode.static.toDataElement = function ( domElements, converter ) {
	const refElement = domElements[ 0 ];
	const body = refElement.innerHTML;
	const refGroup = refElement.getAttribute( 'group' ) || '';
	const listGroup = this.name + '/' + refGroup;
	const name = refElement.getAttribute( 'name' );
	const listKey = name ?
		'literal/' + name :
		'auto/' + converter.internalList.getNextUniqueNumber();
	const queueResult = converter.internalList.queueItemHtml( listGroup, listKey, body );
	const listIndex = queueResult.index;
	const contentsUsed = ( body !== '' && queueResult.isNew );

	const dataElement = {
		type: this.name,
		attributes: {
			listIndex: listIndex,
			listGroup: listGroup,
			listKey: listKey,
			refGroup: refGroup,
			contentsUsed: contentsUsed
		}
	};
	return dataElement;
};

ve.dm.example.StubReferenceNode.static.toDomElements = function ( dataElements, doc ) {
	return [ doc.createElement( 'ref' ) ];
};

/**
 * Handle the node being attached to the root
 */
ve.dm.example.StubReferenceNode.prototype.onRoot = function () {
	this.addToInternalList();
};

/**
 * Handle the node being detached from the root
 *
 * @param {ve.dm.DocumentNode} oldRoot Old document root
 */
ve.dm.example.StubReferenceNode.prototype.onUnroot = function ( oldRoot ) {
	if ( this.getDocument().getDocumentNode() === oldRoot ) {
		this.removeFromInternalList();
	}
};

/**
 * Register the node with the internal list
 */
ve.dm.example.StubReferenceNode.prototype.addToInternalList = function () {
	if ( this.getRoot() === this.getDocument().getDocumentNode() ) {
		this.registeredListGroup = this.element.attributes.listGroup;
		this.registeredListKey = this.element.attributes.listKey;
		this.registeredListIndex = this.element.attributes.listIndex;
		this.getDocument().getInternalList().addNode(
			this.registeredListGroup,
			this.registeredListKey,
			this.registeredListIndex,
			this
		);
	}
};

/**
 * Unregister the node from the internal list
 */
ve.dm.example.StubReferenceNode.prototype.removeFromInternalList = function () {
	if ( !this.registeredListGroup ) {
		// Don't try to remove if we haven't been added in the first place.
		return;
	}
	this.getDocument().getInternalList().removeNode(
		this.registeredListGroup,
		this.registeredListKey,
		this.registeredListIndex,
		this
	);
};

ve.dm.example.StubReferenceNode.prototype.onAttributeChange = function ( key, from, to ) {
	if (
		( key !== 'listGroup' && key !== 'listKey' ) ||
		( key === 'listGroup' && this.registeredListGroup === to ) ||
		( key === 'listKey' && this.registeredListKey === to )
	) {
		return;
	}

	// Need the old list keys and indexes, so we register them in addToInternalList
	// They've already been updated in this.element.attributes before this code runs
	this.removeFromInternalList();
	this.addToInternalList();
};

/* Registration */

ve.dm.modelRegistry.register( ve.dm.example.StubReferenceNode );