Merge lp:~salgado/lazr-js/picker-refactoring2 into lp:lazr-js

Proposed by Guilherme Salgado
Status: Superseded
Proposed branch: lp:~salgado/lazr-js/picker-refactoring2
Merge into: lp:lazr-js
Prerequisite: lp:~salgado/lazr-js/picker-refactoring
Diff against target: 236 lines (+141/-7)
3 files modified
examples/picker/index.html (+2/-3)
src-js/lazrjs/picker/picker.js (+88/-3)
src-js/lazrjs/picker/tests/picker.js (+51/-1)
To merge this branch: bzr merge lp:~salgado/lazr-js/picker-refactoring2
Reviewer Review Type Date Requested Status
Canonical Launchpad Engineering Pending
Review via email: mp+14797@code.launchpad.net

This proposal has been superseded by a proposal from 2009-11-12.

To post a comment you must log in.
Revision history for this message
Guilherme Salgado (salgado) wrote :

Add a new config variable to the picker to specify the DOM element to which picker.show() should be hooked up.

That way callsites don't need to do it themselves.

134. By Guilherme Salgado

merge from trunk

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'examples/picker/index.html'
2--- examples/picker/index.html 2009-10-22 20:22:12 +0000
3+++ examples/picker/index.html 2009-11-12 20:44:11 +0000
4@@ -65,6 +65,8 @@
5
6
7 var picker = new Y.Picker({
8+ picker_activator: '#show-widget',
9+ clear_on_cancel: true,
10 align: {
11 points: [Y.WidgetPositionExt.CC, Y.WidgetPositionExt.CC]
12 },
13@@ -129,9 +131,6 @@
14 });
15 });
16
17- Y.on('click', function () {
18- picker.show();
19- }, '#show-widget');
20 Y.on('change', function () {
21 picker.set(
22 'min_search_chars',
23
24=== modified file 'src-js/lazrjs/picker/picker.js'
25--- src-js/lazrjs/picker/picker.js 2009-10-22 19:27:45 +0000
26+++ src-js/lazrjs/picker/picker.js 2009-11-12 20:44:11 +0000
27@@ -166,6 +166,20 @@
28 * @preventable _defaultSave
29 */
30 this.publish(SAVE, { defaultFn: this._defaultSave } );
31+
32+ // Subscribe to the cancel event so that we can clear the widget when
33+ // requested.
34+ this.subscribe('cancel', this._defaultCancel);
35+
36+ if ( this.get('picker_activator') ) {
37+ var element = Y.one(this.get('picker_activator'));
38+ element.on('click', function(e) {
39+ e.halt();
40+ this.show();
41+ }, this);
42+ element.addClass(this.get('picker_activator_css_class'));
43+ }
44+
45 },
46
47 /**
48@@ -499,7 +513,7 @@
49 // is changed, and reset the selected one to the first one.
50 this.after('batchesChange', function (e) {
51 this._syncBatchesUI();
52- if (this.get(SELECTED_BATCH) == 0){
53+ if (this.get(SELECTED_BATCH) === 0){
54 // If the attribute is already set to the same value,
55 // the 'after' events won't be triggered, so we have
56 // to trigger it manually.
57@@ -550,6 +564,21 @@
58 this._search_input.focus();
59 },
60
61+ /*
62+ * Clear all elements of the picker, resetting it to its original state.
63+ *
64+ * @method _clear
65+ * @param e {Object} The event object.
66+ * @protected
67+ */
68+ _clear: function() {
69+ this._search_input.set('value', '');
70+ this.set('error', '');
71+ this.set('results', [{}]);
72+ this._results_box.set('innerHTML', '');
73+ this.set('batches', []);
74+ },
75+
76 /**
77 * Handle clicks on the 'Search' button or entering the enter key in the
78 * search field. This fires the search event.
79@@ -585,8 +614,25 @@
80 },
81
82 /**
83- * By default, the save event simply hides the widget. The search entered
84- * by the user is passed in the first details attribute of the event.
85+ * By default, the cancel event just hides the widget, but you can
86+ * have it also cleared by setting clear_on_cancel to 'true'.
87+ *
88+ * @method _defaultCancel
89+ * @param e {Event.Facade} An Event Facade object.
90+ * @protected
91+ */
92+ _defaultCancel : function(e) {
93+ Picker.superclass._defaultCancel.apply(this, arguments);
94+ if ( this.get('clear_on_cancel') ) {
95+ this._clear();
96+ }
97+ },
98+
99+ /**
100+ * By default, the save event clears and hides the widget, but you can
101+ * have it not cleared by setting clear_on_save to 'false'. The search
102+ * entered by the user is passed in the first details attribute of the
103+ * event.
104 *
105 * @method _defaultSave
106 * @param e {Event.Facade} An Event Facade object.
107@@ -594,6 +640,9 @@
108 */
109 _defaultSave : function(e) {
110 this.hide();
111+ if ( this.get('clear_on_save') ) {
112+ this._clear();
113+ }
114 },
115
116 /**
117@@ -637,6 +686,42 @@
118
119 Picker.ATTRS = {
120 /**
121+ * Whether or not the search box and result list should be cleared when
122+ * the save event is fired.
123+ *
124+ * @attribute clear_on_save
125+ * @type Boolean
126+ */
127+ clear_on_save: { value: true },
128+
129+ /**
130+ * Whether or not the search box and result list should be cleared when
131+ * the cancel event is fired.
132+ *
133+ * @attribute clear_on_cancel
134+ * @type Boolean
135+ */
136+ clear_on_cancel: { value: false },
137+
138+ /**
139+ * A CSS selector for the DOM element that will activate (show) the picker
140+ * once clicked.
141+ *
142+ * @attribute picker_activator
143+ * @type String
144+ */
145+ picker_activator: { value: null },
146+
147+ /**
148+ * An extra CSS class to be added to the picker_activator, generally used
149+ * to distinguish regular links from js-triggering ones.
150+ *
151+ * @attribute picker_activator_css_class
152+ * @type String
153+ */
154+ picker_activator_css_class: { value: 'js-action' },
155+
156+ /**
157 * Minimum number of characters that need to be entered in the search
158 * string input before a search event will be fired. The search string
159 * will be trimmed before testing the length.
160
161=== modified file 'src-js/lazrjs/picker/tests/picker.js'
162--- src-js/lazrjs/picker/tests/picker.js 2009-10-21 21:43:07 +0000
163+++ src-js/lazrjs/picker/tests/picker.js 2009-11-12 20:44:11 +0000
164@@ -206,7 +206,7 @@
165 [false, true, false, true], results.hasClass(Y.lazr.ui.CSS_ODD));
166 },
167
168- test_clicking_search_button_fires_save_event: function () {
169+ test_clicking_search_button_fires_search_event: function () {
170 this.picker.render();
171
172 var bb = this.picker.get('boundingBox');
173@@ -379,6 +379,14 @@
174 }, 3000);
175 },
176
177+ test_cancel_event_hides_widget: function () {
178+ this.picker.render();
179+
180+ this.picker.fire('cancel', 'bogus');
181+ Assert.isFalse(
182+ this.picker.get('visible'), "The widget should be hidden.");
183+ },
184+
185 test_save_event_hides_widget: function () {
186 this.picker.render();
187
188@@ -387,6 +395,48 @@
189 this.picker.get('visible'), "The widget should be hidden.");
190 },
191
192+ test_save_event_clears_widget_by_default: function () {
193+ this.picker.render();
194+
195+ this.picker._search_input.set('value', 'foo');
196+ this.picker.fire('save', 'bogus');
197+ Assert.areEqual(
198+ '', this.picker._search_input.get('value'),
199+ "The widget hasn't been cleared");
200+ },
201+
202+ test_save_does_not_clear_widget_when_clear_on_save_is_false: function () {
203+ picker = new Y.Picker({clear_on_save: false});
204+ picker.render();
205+
206+ picker._search_input.set('value', 'foo');
207+ picker.fire('save', 'bogus');
208+ Assert.areEqual(
209+ 'foo', picker._search_input.get('value'),
210+ "The widget has been cleared but it should not");
211+ },
212+
213+ test_cancel_event_does_not_clear_widget_by_default: function () {
214+ this.picker.render();
215+
216+ this.picker._search_input.set('value', 'foo');
217+ this.picker.fire('cancel', 'bogus');
218+ Assert.areEqual(
219+ 'foo', this.picker._search_input.get('value'),
220+ "The widget has been cleared but it should not");
221+ },
222+
223+ test_cancel_event_clears_widget_when_clear_on_cancel_is_true: function () {
224+ picker = new Y.Picker({clear_on_cancel: true});
225+ picker.render();
226+
227+ picker._search_input.set('value', 'foo');
228+ picker.fire('cancel', 'bogus');
229+ Assert.areEqual(
230+ '', picker._search_input.get('value'),
231+ "The widget hasn't been cleared");
232+ },
233+
234 test_search_clears_any_eror: function () {
235 this.picker.render();
236 this.picker.set('error', "An error");

Subscribers

People subscribed via source and target branches