Debian Codegraph @

resources/src/mediawiki.rcfilters/dm/SavedQueryItemModel.js

raw symbols SCIP
1 /**
2 * View model for a single saved query.
3 *
4 * @class mw.rcfilters.dm.SavedQueryItemModel
5 * @ignore
6 * @mixes OO.EventEmitter
7 *
8 * @param {string} id Unique identifier
9 * @param {string} label Saved query label
10 * @param {Object} data Saved query data
11 * @param {Object} [config] Configuration options
12 * @param {boolean} [config.default] This item is the default
13 */
14 const SavedQueryItemModel = function MwRcfiltersDmSavedQueriesModel( id, label, data, config ) {
15 config = config || {};
16
17 // Mixin constructor
18 OO.EventEmitter.call( this );
19
20 this.id = id;
21 this.label = label;
22 this.data = data;
23 this.default = !!config.default;
24 };
25
26 /* Initialization */
27
28 OO.initClass( SavedQueryItemModel );
29 OO.mixinClass( SavedQueryItemModel, OO.EventEmitter );
30
31 /* Events */
32
33 /**
34 * Model has been updated.
35 *
36 * @event update
37 * @ignore
38 */
39
40 /* Methods */
41
42 /**
43 * Get an object representing the state of this item
44 *
45 * @return {Object} Object representing the current data state
46 * of the object
47 */
48 SavedQueryItemModel.prototype.getState = function () {
49 return {
50 data: this.getData(),
51 label: this.getLabel()
52 };
53 };
54
55 /**
56 * Get the query's identifier
57 *
58 * @return {string} Query identifier
59 */
60 SavedQueryItemModel.prototype.getID = function () {
61 return this.id;
62 };
63
64 /**
65 * Get query label
66 *
67 * @return {string} Query label
68 */
69 SavedQueryItemModel.prototype.getLabel = function () {
70 return this.label;
71 };
72
73 /**
74 * Update the query label
75 *
76 * @param {string} newLabel New label
77 */
78 SavedQueryItemModel.prototype.updateLabel = function ( newLabel ) {
79 if ( newLabel && this.label !== newLabel ) {
80 this.label = newLabel;
81 this.emit( 'update' );
82 }
83 };
84
85 /**
86 * Get query data
87 *
88 * @return {Object} Object representing parameter and highlight data
89 */
90 SavedQueryItemModel.prototype.getData = function () {
91 return this.data;
92 };
93
94 /**
95 * Get the combined data of this item as a flat object of parameters
96 *
97 * @return {Object} Combined parameter data
98 */
99 SavedQueryItemModel.prototype.getCombinedData = function () {
100 return $.extend( true, {}, this.data.params, this.data.highlights );
101 };
102
103 /**
104 * Check whether this item is the default
105 *
106 * @return {boolean} Query is set to be default
107 */
108 SavedQueryItemModel.prototype.isDefault = function () {
109 return this.default;
110 };
111
112 /**
113 * Toggle the default state of this query item
114 *
115 * @param {boolean} isDefault Query is default
116 */
117 SavedQueryItemModel.prototype.toggleDefault = function ( isDefault ) {
118 isDefault = isDefault === undefined ? !this.default : isDefault;
119
120 if ( this.default !== isDefault ) {
121 this.default = isDefault;
122 this.emit( 'update' );
123 }
124 };
125
126 module.exports = SavedQueryItemModel;