Merge "Revised styling of sister-search sidebar."
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.SavedQueryItemModel.js
1 ( function ( mw ) {
2 /**
3 * View model for a single saved query
4 *
5 * @mixins OO.EventEmitter
6 *
7 * @constructor
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} [default] This item is the default
13 */
14 mw.rcfilters.dm.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( mw.rcfilters.dm.SavedQueryItemModel );
29 OO.mixinClass( mw.rcfilters.dm.SavedQueryItemModel, OO.EventEmitter );
30
31 /* Events */
32
33 /**
34 * @update
35 *
36 * Model has been updated
37 */
38
39 /* Methods */
40
41 /**
42 * Get an object representing the state of this item
43 *
44 * @returns {Object} Object representing the current data state
45 * of the object
46 */
47 mw.rcfilters.dm.SavedQueryItemModel.prototype.getState = function () {
48 return {
49 data: this.getData(),
50 label: this.getLabel()
51 };
52 };
53
54 /**
55 * Get the query's identifier
56 *
57 * @return {string} Query identifier
58 */
59 mw.rcfilters.dm.SavedQueryItemModel.prototype.getID = function () {
60 return this.id;
61 };
62
63 /**
64 * Get query label
65 *
66 * @return {label} Query label
67 */
68 mw.rcfilters.dm.SavedQueryItemModel.prototype.getLabel = function () {
69 return this.label;
70 };
71
72 /**
73 * Update the query label
74 *
75 * @param {string} newLabel New label
76 */
77 mw.rcfilters.dm.SavedQueryItemModel.prototype.updateLabel = function ( newLabel ) {
78 if ( newLabel && this.label !== newLabel ) {
79 this.label = newLabel;
80 this.emit( 'update' );
81 }
82 };
83
84 /**
85 * Get query data
86 *
87 * @return {Object} Object representing parameter and highlight data
88 */
89 mw.rcfilters.dm.SavedQueryItemModel.prototype.getData = function () {
90 return this.data;
91 };
92
93 /**
94 * Check whether this item is the default
95 *
96 * @return {boolean} Query is set to be default
97 */
98 mw.rcfilters.dm.SavedQueryItemModel.prototype.isDefault = function () {
99 return this.default;
100 };
101
102 /**
103 * Toggle the default state of this query item
104 *
105 * @param {boolean} isDefault Query is default
106 */
107 mw.rcfilters.dm.SavedQueryItemModel.prototype.toggleDefault = function ( isDefault ) {
108 isDefault = isDefault === undefined ? !this.default : isDefault;
109
110 if ( this.default !== isDefault ) {
111 this.default = isDefault;
112 this.emit( 'update' );
113 }
114 };
115 }( mediaWiki ) );