Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/widgets/ve.ui.AuthorItemWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface AuthorItemWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /* global CP */
9
10 /**
11 * UserInterface AuthorItemWidget
12 *
13 * @class
14 * @extends OO.ui.Widget
15 * @mixes OO.ui.mixin.IconElement
16 * @mixes OO.ui.mixin.LabelElement
17 *
18 * @constructor
19 * @param {ve.dm.SurfaceSynchronizer} synchronizer Surface synchronizer
20 * @param {jQuery} $overlay Overlay in which to attach popups (e.g. color picker)
21 * @param {Object} [config] Configuration options
22 */
23 ve.ui.AuthorItemWidget = function VeUiAuthorItemWidget( synchronizer, $overlay, config ) {
24 config = config || {};
25
26 // Parent constructor
27 ve.ui.AuthorItemWidget.super.call( this, config );
28
29 // Mixin constructors
30 OO.ui.mixin.LabelElement.call( this, config );
31
32 this.synchronizer = synchronizer;
33 this.editable = !!config.editable;
34 this.authorId = config.authorId;
35 this.name = null;
36 this.color = null;
37
38 this.$color = $( '<div>' ).addClass( 've-ui-authorItemWidget-color' );
39 this.$element.append( this.$color );
40
41 if ( this.editable ) {
42 this.input = new OO.ui.TextInputWidget( {
43 classes: [ 've-ui-authorItemWidget-nameInput' ],
44 placeholder: ve.msg( 'visualeditor-rebase-client-author-name' )
45 } );
46 // Re-emit change events
47 this.input.on( 'change', this.emit.bind( this, 'change' ) );
48
49 this.colorPicker = new CP( this.$color[ 0 ] );
50 this.colorPicker.on( 'change', ( color ) => {
51 this.color = color;
52 this.$color.css( 'background-color', '#' + color );
53 } );
54 this.colorPicker.on( 'exit', () => {
55 if ( this.color !== null ) {
56 this.emit( 'changeColor', this.color );
57 }
58 } );
59
60 this.colorPicker.picker.classList.add( 've-ui-authorItemWidget-colorPicker' );
61 this.colorPicker.fit = () => {
62 this.colorPicker.picker.style.left = this.$element[ 0 ].offsetLeft + 'px';
63 this.colorPicker.picker.style.top = this.$element[ 0 ].offsetTop + 'px';
64 $overlay[ 0 ].appendChild( this.colorPicker.picker );
65 };
66
67 this.$element
68 .addClass( 've-ui-authorItemWidget-editable' )
69 .append( this.input.$element );
70 } else {
71 this.$element.append( this.$label );
72 }
73
74 this.update();
75
76 this.$element.addClass( 've-ui-authorItemWidget' );
77 };
78
79 /* Inheritance */
80
81 OO.inheritClass( ve.ui.AuthorItemWidget, OO.ui.Widget );
82
83 OO.mixinClass( ve.ui.AuthorItemWidget, OO.ui.mixin.IconElement );
84
85 OO.mixinClass( ve.ui.AuthorItemWidget, OO.ui.mixin.LabelElement );
86
87 /* Methods */
88
89 /**
90 * Focus the widget, if possible
91 */
92 ve.ui.AuthorItemWidget.prototype.focus = function () {
93 if ( this.editable ) {
94 this.input.focus();
95 }
96 };
97
98 /**
99 * Get the user's name
100 *
101 * @return {string} User's name
102 */
103 ve.ui.AuthorItemWidget.prototype.getName = function () {
104 if ( this.editable ) {
105 return this.input.getValue();
106 } else {
107 return this.name;
108 }
109 };
110
111 /**
112 * Set author ID
113 *
114 * @param {number} authorId Author ID
115 */
116 ve.ui.AuthorItemWidget.prototype.setAuthorId = function ( authorId ) {
117 this.authorId = authorId;
118 };
119
120 /**
121 * Update name and color from synchronizer
122 */
123 ve.ui.AuthorItemWidget.prototype.update = function () {
124 const authorData = this.synchronizer.getAuthorData( this.authorId );
125 this.name = authorData.name;
126 this.color = authorData.color;
127 this.$color.css( 'background-color', '#' + this.color );
128
129 if ( this.editable ) {
130 this.input.setValue( this.name );
131 this.colorPicker.set( '#' + this.color );
132 } else {
133 // TODO: Handle empty names with a message
134 this.setLabel( this.name || '…' );
135 }
136 };