Merge lp:~openerp-dev/openobject-client-web/inline-list-prompt-action into lp:openobject-client-web/trunk

Proposed by Vaibhav Darji
Status: Rejected
Rejected by: Xavier (Open ERP)
Proposed branch: lp:~openerp-dev/openobject-client-web/inline-list-prompt-action
Merge into: lp:openobject-client-web/trunk
Diff against target: 249 lines (+100/-75)
3 files modified
addons/openerp/static/javascript/form.js (+29/-4)
addons/openerp/static/javascript/listgrid.js (+58/-64)
addons/openerp/widgets/templates/listgrid.mako (+13/-7)
To merge this branch: bzr merge lp:~openerp-dev/openobject-client-web/inline-list-prompt-action
Reviewer Review Type Date Requested Status
Krisztian Eyssen (community) Approve
Xavier (Open ERP) (community) Needs Fixing
Review via email: mp+39436@code.launchpad.net

Description of the change

Inline ListView Prompt action same as form view.

To post a comment you must log in.
Revision history for this message
Xavier (Open ERP) (xmo-deactivatedaccount) wrote :

* The validation message is terrible, what is "the record"? How is anybody going to understand that? It was OK for form, but for list is needs to be much clearer.
* old listgridValidation(name, o2m) have become far too complex and redundant, please extract redundancies into a function
* In validateForm, `_list_view` can not be `0` or an empty string (or id selector will fail anyway), so you don't need `_list_view != null`, `_list_view` should be enough to test that there is a list view id.
* You probably should not use `is_list_changed` (denoting a boolean value) to store the id of the modified list. Either rename key (to e.g. `modified_list`) or use two different data keys (a boolean to know if a list changed, and then a string to know *which* list changed)
-- listgrid.js
* Why async=false in listgrid save?
* Do not use alert() for errors
* If you have an id (as string) and want to turn it into a jquery id selector, there is now an `idSelector` function in openobject.dom.js, this should be used in ListView.save and in listgridValidation
* Also the `return` after $focus_field is redundant
* In listgridValidation, `0` is not a valid second parameter for parseInt, use `10`.
* jQuery.ajax can take a context keyword argument, this means the `var self=this` aliasing is now unnecessary (just pass `context: this` and in the success callback replace `self` by `this`)

Also more general comments:
* create more, smaller and more focused commits. It's easier to review and cherrypick if each commit does a single job instead of having a single big commit doing 3000 things at once. Intent of change also becomes clearer due to commit messages. You're not limited in number of commits and you don't have to pay for them so...
* Please remember to add comment if you commit/push anything new as launchpad doesn't warn reviewers

review: Needs Fixing
Revision history for this message
Krisztian Eyssen (eyssen) :
review: Approve

Unmerged revisions

3736. By Vaibhav Darji

[IMP] Improved inline listgrid validation same as form.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'addons/openerp/static/javascript/form.js'
2--- addons/openerp/static/javascript/form.js 2010-10-27 09:36:11 +0000
3+++ addons/openerp/static/javascript/form.js 2010-10-27 11:26:02 +0000
4@@ -1186,14 +1186,38 @@
5 }
6
7
8-function validateForm(){
9- jQuery('#view_form table tr td:first').find('input:not([type=hidden]), select').change(function(){
10- jQuery('#view_form').data('is_form_changed', true);
11- });
12+function validateForm(_list_view){
13+ var $form = jQuery('#view_form');
14+ if (_list_view != null) {
15+ var $list = jQuery('[id="' + _list_view + '"]').removeAttr('current_id');
16+
17+ if($form.data('is_list_changed')) {
18+ $form.removeData('is_list_changed');
19+ }
20+
21+ jQuery('table.grid[id="'+_list_view+'_grid'+'"] tr.grid-row[record] td.grid-cell:not(.selector)').find('input, select').change(function(){
22+ $form.data('is_list_changed', _list_view);
23+ $list.attr('current_id',parseInt(jQuery(this).closest('tr.grid-row').attr('record'), 10) || -1);
24+ });
25+ }
26+ else {
27+ jQuery('#view_form table tr td:first').find('input:not([type=hidden]), select').change(function(){
28+ $form.data('is_form_changed', true);
29+ });
30+ }
31 }
32
33 function validate_action() {
34 var $form = jQuery('#view_form');
35+ if($form.data('is_list_changed') && confirm('The record has been modified \n Do you want to save it ?')){
36+ var _list = $form.data('is_list_changed');
37+ var current_id = jQuery('[id="' + _list + '"]').attr('current_id');
38+ var record_id = jQuery('[id="' + _list + '"]').attr('_record_id');
39+ if(current_id != null) {
40+ new ListView(_list).save(current_id, record_id);
41+ }
42+ }
43+
44 if ($form.data('is_form_changed') && confirm('The record has been modified \n Do you want to save it ?')) {
45 if (!validate_required($form.get(0))) {
46 return;
47@@ -1210,6 +1234,7 @@
48 }
49 });
50 }
51+
52 if (arguments.length) {
53 var params = arguments[0];
54 var action = arguments[1];
55
56=== modified file 'addons/openerp/static/javascript/listgrid.js'
57--- addons/openerp/static/javascript/listgrid.js 2010-10-27 07:13:40 +0000
58+++ addons/openerp/static/javascript/listgrid.js 2010-10-27 11:26:02 +0000
59@@ -544,44 +544,49 @@
60 args['_terp_source'] = this.name;
61
62 var self = this;
63- var req = openobject.http.postJSON('/openerp/listgrid/save', args);
64-
65 var $current_record = jQuery('table[id="'+this.name+'_grid'+'"]').find('tr.grid-row[record="'+id+'"]');
66- req.addCallback(function(obj) {
67- if (obj.error) {
68- error_display(obj.error);
69-
70- var $focus_field;
71- for (var k in data) {
72- var $req_field = $current_record.find('td [id="'+'_terp_listfields/'+k+'"].requiredfield');
73-
74- if(!$req_field.length)
75- continue;
76- if($req_field.attr('kind') == 'many2one') {
77- $req_field = $current_record.find('td [id="'+'_terp_listfields/'+k+'_text'+'"]');
78- }
79-
80- var req_value = $req_field.val();
81- if(req_value == '') {
82- $req_field.addClass('errorfield');
83- if(!$focus_field) {
84- $focus_field = $req_field;
85- }
86- } else if($req_field.hasClass('errorfield')) {
87- $req_field.removeClass('errorfield');
88- }
89- }
90- if($focus_field) {
91- $focus_field.focus();
92- }
93- } else {
94- openobject.dom.get(prefix + '_terp_id').value = obj.id;
95- openobject.dom.get(prefix + '_terp_ids').value = obj.ids;
96-
97- if(prev_id != undefined) {
98- self.reload(prev_id , prefix ? 1 : 0);
99+ jQuery.ajax({
100+ url: '/openerp/listgrid/save',
101+ data: args,
102+ dataType: 'json',
103+ type: 'POST',
104+ async: false,
105+ error: function(obj) {
106+ alert('error'+obj)
107+ },
108+ success: function(obj) {
109+ if(obj.error) {
110+ error_display(obj.error);
111+ var $focus_field;
112+ for (var k in data) {
113+ var $req_field = $current_record.find('td [id="'+'_terp_listfields/'+k+'"].requiredfield');
114+ if(!$req_field.length)
115+ continue;
116+ if($req_field.attr('kind') == 'many2one') {
117+ $req_field = $current_record.find('td [id="'+'_terp_listfields/'+k+'_text'+'"]');
118+ }
119+ var req_value = $req_field.val();
120+ if(req_value == '') {
121+ $req_field.addClass('errorfield');
122+ if(!$focus_field) {
123+ $focus_field = $req_field;
124+ }
125+ } else if($req_field.hasClass('errorfield')) {
126+ $req_field.removeClass('errorfield');
127+ }
128+ }
129+ if($focus_field) {
130+ $focus_field.focus();
131+ }
132+ return;
133 } else {
134- self.reload(id > 0 ? null : -1, prefix ? 1 : 0);
135+ jQuery('[id="'+prefix+'_terp_id'+'"]').val(obj.id);
136+ jQuery('[id="'+prefix+'_terp_ids'+'"]').val(obj.ids);
137+ if(prev_id != undefined) {
138+ self.reload(prev_id , prefix ? 1 : 0);
139+ } else {
140+ self.reload(id > 0 ? null : -1, prefix ? 1 : 0);
141+ }
142 }
143 }
144 });
145@@ -841,36 +846,25 @@
146 }
147 });
148
149-function validateList(_list) {
150- var $list = jQuery('[id="' + _list + '"]').removeAttr('current_id');
151- var $check = jQuery('table.grid[id="'+_list+'_grid'+'"] tr.grid-row[record] td.grid-cell:not(.selector)').find('input, select');
152- $check.change(function() {
153- $list.attr(
154- 'current_id',
155- parseInt(jQuery(this).closest('tr.grid-row').attr('record'), 10) || -1);
156- });
157-}
158-
159-function listgridValidation(_list, o2m, record_id) {
160- o2m = parseInt(o2m, 0);
161- var current_id = jQuery('[id="' + _list + '"]').attr('current_id');
162- // not(null | undefined)
163- if(current_id != null) {
164- if(confirm('The record has been modified \n Do you want to save it ?')) {
165- new ListView(_list).save(current_id, record_id);
166- }
167- } else{
168- if(o2m) {
169- var detail = jQuery('table.one2many[id$="' + _list + '"]').attr('detail');
170- if(record_id == undefined || record_id == -1) {
171- new One2Many(_list, detail).create();
172- } else {
173- new ListView(_list).edit(record_id);
174- }
175- } else if(record_id == -1) {
176- new ListView(_list).create();
177+function listgridValidation(_list) {
178+ var $list = jQuery('[id="' + _list + '"]');
179+ var o2m = parseInt($list.attr('_o2m'), 0);
180+ var current_id = $list.attr('current_id');
181+ var record_id = $list.attr('_record_id');
182+ if (current_id != null) {
183+ return;
184+ }
185+
186+ if (o2m) {
187+ var detail = jQuery('table.one2many[id$="' + _list + '"]').attr('detail');
188+ if (record_id == undefined || record_id == -1) {
189+ new One2Many(_list, detail).create();
190 } else {
191 new ListView(_list).edit(record_id);
192 }
193+ } else if(record_id == -1) {
194+ new ListView(_list).create();
195+ } else {
196+ new ListView(_list).edit(record_id);
197 }
198 }
199
200=== modified file 'addons/openerp/widgets/templates/listgrid.mako'
201--- addons/openerp/widgets/templates/listgrid.mako 2010-10-25 12:46:53 +0000
202+++ addons/openerp/widgets/templates/listgrid.mako 2010-10-27 11:26:02 +0000
203@@ -133,17 +133,22 @@
204 ${_('Add')}
205 </button>
206 % elif o2m:
207- <button title="${_('Create new record.')}" id="${name}_btn_"
208- onclick="listgridValidation('${name}', '${o2m or 0}', -1); return false;">
209- ${_('New')}
210- </button>
211+ <button title="${_('Create new record.')}" id="${name}_btn_">${_('New')}</button>
212+ <script type="text/javascript">
213+ jQuery('[id=${name}_btn_]').click(function(){
214+ jQuery('[id=${name}]').attr({'_o2m': '${o2m or 0}', '_record_id': -1});
215+ validate_action('${name}', listgridValidation);
216+ return false;
217+ });
218+ </script>
219 % else:
220 % if not dashboard:
221 <button id="${name}_new" title="${_('Create new record.')}">${_('New')}</button>
222 % if editors:
223 <script type="text/javascript">
224 jQuery('[id=${name}_new]').click(function() {
225- listgridValidation('${name}', '${o2m or 0}', -1)
226+ jQuery('[id=${name}]').attr({'_o2m': '${o2m or 0}', '_record_id': -1});
227+ validate_action('${name}', listgridValidation);
228 return false;
229 });
230 </script>
231@@ -160,7 +165,7 @@
232 % if not m2m and not dashboard and editors:
233 <script type="text/javascript">
234 jQuery(document).ready(function() {
235- validateList('${name}')
236+ validateForm('${name}', '${o2m or 0}')
237 });
238 </script>
239 % endif
240@@ -328,7 +333,8 @@
241 jQuery(row).click(function(event) {
242 if (!jQuery(event.target).is(':input, img, option, td.m2o_coplition')) {
243 var record_id = parseInt(jQuery(row).attr('record'), 10) || -1;
244- listgridValidation('${name}','${o2m or 0}', record_id);
245+ jQuery('[id=${name}]').attr({'_o2m': '${o2m or 0}', '_record_id': record_id});
246+ validate_action('${name}', listgridValidation);
247 }
248 });
249 });