Merge lp:~jcsackett/launchpad/picker-patcher-picked-a-patch-of-personpickers into lp:launchpad

Proposed by j.c.sackett
Status: Merged
Approved by: Curtis Hovey
Approved revision: no longer in the source branch.
Merged at revision: 13218
Proposed branch: lp:~jcsackett/launchpad/picker-patcher-picked-a-patch-of-personpickers
Merge into: lp:launchpad
Diff against target: 172 lines (+67/-31)
3 files modified
lib/lp/app/javascript/picker.js (+11/-15)
lib/lp/app/javascript/tests/test_personpicker.js (+18/-2)
lib/lp/app/javascript/widgets.js (+38/-14)
To merge this branch: bzr merge lp:~jcsackett/launchpad/picker-patcher-picked-a-patch-of-personpickers
Reviewer Review Type Date Requested Status
Curtis Hovey (community) code Approve
Ian Booth Pending
Review via email: mp+64094@code.launchpad.net

Commit message

[r=sinzui][bug=796635] Updates addPickerPatcher to use the personpicker.

Description of the change

Summary
=======
After creating the new js personpicker widget, we realized that there was an alternative means of getting a person picker via lp.app.picker.addPickerPatcher, which created a picker via picker.create. Because it wasn't activated by means of the PersonPicker in popup.py, picker_type did not get set to person.

Proposed fix
============
Set up addPickerPatcher to use a personpicker to create the "Assign me" and "Remove assignee" buttons as it needs them, since this is one of the major things added in the person picker, and we would like to avoid duplicate javascript.

Pre-implementation notes
========================
Spoke with Curtis Hovey and Ian Booth about the duplication and the idea of simply using personpicker to create the assignme and remove buttons in picker patcher.

Implementation details
======================
lib/lp/app/javascript/widgets.js
--------------------------------
Updated PersonPicker to generate the extra buttons based on the presence of show_assign_me and show_remove config options, so it can use the information passed along when the addPickerPatcher is used.

Also, added the remove and assign buttons as properties on the picker so that addPickerPatcher could easily override the functions attached to those buttons click events.

lib/lp/app/javascript/picker.js
-------------------------------
Removed the code generating the extra buttons in addPickerPatcher, since that's now done by the personpicker. Retained the custom assign_me and remove methods, which are now added to the personpicker's assign/remove buttons after purgeElement is called to remove the personpicker's usual events when those buttons are clicked.

Tests
=====
firefox lib/lp/app/javascript/tests/test_personpicker.html
firefox lib/lp/app/javascript/tests/test_picker.html

Demo and Q/A
============
Check the various pickers; they should all function the same as prior to this branch landing. This is just cleaning up code.

Launchpad lint
==============

Checking for conflicts and issues in changed files.

Linting changed files:
  lib/lp/app/javascript/widgets.js
  lib/lp/app/javascript/picker.js

./lib/lp/app/javascript/widgets.js
      14: 'Picker' has not been fully defined yet.
      62: 'PersonPicker' has not been fully defined yet.

I tried changing the lines causing this to use 'this' instead of 'Picker' or 'PersonPicker', but that broke the code. Perhaps this is an issue in the linter?

To post a comment you must log in.
Revision history for this message
Curtis Hovey (sinzui) wrote :

I think line 82 should use !==, not !=:
    if (cfg.show_remove_button !== undefined) {

This is good to land with this fix.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/app/javascript/picker.js'
2--- lib/lp/app/javascript/picker.js 2011-06-09 07:53:07 +0000
3+++ lib/lp/app/javascript/picker.js 2011-06-13 14:42:32 +0000
4@@ -43,6 +43,7 @@
5 var remove_button_text = 'Remove';
6 var null_display_value = 'None';
7 var show_search_box = true;
8+
9 resource_uri = Y.lp.client.normalize_uri(resource_uri);
10
11 if (config !== undefined) {
12@@ -65,6 +66,10 @@
13 if (config.show_search_box !== undefined) {
14 show_search_box = config.show_search_box;
15 }
16+ if (config.picker_type === undefined &&
17+ (show_assign_me_button || show_remove_button)) {
18+ config.picker_type = 'person';
19+ }
20 }
21
22 var content_box = Y.one('#' + content_box_id);
23@@ -121,6 +126,8 @@
24 });
25 };
26
27+ // Create a new assign_me and remove_assigne function that uses the
28+ // patcher save method instead of the standard picker save.
29 var assign_me = function () {
30 picker.hide();
31 save({
32@@ -156,27 +163,16 @@
33 config.save = save;
34 var picker = namespace.create(vocabulary, config, activator);
35 picker._resource_uri = resource_uri;
36- var extra_buttons = Y.Node.create('<div class="extra-form-buttons"/>');
37- var remove_button, assign_me_button;
38+ var remove_button = picker.remove_button;
39+ var assign_me_button = picker.assign_me_button;
40 if (show_remove_button) {
41- remove_button = Y.Node.create(
42- '<a class="yui-picker-remove-button bg-image" ' +
43- 'href="javascript:void(0)" ' +
44- 'style="background-image: url(/@@/remove); padding-right: 1em">' +
45- remove_button_text + '</a>');
46+ Y.Event.purgeElement(remove_button);
47 remove_button.on('click', remove);
48- extra_buttons.appendChild(remove_button);
49 }
50 if (show_assign_me_button) {
51- assign_me_button = Y.Node.create(
52- '<a class="yui-picker-assign-me-button bg-image" ' +
53- 'href="javascript:void(0)" ' +
54- 'style="background-image: url(/@@/person)">' +
55- 'Assign Me</a>');
56+ Y.Event.purgeElement(assign_me_button);
57 assign_me_button.on('click', assign_me);
58- extra_buttons.appendChild(assign_me_button);
59 }
60- picker.set('footer_slot', extra_buttons);
61
62 // If we are to pre-load the vocab, we need a spinner.
63 // We set it up here because we only want to do it once and the
64
65=== modified file 'lib/lp/app/javascript/tests/test_personpicker.js'
66--- lib/lp/app/javascript/tests/test_personpicker.js 2011-06-09 07:53:07 +0000
67+++ lib/lp/app/javascript/tests/test_personpicker.js 2011-06-13 14:42:32 +0000
68@@ -28,8 +28,8 @@
69
70 // The extra buttons section exists
71 Y.Assert.isNotNull(Y.one('.extra-form-buttons'));
72- Y.Assert.isNotNull(Y.one('.yui-picker-remove-button'));
73- Y.Assert.isNotNull(Y.one('.yui-picker-assign-me-button'));
74+ Y.Assert.isNotUndefined(personpicker.assign_me_button);
75+ Y.Assert.isNotUndefined(personpicker.remove_button);
76 },
77
78 test_buttons: function () {
79@@ -50,6 +50,22 @@
80 var assign_me = Y.one('.yui-picker-assign-me-button');
81 assign_me.simulate('click');
82 Y.Assert.areEqual('no-one', data);
83+ },
84+
85+ test_buttons_config: function () {
86+ cfg = {
87+ show_assign_me_button: false,
88+ show_remove_button: false
89+ };
90+
91+ personpicker = new Y.lp.app.widgets.PersonPicker(cfg);
92+ personpicker.render();
93+ personpicker.show();
94+
95+ Y.Assert.isNotNull(Y.one('.extra-form-buttons'));
96+ Y.Assert.isUndefined(personpicker.remove_button);
97+ Y.Assert.isUndefined(personpicker.assign_me_button);
98+
99 }
100 }));
101
102
103=== modified file 'lib/lp/app/javascript/widgets.js'
104--- lib/lp/app/javascript/widgets.js 2011-06-09 08:39:57 +0000
105+++ lib/lp/app/javascript/widgets.js 2011-06-13 14:42:32 +0000
106@@ -64,6 +64,24 @@
107 };
108
109 Y.extend(PersonPicker, namespace.Picker, {
110+ initializer: function(cfg) {
111+ PersonPicker.superclass.initializer.apply(this, arguments);
112+
113+ var show_assign_me_button = true;
114+ var show_remove_button = true;
115+
116+ if (cfg !== undefined) {
117+ if (cfg.show_assign_me_button !== undefined) {
118+ show_assign_me_button = cfg.show_assign_me_button;
119+ }
120+ if (cfg.show_remove_button !== undefined) {
121+ show_remove_button = cfg.show_remove_button;
122+ }
123+ }
124+ this._show_assign_me_button = show_assign_me_button;
125+ this._show_remove_button = show_remove_button;
126+ },
127+
128 hide: function() {
129 this.get('boundingBox').setStyle('display', 'none');
130 },
131@@ -87,21 +105,27 @@
132 var remove_button_text = "Remove assignee";
133 var assign_me_button_text = "Assign me";
134
135- remove_button = Y.Node.create(
136- '<a class="yui-picker-remove-button bg-image" ' +
137- 'href="javascript:void(0)" ' +
138- 'style="background-image: url(/@@/remove); padding-right: 1em">' +
139- remove_button_text + '</a>');
140- remove_button.on('click', this.remove, this);
141- this._extra_buttons.appendChild(remove_button);
142+ if (this._show_remove_button) {
143+ remove_button = Y.Node.create(
144+ '<a class="yui-picker-remove-button bg-image" ' +
145+ 'href="javascript:void(0)" ' +
146+ 'style="background-image: url(/@@/remove); padding-right: ' +
147+ '1em">' + remove_button_text + '</a>');
148+ remove_button.on('click', this.remove, this);
149+ this._extra_buttons.appendChild(remove_button);
150+ this.remove_button = remove_button;
151+ }
152
153- assign_me_button = Y.Node.create(
154- '<a class="yui-picker-assign-me-button bg-image" ' +
155- 'href="javascript:void(0)" ' +
156- 'style="background-image: url(/@@/person)">' +
157- assign_me_button_text + '</a>');
158- assign_me_button.on('click', this.assign_me, this);
159- this._extra_buttons.appendChild(assign_me_button);
160+ if (this._show_assign_me_button) {
161+ assign_me_button = Y.Node.create(
162+ '<a class="yui-picker-assign-me-button bg-image" ' +
163+ 'href="javascript:void(0)" ' +
164+ 'style="background-image: url(/@@/person)">' +
165+ assign_me_button_text + '</a>');
166+ assign_me_button.on('click', this.assign_me, this);
167+ this._extra_buttons.appendChild(assign_me_button);
168+ this.assign_me_button = assign_me_button;
169+ }
170 },
171
172 syncUI: function() {