Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/nodes/ve.ce.CheckListItemNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable CheckListItemNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * ContentEditable list item node.
9 *
10 * @class
11 * @extends ve.ce.BranchNode
12 * @constructor
13 * @param {ve.dm.CheckListItemNode} model Model to observe
14 * @param {Object} [config] Configuration options
15 */
16 ve.ce.CheckListItemNode = function VeCeCheckListItemNode() {
17 // Parent constructor
18 ve.ce.CheckListItemNode.super.apply( this, arguments );
19
20 this.$element.addClass( 've-ce-checkListItemNode' );
21
22 // Events
23 this.model.connect( this, { attributeChange: 'onAttributeChange' } );
24 this.$element.on( 'click', this.onClick.bind( this ) );
25
26 this.updateChecked();
27 };
28
29 /* Inheritance */
30
31 OO.inheritClass( ve.ce.CheckListItemNode, ve.ce.BranchNode );
32
33 /* Static Properties */
34
35 ve.ce.CheckListItemNode.static.name = 'checkListItem';
36
37 ve.ce.CheckListItemNode.static.tagName = 'li';
38
39 ve.ce.CheckListItemNode.static.splitOnEnter = true;
40
41 /* Methods */
42
43 /**
44 * Handle click events on the checkbox
45 *
46 * @param {jQuery.Event} e Click event
47 */
48 ve.ce.CheckListItemNode.prototype.onClick = function ( e ) {
49 if ( e.target === this.$element[ 0 ] ) {
50 // TODO: This should probably live in ui.Actions.
51 const fragment = this.getRoot().getSurface().getModel().getLinearFragment( this.getOuterRange(), true );
52 fragment.changeAttributes( { checked: !this.getModel().getAttribute( 'checked' ) } );
53 }
54 };
55
56 /**
57 * @param {string} key Attribute key
58 * @param {string} from Old value
59 * @param {string} to New value
60 */
61 ve.ce.CheckListItemNode.prototype.onAttributeChange = function ( key ) {
62 if ( key === 'checked' ) {
63 this.updateChecked();
64 }
65 };
66
67 ve.ce.CheckListItemNode.prototype.updateChecked = function () {
68 this.$element.toggleClass( 've-ce-checkListItemNode-checked', !!this.getModel().getAttribute( 'checked' ) );
69 };
70
71 /* Registration */
72
73 ve.ce.nodeFactory.register( ve.ce.CheckListItemNode );