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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
|
/**
* Copyright 2011 Canonical Ltd. This software is licensed under the
* GNU Affero General Public License version 3 (see the file LICENSE).
*
* DistroSeries Widgets.
*
* @module lp.registry.distroseries
* @submodule widgets
*/
YUI.add('lp.registry.distroseries.widgets', function(Y) {
Y.log('loading lp.registry.distroseries.widgets');
var namespace = Y.namespace('lp.registry.distroseries.widgets');
var formwidgets = Y.lp.app.formwidgets;
/**
* A table to display, order, delete the selected parent series. Each parent
* can also be made an overlay, and a component and a pocket selected.
*
*/
var ParentSeriesListWidget;
ParentSeriesListWidget = function() {
ParentSeriesListWidget
.superclass.constructor.apply(this, arguments);
};
Y.mix(ParentSeriesListWidget, {
NAME: 'parentSeriesListWidget',
ATTRS: {
/**
* The DistroSeries the choices in this field should
* reflect. Takes the form of a list of ids,
* e.g. ["4", "55"].
*
* @property parents
*/
parents: {
getter: function() {
var series = [];
this.fieldNode.all("tbody > tr.parent").each(
function(node) {
series.push(
node.get('id').replace('parent-',''));
}
);
return series;
}
},
overlays: {
getter: function() {
var overlays = [];
this.fieldNode.all("tbody > tr.parent").each(
function(node) {
overlays.push(
node.one('input.overlay').get('checked'));
}
);
return overlays;
}
},
overlay_pockets: {
getter: function() {
var overlay_pockets = [];
this.fieldNode.all("tbody > tr.parent").each(
function(node) {
var select = node.one('td.pocket').one('select');
if (select !== null) {
overlay_pockets.push(select.get('value'));
}
else {
overlay_pockets.push(null);
}
}
);
return overlay_pockets;
}
},
overlay_components: {
getter: function() {
var overlay_components = [];
this.fieldNode.all("tbody > tr.parent").each(
function(node) {
var select = node.one('td.component').one('select');
if (select !== null) {
overlay_components.push(select.get('value'));
}
else {
overlay_components.push(null);
}
}
);
return overlay_components;
}
}
}
});
Y.extend(ParentSeriesListWidget, formwidgets.FormRowWidget, {
initializer: function() {
ParentSeriesListWidget.superclass.initializer();
this.client = new Y.lp.client.Launchpad();
this.clean_display();
},
/**
* Display a simple message when no parent series are selected.
*
* @method clean_display
*/
clean_display: function() {
this.fieldNode.empty();
this.fieldNode.append(Y.Node.create("<div />")
.set('text', '[No parent for this series yet!]'));
},
/**
* Display the table header.
*
* @method init_display
*/
init_display: function() {
this.fieldNode.empty();
this.fieldNode = Y.Node.create("<table />");
var table_header = Y.Node.create(
["<thead><tr>",
"<th>Order</th>",
"<th>Parent name</th>",
"<th>Overlay?</th>",
"<th>Component</th>",
"<th>Pocket</th>",
"<th>Delete</th>",
"</tr></thead>",
"<tbody>",
"</tbody>"
].join(""));
this.fieldNode.append(table_header);
},
/**
* Helper method to create a select widget from a list and add it
* to a node.
*
* @method build_selector
*/
build_selector: function(node, res_list, class_name) {
var select = Y.Node.create('<select disabled="disabled"/>');
res_list.forEach(
function(choice) {
select.appendChild('<option />')
.set('text', choice)
.set('value', choice);
});
node.one('td.'+class_name).append(select);
node.one('input').on('click', function(e) {
var select = node.one('td.'+class_name).one('select');
if (select.hasAttribute('disabled')) {
select.removeAttribute('disabled');
}
else {
select.setAttribute('disabled', 'disabled');
}
});
},
/**
* Build a select widget from a list retrieved from the api.
*
* @method build_select
*/
build_select: function(node, class_name, path) {
var self = this;
var on = {
success: function(res_list) {
self.build_selector(node, res_list, class_name);
},
failure: function() {
var failed_node = Y.Node.create('<span />')
.set('text', 'Failed to retrieve content.');
node.one('td.'+class_name).append(failed_node);
self.disable_overlay(node);
}
};
this.client.get(path, {on: on});
},
/**
* Move down a parent's line in the table.
*
* @method move_down
*/
move_down: function(parent_id) {
var node = this.fieldNode.one("tr#parent-" + parent_id);
var other = node.next('tr.parent');
if (other !== null) { node.swap(other);}
Y.lazr.anim.green_flash({node: node}).run();
},
/**
* Move up a parent's line in the table.
*
* @method move_up
*/
move_up: function(parent_id) {
var node = this.fieldNode.one("tr#parent-" + parent_id);
var other = node.previous('tr.parent');
if (other !== null) { node.swap(other);}
Y.lazr.anim.green_flash({node: node}).run();
},
/**
* Remove a parent series.
*
* @method remove_parent
*/
remove_parent: function(parent_id) {
if (this.get('parents').length === 1) {
this.clean_display();
this.renderUI();
}
else {
this.fieldNode.one('tr#parent-' + parent_id).remove();
}
Y.fire("parent_removed", parent_id);
},
/**
* Disable the overlay (used when something goes wrong fetching possible
* components or pockets for the overlay).
*
* @method disable_overlay
*/
disable_overlay: function(parent_node) {
var overlay = parent_node.one('input.overlay');
if (overlay.get('disabled') !== 'disabled') {
Y.lazr.anim.red_flash({node: parent_node}).run();
parent_node.one('input.overlay').set('disabled','disabled');
}
},
/**
* Add a parent series.
*
* @method add_parent
*/
add_parent: function(parent) {
if (this.get('parents').length === 0) {
this.init_display();
this.renderUI();
}
var item = this.fieldNode.one('tr#parent-' + parent.value);
if (item !== null) {
Y.lazr.anim.red_flash({node: item}).run();
return false;
}
item = Y.Node.create("<tr />")
.addClass('parent')
.set('id', 'parent-' + parent.value)
.append(Y.Node.create("<td />")
.append(Y.Node.create('<a href="" title="Move parent up"/>')
.addClass('move-up')
.set('innerHTML', '↑'))
.append(Y.Node.create('<a href="" title="Move parent down"/>')
.addClass('move-down')
.set('innerHTML', '↓')))
.append(Y.Node.create("<td />")
.addClass('series')
.set('text', parent.title))
.append(Y.Node.create("<td />")
.set('align', 'center')
.append(Y.Node.create('<input type="checkbox" />')
.addClass('overlay')))
.append(Y.Node.create("<td />")
.addClass('component'))
.append(Y.Node.create("<td />")
.addClass('pocket'))
.append(Y.Node.create("<td />")
.set('align', 'center')
.append(Y.Node.create('<span />')
.addClass('sprite')
.addClass('remove')));
this.fieldNode.one('tbody').append(item);
this.build_select(item, 'component',
parent.api_uri + '/component_names');
this.build_select(item, 'pocket',
parent.api_uri + '/suite_names');
item.one('.move-up').on('click', function(e) {
this.move_up(parent.value);
e.preventDefault();
return false;
}, this);
item.one('.move-down').on('click', function(e) {
this.move_down(parent.value);
e.preventDefault();
return false;
}, this);
item.one('.remove').on('click', function(e) {
var parent_id = item.get('id').replace('parent-','');
this.remove_parent(parent_id);
e.preventDefault();
return false;
}, this);
Y.lazr.anim.green_flash({node: item}).run();
return true;
}
});
namespace.ParentSeriesListWidget = ParentSeriesListWidget;
/**
* A special form of ChoiceListWidget for choosing architecture tags.
*
* @class ArchitecturesChoiceListWidget
*/
var ArchitecturesChoiceListWidget;
ArchitecturesChoiceListWidget = function() {
ArchitecturesChoiceListWidget
.superclass.constructor.apply(this, arguments);
};
Y.mix(ArchitecturesChoiceListWidget, {
NAME: 'architecturesChoiceListWidget',
ATTRS: {
}
});
Y.extend(ArchitecturesChoiceListWidget, formwidgets.ChoiceListWidget, {
initializer: function(config) {
this.client = new Y.lp.client.Launchpad();
this.error_handler = new Y.lp.client.ErrorHandler();
this.error_handler.clearProgressUI = Y.bind(this.hideSpinner, this);
this.error_handler.showError = Y.bind(this.showError, this);
this._distroseries = {};
this.clean_display();
},
/**
* Display a simple message when no parent series are selected.
*
* @method clean_display
*/
clean_display: function() {
this.fieldNode.empty();
this.fieldNode.append(Y.Node.create("<div />")
.set('text', '[No architectures to select from yet!]'));
this.renderUI();
},
/**
* Add a parent distroseries, add the architectures for this new
* distroseries to the possible choices.
*
* @method add_distroseries
* @param {Object} distroseries The distroseries to add
* ({value:distroseries_id, api_uri:distroseries_uri}).
*/
add_distroseries: function(distroseries) {
var path = distroseries.api_uri + "/architectures";
var distroseries_id = distroseries.value;
var self = this;
var on = {
success: function (results) {
self.add_distroarchseries(distroseries_id, results);
},
failure: this.error_handler.getFailureHandler()
};
this.client.get(path, {on: on});
},
/**
* Remove a parent distroseries, remove the architectures only
* present in this parent series from the possible choices.
*
* @method remove_distroseries
*/
remove_distroseries: function(distroseries_id) {
// Compute which das is only in the distroseries to be removed.
var arch_to_remove = [];
var das = this._distroseries[distroseries_id];
var i, ds, j;
for (i=0; i<das.entries.length; i++) {
var remove_das = true;
var arch = das.entries[i].get('architecture_tag');
for (ds in this._distroseries) {
if (this._distroseries.hasOwnProperty(ds) &&
ds !== distroseries_id) {
var other_das = this._distroseries[ds];
for (j=0; j<other_das.entries.length; j++) {
var other_arch = other_das.entries[j].get(
'architecture_tag');
if (other_arch === arch) {
remove_das = false;
}
}
}
}
if (remove_das) {
arch_to_remove.push(arch);
}
}
delete this._distroseries[distroseries_id];
this.remove_choices(arch_to_remove);
if (this.fieldNode.all('input').isEmpty()) {
this.clean_display();
}
},
/**
* Add a list of distroarchseries.
*
* @method add_distroarchseries
* @param {String} distroseries_id The distroarchseries id.
* @param {Object} distroarchseries The distroarchseries object.
*/
add_distroarchseries: function(distroseries_id, distroarchseries) {
this._distroseries[distroseries_id] = distroarchseries;
var choices = distroarchseries.entries.map(
function(das) {
return das.get("architecture_tag");
}
);
this.add_choices(choices);
}
});
namespace.ArchitecturesChoiceListWidget = ArchitecturesChoiceListWidget;
/**
* A special form of SelectWidget for choosing packagesets.
*
* @class PackagesetPickerWidget
*/
var PackagesetPickerWidget;
PackagesetPickerWidget = function() {
PackagesetPickerWidget
.superclass.constructor.apply(this, arguments);
};
Y.mix(PackagesetPickerWidget, {
NAME: 'packagesetPickerWidget',
ATTRS: {
/**
* The DistroSeries the choices in this field should
* reflect. Takes the form of a string, e.g. "ubuntu/hoary".
*
* @property distroSeries
* @deprecated Use add_distroseries() and
* remove_distroseries() instead.
*/
distroSeries: {
/* XXX: GavinPanella 2011-07-20 bug=814509: This widget
handles multiple distroseries so this attribute is
defunct. */
setter: function(value, name) {
var distro_series_uri = Y.lp.client.get_absolute_uri(value);
var on = {
start: Y.bind(this.showSpinner, this),
success: Y.bind(this.set, this, "packageSets"),
failure: this.error_handler.getFailureHandler(),
end: Y.bind(this.hideSpinner, this)
};
var config = {
on: on,
parameters: {
distroseries: distro_series_uri
}
};
this.client.named_get("package-sets", "getBySeries", config);
}
}
}
});
Y.extend(PackagesetPickerWidget, formwidgets.SelectWidget, {
/**
* Add a distroseries: add its packagesets to the packageset picker.
*
* @method add_distroseries
* @param {Object} distroseries An object describing a
* distroseries, containing three properties: api_uri, title
* and value. The first two are what you might expect. The
* last is simply a unique term for referencing the
* distroseries which is required when calling
* remove_distroseries. TODO: Accept an lp.client.Entry as an
* alternative?
*/
add_distroseries: function(distroseries) {
var distro_series_uri = Y.lp.client.get_absolute_uri(
distroseries.api_uri);
var self = this;
var on = {
success: function (results) {
self.add_packagesets(results, distroseries);
},
failure: this.error_handler.getFailureHandler()
};
var config = {
on: on,
parameters: {
distroseries: distro_series_uri
}
};
this.client.named_get("package-sets", "getBySeries", config);
},
/**
* Display a simple message when no parent series are selected.
*
* @method clean_display
*/
clean_display: function() {
/* XXX: GavinPanella 2011-07-20 bug=814531: The list of
choices should be set via this.set("choices", ...) and the
setter should ensure that the selection is maintained.
Doing it via the back door means that the choicesChange
event is not fired. Rather than fire it explicity we should
DTRT. */
/* XXX: GavinPanella 2011-07-18 bug=814535: This does not
update this._distroseries and can thus leave the widget in
an invalid state. */
this.fieldNode.empty();
this.fieldNode.append(Y.Node.create("<div />")
.set('text', '[No package sets to select from yet!]'));
this.renderUI();
},
/**
* Initialize the picker's select node.
*
* @method init_select
*/
init_select: function() {
/* XXX: GavinPanella 2011-07-20 bug=814531: This method will
not be necessary once this.set("choices", ...) is used
consistently. */
var select = this.fieldNode.one('select');
if (select === null) {
select = Y.Node.create("<select />");
select.set("name", this.get("name"))
.set("size", this.get("size"));
if (this.get("multiple")) {
select.set("multiple", "multiple");
}
this.fieldNode.empty().append(select);
}
return select;
},
/**
* Add a choice to the picker.
*
* @method add_choice
* @param {Object} choice The choice object to be added
* ({text: choice_text, value: choice_value, data: choice_data}).
*/
add_choice: function(choice) {
/* XXX: GavinPanella 2011-07-20 bug=814531: The list of
choices should be set via this.set("choices", ...) and the
setter should ensure that the selection is maintained.
Doing it via the back door means that the choicesChange
event is not fired. Rather than fire it explicity we should
DTRT. */
var select = this.init_select();
var option = Y.Node.create("<option />");
option.set("value", choice.value)
.set("text", choice.text)
.setData("data", choice.data);
var options = select.all('option');
if (options.isEmpty()) {
select.append(option);
}
else {
var pos = this._sorted_position(choice.text);
if (pos === 0) {
select.prepend(option);
}
else {
select.insertBefore(option, options.item(pos));
}
}
},
/**
* Add choices (a set of packagesets) to the picker.
*
* @method add_packagesets
* @param {Y.lp.client.Collection} packagesets The collection of
* packagesets to add.
* @param {Object} distroseries The distroseries object
* ({value:distroseries_id), api_uri:distroseries_uri}).
*/
add_packagesets: function(packagesets, distroseries) {
this._packagesets[distroseries.value] = packagesets.entries;
packagesets.entries.forEach(
function(packageset) {
var value = packageset.get("id");
this.add_choice({
data: packageset,
value: value,
text: (
packageset.get("name") + ": " +
packageset.get("description") +
" (" + distroseries.title + ") ")
});
}, this);
this.autoSize(10);
Y.lazr.anim.green_flash({node: this.fieldNode}).run();
},
/**
* Remove a distroseries: remove its packagesets from the picker.
*
* @method remove_distroseries
* @param {String} distroseries_id The id of the distroseries to be
* removed.
*/
remove_distroseries: function(distroseries_id) {
this._packagesets[distroseries_id].forEach(
function(packageset) {
/* XXX: GavinPanella 2011-07-20 bug=814531: The list
of choices should be set via this.set("choices",
...) and the setter should ensure that the
selection is maintained. Doing it via the back door
means that the choicesChange event is not
fired. Rather than fire it explicity we should
DTRT. */
this.fieldNode.one(
'option[value="' + packageset.get("id") + '"]').remove();
}, this);
Y.lazr.anim.green_flash({node: this.fieldNode}).run();
if (this.fieldNode.all('option').isEmpty()) {
this.clean_display();
}
},
initializer: function(config) {
this.client = new Y.lp.client.Launchpad();
this.error_handler = new Y.lp.client.ErrorHandler();
this.error_handler.clearProgressUI = Y.bind(this.hideSpinner, this);
this.error_handler.showError = Y.bind(this.showError, this);
this.clean_display();
// _packagesets maps each distroseries' id to a collection of ids
// of its packagesets.
// It's populated each time a new distroseries is added as a parent
// and used when a distroseries is removed to get all the
// corresponding packagesets to be removed from the widget.
this._packagesets = {};
}
});
namespace.PackagesetPickerWidget = PackagesetPickerWidget;
}, "0.1", {"requires": [
"node", "dom", "io", "widget", "lp.client",
"lp.app.formwidgets", "lazr.anim", "array-extras",
"transition"]});
|