1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/* Copyright (c) 2011, Canonical Ltd. All rights reserved. */
YUI().add('lp.buglisting_utils', function(Y) {
/**
* A utility for configuring the display of bug listings.
*
* The purpose of this widget is be a mechanism for turning
* fields on and off in a bug listing display. It extends
* from BaseConfigUtil, which provides the clickable settings
* icon. When the icon is clicked, a form overlay opens with
* various checkboxes for turning fields on and off.
*
* This doesn't actually change the display, though. It fires
* an event that the buglisting navigator will hook into to update
* the list's display.
*
* @module lp.buglisting_utils
*/
// Constants.
var FORM = 'form';
/**
* BugListingConfigUtil is the main object used to manipulate
* a bug listing's display.
*
* Constructor accepts a config containing
* - model (a BugListingModel)
* - cookie_name
* - form the FormOverlay to manipulate
*
* If model is not supplied, model parameters must be included, especially
* - form_visibility
* - form_visibility_defaults
*
* @class BugListingConfigUtil
* @extends Y.lp.configutils.BaseConfigUtil
* @constructor
*/
function BugListingConfigUtil() {
BugListingConfigUtil.superclass.constructor.apply(this, arguments);
}
BugListingConfigUtil.NAME = 'buglisting-config-util';
/**
* Object to reference display names for field_visibility
* form inputs.
*/
BugListingConfigUtil.field_display_names = {
show_id: 'Number',
show_importance: 'Importance',
show_status: 'Status',
show_heat: 'Heat',
show_targetname: 'Package/Project/Series name',
show_datecreated: 'Age',
show_date_last_updated: 'Date last updated',
show_assignee: 'Assignee',
show_reporter: 'Reporter',
show_milestone_name: 'Milestone',
show_tag: 'Tags'
};
BugListingConfigUtil.ATTRS = {
/**
* The cookie name as set by the view.
*
* We get this value from the LP cache.
*
* @attribute cookie_name
* @type String
*/
cookie_name: {
valueFn: function() {
if (
Y.Lang.isValue(window.LP) &&
Y.Lang.isValue(LP.cache.cbl_cookie_name)) {
return LP.cache.cbl_cookie_name;
} else {
return '';
}
}
},
/**
* A reference to the form overlay used in the overlay.
*
* @attribute form
* @type Y.lazr.FormOverlay
* @default null
*/
form: {
value: null
},
model: {
value: null
}
};
BugListingConfigUtil.INPUT_TEMPLATE = [
'<li><input type="checkbox" class="{name}" name="{name}" ',
'value="{display_name}" id="{name}_id" {checked}> ',
'<label for="{name}_id">{display_name}</label></li>'].join('');
Y.extend(BugListingConfigUtil, Y.lp.configutils.BaseConfigUtil, {
initializer: function(config){
if (config === undefined){
config = {};
}
if (Y.Lang.isNull(this.get('model'))){
this.set('model',
new Y.lp.bugs.buglisting.BugListingModel(config));
}
},
/**
* Hook into the destroy lifecyle to ensure the form
* overlay is destroyed.
*
* @method destructor
*/
destructor: function() {
if (Y.Lang.isValue(this.get(FORM))) {
var form = this.get(FORM);
this.set(FORM, null);
form.destroy();
}
},
/**
* Build the input nodes used on the form overlay.
*
* @method getFormInputs
*/
getFormInputs: function() {
var fields = this.get('model').get_field_visibility();
var display_names = this.constructor.field_display_names;
var nodes = [];
var item,
name,
display_name,
checked,
input_html,
input_node;
for (item in fields) {
if (fields.hasOwnProperty(item)) {
name = item;
display_name = display_names[item];
if (fields[item] === true) {
checked = 'checked';
} else {
checked = '';
}
input_html = Y.Lang.substitute(
this.constructor.INPUT_TEMPLATE,
{name: name, display_name: display_name,
checked: checked});
input_node = Y.Node.create(input_html);
nodes.push(input_node);
}
}
return new Y.NodeList(nodes);
},
/**
* Build the reset link for the form.
*
* Also, provide a click handler to reset the fields config.
*
* @method getResetLink
*/
getResetLink: function() {
var link = Y.Node.create('<a></a>');
link.addClass('js-action');
link.addClass('reset-buglisting');
link.setContent('Reset to default');
link.on('click', function(e) {
var model = this.get('model');
var defaults = model.get('field_visibility_defaults');
this.updateFieldVisibilty(defaults, true);
this.setCookie();
}, this);
return link;
},
/**
* Build the form content for form overlay.
*
* @method buildFormContent
*/
buildFormContent: function() {
var div = Y.Node.create(
'<ul></ul>').addClass('buglisting-opts');
var inputs = this.getFormInputs();
div.append(inputs);
var link = this.getResetLink();
div.append(link);
return div;
},
/**
* Helper method for updating field_visibility.
*
* @method updateFieldVisibilty
*/
updateFieldVisibilty: function(fields, destroy_form) {
this.get('model').set_field_visibility(fields);
var form = this.get(FORM);
if (Y.Lang.isValue(form)) {
form.hide();
}
// Destroy the form and rebuild it.
if (destroy_form === true) {
this.get(FORM).hide();
this._extraRenderUI();
}
},
/**
* Process the data from the form overlay submit.
*
* data is an object whose members are the checked
* input elements from the form. data has the same members
* as field_visibility, so if the key is in data it should
* be set to true in field_visibility.
*
* @method handleOverlaySubmit
*/
handleOverlaySubmit: function(data) {
var fields = this.get('model').get_field_visibility();
var member;
for (member in fields) {
if (fields.hasOwnProperty(member)) {
if (Y.Lang.isValue(data[member])) {
// If this field exists in data, set it true.
// in field_visibility.
fields[member] = true;
} else {
// Otherwise, set the member to false in
// field_visibility.
fields[member] = false;
}
}
}
this.updateFieldVisibilty(fields);
this.setCookie(fields);
},
/**
* Set the given value for the buglisting config cookie.
* If config is not specified, the cookie will be cleared.
*
* @method setCookie
*/
setCookie: function(config) {
var cookie_name = this.get('cookie_name');
if (Y.Lang.isValue(config)) {
Y.Cookie.setSubs(cookie_name, config, {
path: '/',
expires: new Date('January 19, 2038')});
} else {
Y.Cookie.remove(cookie_name, {path: '/'});
}
},
/**
* Hook in _extraRenderUI provided by BaseConfigUtil
* to add a form overlay to the widget.
*
* @method _extraRenderUI
*/
_extraRenderUI: function() {
var form_content = this.buildFormContent();
var on_submit_callback = Y.bind(this.handleOverlaySubmit, this);
util_overlay = new Y.lazr.FormOverlay({
align: 'left',
headerContent: '<h2>Visible information</h2>',
centered: true,
form_content: form_content,
form_submit_button: Y.Node.create(
'<input type="submit" value="Update" ' +
'class="update-buglisting" />'
),
form_cancel_button: Y.Node.create(
'<button type="button" name="field.actions.cancel" ' +
'class="hidden" >Cancel</button>'
),
form_submit_callback: on_submit_callback
});
util_overlay.get(
'boundingBox').addClass(this.getClassName('overlay'));
this.set(FORM, util_overlay);
util_overlay.render();
util_overlay.hide();
},
/**
* Hook into _handleClick provided by BaseConfigUtil
* to show overlay when the settings cog icon is clicked.
*
* @method _handleClick
*/
_handleClick: function() {
var form = this.get(FORM);
form.show();
}
});
var buglisting_utils = Y.namespace('lp.buglisting_utils');
buglisting_utils.BugListingConfigUtil = BugListingConfigUtil;
/**
* Update the visibilty of the sort buttons.
*
* We want to display only the sort buttons for fields which
* are displayed. To avoid surprises for users, the current sort
* order is always displayed, even when the related data is not
* shown.
*
* @param orderbybar {Object} The order by bar.
* @param data_visibility {Associative Array} The visibility data
* as used in Y.bugs.buglisting.BugListingModel.
*/
function update_sort_button_visibility(orderbybar, data_visibility) {
// We must translate the field names as used by
// BugListingConfigUtil to those used by the "order by" buttons.
var orderby_visibility = {};
var order_key;
var data_key;
for (data_key in data_visibility) {
if (data_visibility.hasOwnProperty(data_key) &&
data_key.substring(0, 5) === 'show_') {
order_key = data_key.replace('show_', '');
orderby_visibility[order_key] = data_visibility[data_key];
}
}
// Never hide the button for the current sort order...
orderby_visibility[orderbybar.get('active')] = true;
// ...and buttons for sort orders that should always be displayed.
Y.each(orderbybar.always_display, function(sort_key) {
orderby_visibility[sort_key] = true;
});
orderbybar.updateVisibility(orderby_visibility);
}
buglisting_utils.update_sort_button_visibility =
update_sort_button_visibility;
}, '0.1', {'requires': [
'cookie', 'history', 'lp.configutils', 'lazr.formoverlay',
'lp.bugs.buglisting'
]});
|