Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/nodes/ve.dm.ListNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel ListNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel list node.
9 *
10 * @class
11 * @extends ve.dm.BranchNode
12 *
13 * @constructor
14 * @param {Object} [element] Reference to element in linear model
15 * @param {ve.dm.Node[]} [children]
16 */
17 ve.dm.ListNode = function VeDmListNode() {
18 // Parent constructor
19 ve.dm.ListNode.super.apply( this, arguments );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.dm.ListNode, ve.dm.BranchNode );
25
26 /* Static Properties */
27
28 ve.dm.ListNode.static.name = 'list';
29
30 ve.dm.ListNode.static.childNodeTypes = [ 'listItem' ];
31
32 ve.dm.ListNode.static.defaultAttributes = {
33 style: 'bullet'
34 };
35
36 ve.dm.ListNode.static.matchTagNames = [ 'ul', 'ol' ];
37
38 ve.dm.ListNode.static.isDiffedAsList = true;
39
40 /**
41 * Creates a list item element
42 *
43 * @return {Object} Element data
44 */
45 ve.dm.ListNode.static.createItem = function () {
46 return { type: 'listItem' };
47 };
48
49 ve.dm.ListNode.static.toDataElement = function ( domElements ) {
50 const style = domElements[ 0 ].nodeName.toLowerCase() === 'ol' ? 'number' : 'bullet';
51 return { type: this.name, attributes: { style: style } };
52 };
53
54 ve.dm.ListNode.static.toDomElements = function ( dataElement, doc ) {
55 const tag = dataElement.attributes && dataElement.attributes.style === 'number' ? 'ol' : 'ul';
56 return [ doc.createElement( tag ) ];
57 };
58
59 ve.dm.ListNode.static.describeChanges = function ( attributeChanges, attributes, element ) {
60 attributeChanges = ve.copy( attributeChanges );
61 if ( 'listType' in attributeChanges ) {
62 let mapped = false;
63 [ 'from', 'to' ].forEach( ( fromOrTo ) => {
64 if ( attributeChanges.listType[ fromOrTo ] === 'definitionList' ) {
65 attributeChanges.style[ fromOrTo ] = 'indent';
66 mapped = true;
67 }
68 } );
69 if ( mapped ) {
70 delete attributeChanges.listType;
71 }
72 }
73 // Parent method
74 return ve.dm.ListNode.super.static.describeChanges.call( this, attributeChanges, attributes, element );
75 };
76
77 ve.dm.ListNode.static.describeChange = function ( key, change ) {
78 const messageKeys = {
79 bullet: 'visualeditor-listbutton-bullet-tooltip',
80 number: 'visualeditor-listbutton-number-tooltip',
81 indent: 'visualeditor-changedesc-list-style-indent'
82 };
83 if ( key === 'style' && change.from in messageKeys && change.to in messageKeys ) {
84 return ve.htmlMsg( 'visualeditor-changedesc-no-key',
85 // Message keys documented above
86 // eslint-disable-next-line mediawiki/msg-doc
87 this.wrapText( 'del', ve.msg( messageKeys[ change.from ] ) ),
88 // eslint-disable-next-line mediawiki/msg-doc
89 this.wrapText( 'ins', ve.msg( messageKeys[ change.to ] ) )
90 );
91 }
92 // Parent method
93 return ve.dm.ListNode.super.static.describeChange.apply( this, arguments );
94 };
95
96 /* Methods */
97
98 ve.dm.ListNode.prototype.canHaveSlugAfter = function () {
99 // A paragraph can be added after a list by pressing enter in an empty list item
100 return false;
101 };
102
103 /* Registration */
104
105 ve.dm.modelRegistry.register( ve.dm.ListNode );