Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/tools/ve.ui.AuthorListPopupTool.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface AuthorListPopupTool class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * UserInterface AuthorListPopupTool
10 *
11 * @class
12 * @extends OO.ui.PopupTool
13 *
14 * @constructor
15 * @param {OO.ui.ToolGroup} toolGroup
16 * @param {Object} [config]
17 */
18 ve.ui.AuthorListPopupTool = function VeUiAuthorListPopupTool( toolGroup, config ) {
19 this.$authorList = $( '<div>' );
20
21 // Parent constructor
22 ve.ui.AuthorListPopupTool.super.call( this, toolGroup, ve.extendObject( {
23 popup: {
24 classes: [ 've-ui-authorListWidget-listPopup' ],
25 $content: this.$authorList,
26 padded: true,
27 align: 'backwards'
28 }
29 }, config ) );
30
31 // Events
32 this.toolbar.connect( this, { surfaceChange: 'onSurfaceChange' } );
33
34 this.$element.addClass( 've-ui-authorListPopupTool' );
35 };
36
37 /* Inheritance */
38
39 OO.inheritClass( ve.ui.AuthorListPopupTool, OO.ui.PopupTool );
40
41 /* Methods */
42
43 /**
44 * Handle surfaceChange event fromt the toolbar
45 *
46 * @param {ve.dm.Surface|null} oldSurface Old surface
47 * @param {ve.dm.Surface|null} newSurface New surface
48 */
49 ve.ui.AuthorListPopupTool.prototype.onSurfaceChange = function ( oldSurface, newSurface ) {
50 // TODO: Disconnect oldSurface. Currently in the CollabTarget life-cycle the surface is never changed.
51 this.setup( newSurface );
52 };
53
54 /**
55 * @inheritdoc
56 */
57 ve.ui.AuthorListPopupTool.prototype.onPopupToggle = function ( visible ) {
58 // Parent method
59 ve.ui.AuthorListPopupTool.super.prototype.onPopupToggle.apply( this, arguments );
60
61 if ( visible ) {
62 this.selfItem.focus();
63 }
64 };
65
66 /**
67 * Setup the popup which a specific surface
68 *
69 * @param {ve.ui.Surface} surface
70 */
71 ve.ui.AuthorListPopupTool.prototype.setup = function ( surface ) {
72 this.oldName = '';
73 this.updatingName = false;
74 this.synchronizer = surface.getModel().synchronizer;
75 this.authorItems = {};
76
77 this.surface = surface;
78
79 if ( !this.synchronizer ) {
80 this.setDisabled( true );
81 return;
82 }
83
84 // TODO: Unbind from an existing surface if one is set
85
86 this.changeNameDebounced = ve.debounce( this.changeName.bind( this ), 250 );
87
88 this.selfItem = new ve.ui.AuthorItemWidget(
89 this.synchronizer,
90 this.popup.$element,
91 { editable: true, authorId: this.synchronizer.getAuthorId() }
92 );
93 this.$authorList.prepend( this.selfItem.$element );
94 this.selfItem.connect( this, {
95 change: 'onSelfItemChange',
96 changeColor: 'onSelfItemChangeColor'
97 } );
98
99 this.synchronizer.connect( this, {
100 authorChange: 'onSynchronizerAuthorUpdate',
101 authorDisconnect: 'onSynchronizerAuthorDisconnect'
102 } );
103
104 for ( const authorId in this.synchronizer.authors ) {
105 this.onSynchronizerAuthorUpdate( +authorId );
106 }
107 };
108
109 /**
110 * Handle change events from the user's authorItem
111 *
112 * @param {string} value
113 */
114 ve.ui.AuthorListPopupTool.prototype.onSelfItemChange = function () {
115 if ( !this.updatingName ) {
116 this.changeNameDebounced();
117 }
118 };
119
120 /**
121 * Handle change color events from the user's authorItem
122 *
123 * @param {string} color
124 */
125 ve.ui.AuthorListPopupTool.prototype.onSelfItemChangeColor = function ( color ) {
126 this.synchronizer.changeAuthor( { color: color } );
127 };
128
129 /**
130 * Notify the server of a name change
131 */
132 ve.ui.AuthorListPopupTool.prototype.changeName = function () {
133 this.synchronizer.changeAuthor( { name: this.selfItem.getName() } );
134 };
135
136 /**
137 * Update the user count
138 */
139 ve.ui.AuthorListPopupTool.prototype.updateAuthorCount = function () {
140 this.setTitle( ( Object.keys( this.authorItems ).length + 1 ).toString() );
141 };
142
143 /**
144 * Called when the synchronizer receives a remote author selection or name change
145 *
146 * @param {number} authorId The author ID
147 */
148 ve.ui.AuthorListPopupTool.prototype.onSynchronizerAuthorUpdate = function ( authorId ) {
149 let authorItem = this.authorItems[ authorId ];
150
151 if ( authorId !== this.synchronizer.getAuthorId() ) {
152 if ( !authorItem ) {
153 authorItem = new ve.ui.AuthorItemWidget( this.synchronizer, this.popup.$element, { authorId: authorId } );
154 this.authorItems[ authorId ] = authorItem;
155 this.updateAuthorCount();
156 this.$authorList.append( authorItem.$element );
157 } else {
158 authorItem.update();
159 }
160 } else {
161 // Don't update nameInput if the author is still changing it
162 if ( this.selfItem.getName() === this.oldName ) {
163 // Don't send this "new" name back to the server
164 this.updatingName = true;
165 try {
166 this.selfItem.setAuthorId( this.synchronizer.getAuthorId() );
167 this.selfItem.update();
168 } finally {
169 this.updatingName = false;
170 }
171 }
172 }
173 this.oldName = this.synchronizer.getAuthorData( authorId ).name;
174 };
175
176 /**
177 * Called when the synchronizer receives a remote author disconnect
178 *
179 * @param {number} authorId The author ID
180 */
181 ve.ui.AuthorListPopupTool.prototype.onSynchronizerAuthorDisconnect = function ( authorId ) {
182 const authorItem = this.authorItems[ authorId ];
183 if ( authorItem ) {
184 authorItem.$element.remove();
185 delete this.authorItems[ authorId ];
186 this.updateAuthorCount();
187 }
188 };
189
190 /* Static Properties */
191
192 ve.ui.AuthorListPopupTool.static.name = 'authorList';
193 ve.ui.AuthorListPopupTool.static.group = 'users';
194 ve.ui.AuthorListPopupTool.static.icon = 'userAvatar';
195 ve.ui.AuthorListPopupTool.static.title = '1';
196 ve.ui.AuthorListPopupTool.static.autoAddToCatchall = false;
197 ve.ui.AuthorListPopupTool.static.autoAddToGroup = false;
198 ve.ui.AuthorListPopupTool.static.displayBothIconAndLabel = true;
199
200 /* Registration */
201
202 ve.ui.toolFactory.register( ve.ui.AuthorListPopupTool );