Silence a lot more trailing comma warnings
[Evergreen.git] / Open-ILS / web / js / dojo / openils / widget / TranslatorPopup.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * Copyright (C) 2008  Equinox Software, Inc
4  * Mike Rylander <miker@esilibrary.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * ---------------------------------------------------------------------------
16  */
17
18 if(!dojo._hasResource["openils.widget.TranslatorPopup"]) {
19
20     dojo._hasResource["openils.widget.TranslatorPopup"] = true;
21     dojo.provide("openils.widget.TranslatorPopup");
22     dojo.require("openils.I18N");
23     dojo.require("fieldmapper.dojoData");
24     dojo.require("DojoSRF");
25         dojo.require("dojo.data.ItemFileWriteStore");
26         dojo.require("dijit._Widget");
27         dojo.require("dijit._Templated");
28         dojo.require("dijit.layout.ContentPane");
29         dojo.require("dijit.Dialog");
30         dojo.require("dijit.form.Button");
31         dojo.require("dijit.form.TextBox");
32         dojo.require("dijit.form.ComboBox");
33         dojo.requireLocalization("openils.widget", "TranslatorPopup");
34
35
36     dojo.declare(
37                 'openils.widget.TranslatorPopup',
38                 [dijit._Widget, dijit._Templated],
39                 {
40
41                         templateString : "<span dojoAttachPoint='node'><div dojoAttachPoint='translateLabelNode' dojoType='dijit.form.DropDownButton'><span>Translate</span><div id='${field}_translation_${unique}' dojoType='dijit.TooltipDialog' onOpen='openils.widget.TranslatorPopup.renderTranslatorPopup(${targetObject}, \"${field}\", \"${unique}\")' ><div dojoType='dijit.layout.ContentPane'><table><tbody class='translation_tbody_template' style='display:none; visiblity:hidden;'><tr><th dojoAttachPoint='localeLabelNode'/><td class='locale'><div class='locale_combobox'></div></td><th dojoAttachPoint='translationLabelNode'/><td class='translation'><div class='translation_textbox'></div></td><td><button class='create_button' style='display:none; visiblity:hidden;'><span dojoAttachPoint='createButtonNode'/></button><button class='update_button' style='display:none; visiblity:hidden;'><span dojoAttachPoint='updateButtonNode'/></button><button class='delete_button' style='display:none; visiblity:hidden;'><span dojoAttachPoint='removeButtonNode'/></button></td></tr></tbody><tbody class='translation_tbody'></tbody></table></div></div></div></span>",
42
43                         widgetsInTemplate: true,
44                         field : "",
45                         targetObject : "",
46                         unique : "",
47
48                         postCreate : function () {
49                                 var nls = dojo.i18n.getLocalization("openils.widget", "TranslatorPopup");
50                                 this.localeLabelNode.textContent = nls.locale;
51                                 this.translationLabelNode.textContent = nls.translation;
52                                 this.translateLabelNode.setLabel(nls.translate);
53                                 this.createButtonNode.textContent = nls.create;
54                                 this.updateButtonNode.textContent = nls.update;
55                                 this.removeButtonNode.textContent = nls.remove;
56                         }
57                 }
58         );
59
60         openils.widget.TranslatorPopup.renderTranslatorPopup = function (obj, field, num) {
61                 var node = dojo.byId(field + '_translation_' + num);
62
63                 var trans_list = openils.I18N.getTranslations( obj, field );
64
65                 var trans_template = dojo.query('.translation_tbody_template', node)[0];
66                 var trans_tbody = dojo.query('.translation_tbody', node)[0];
67
68                 // Empty it
69                 while (trans_tbody.lastChild) trans_tbody.removeChild( trans_tbody.lastChild );
70
71                 for (var i in trans_list) {
72                         if (!trans_list[i]) continue;
73
74                         var trans_obj = trans_list[i];
75                         var trans_id = trans_obj.id();
76
77                         var trans_row = dojo.query('tr',trans_template)[0].cloneNode(true);
78                         trans_row.id = 'translation_row_' + trans_id;
79
80                         var old_dijit = dijit.byId('locale_' + trans_id);
81                         if (old_dijit) old_dijit.destroy();
82
83                         old_dijit = dijit.byId('translation_' + trans_id);
84                         if (old_dijit) old_dijit.destroy();
85
86                         dojo.query('.locale_combobox',trans_row).instantiate(
87                                 dijit.form.ComboBox,
88                                 { store:openils.I18N.localeStore,
89                                   searchAttr:'locale',
90                                   lowercase:true,
91                                   required:true,
92                                   id:'locale_' + trans_id,
93                                   value: trans_obj.translation(),
94                                   invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us',
95                                   regExp:'[a-z_]+'
96                                 }
97                         );
98
99                         dojo.query('.translation_textbox',trans_row).instantiate(
100                                 dijit.form.TextBox,
101                                 { required : true,
102                                   id:'translation_' + trans_id,
103                                   value: trans_obj.string()
104                                 }
105                         );
106
107                         dojo.query('.update_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
108                                 dijit.form.Button,
109                                 { onClick :
110                                         (function (trans_id, obj, field) {
111                                                 return function () { openils.widget.TranslatorPopup.updateTranslation(trans_id, obj, field, num) }
112                                         })(trans_id, obj, field) 
113                                 }
114                         );
115
116                         dojo.query('.delete_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
117                                 dijit.form.Button,
118                                 { onClick :
119                                         (function (trans_id, obj, field) {
120                                                 return function () { openils.widget.TranslatorPopup.removeTranslation(trans_id, obj, field, num) }
121                                         })(trans_id, obj, field) 
122                                 }
123                         );
124
125                         trans_tbody.appendChild( trans_row );
126                 }
127
128                 old_dijit = dijit.byId('i18n_new_locale_' + obj.classname + '.' + field + num);
129                 if (old_dijit) old_dijit.destroy();
130
131                 old_dijit = dijit.byId('i18n_new_translation_' + obj.classname + '.' + field + num);
132                 if (old_dijit) old_dijit.destroy();
133
134                 trans_row = dojo.query('tr',trans_template)[0].cloneNode(true);
135
136                 dojo.query('.locale_combobox',trans_row).instantiate(
137                         dijit.form.ComboBox,
138                         { store:openils.I18N.localeStore,
139                           searchAttr:'locale',
140                           id:'i18n_new_locale_' + obj.classname + '.' + field + num,
141                           lowercase:true,
142                           required:true,
143                           invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us',
144                           regExp:'[a-z_]+'
145                         }
146                 );
147
148                 dojo.query('.translation_textbox',trans_row).addClass('new_translation').instantiate(
149                         dijit.form.TextBox,
150                         { required : true,
151                           id:'i18n_new_translation_' + obj.classname + '.' + field + num
152                         }
153                 );
154
155                 dojo.query('.create_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
156                         dijit.form.Button,
157                         { onClick : function () { openils.widget.TranslatorPopup.createTranslation( obj, field, num) } }
158                 );
159
160                 trans_tbody.appendChild( trans_row );
161         }
162
163         openils.widget.TranslatorPopup.updateTranslation = function (trans_id, obj, field, num) {
164                 return openils.widget.TranslatorPopup.changeTranslation('update', trans_id, obj, field, num);
165         }
166         
167         openils.widget.TranslatorPopup.removeTranslation = function (trans_id, obj, field, num) {
168                 return openils.widget.TranslatorPopup.changeTranslation('delete', trans_id, obj, field, num);
169         }
170         
171         openils.widget.TranslatorPopup.changeTranslation = function (method, trans_id, obj, field, num) {
172         
173                 var trans_obj = new i18n().fromHash({
174                         ischanged : method == 'update' ? 1 : 0,
175                         isdeleted : method == 'delete' ? 1 : 0,
176                         id : trans_id,
177                         fq_field : obj.classname + '.' + field,
178                         identity_value : obj.id(),
179                         translation : dijit.byId('locale_' + trans_id).getValue(),
180                         string : dijit.byId('translation_' + trans_id).getValue()
181                 });
182         
183                 openils.widget.TranslatorPopup.writeTranslation(method, trans_obj, obj, field, num);
184         }
185         
186         openils.widget.TranslatorPopup.createTranslation = function (obj, field, num) {
187                 var node = dojo.byId(field + '_translation_' + num);
188         
189                 var trans_obj = new i18n().fromHash({
190                         isnew : 1,
191                         fq_field : obj.classname + '.' + field,
192                         identity_value : obj.id(),
193                         translation : dijit.byId('i18n_new_locale_' + obj.classname + '.' + field + num).getValue(),
194                         string : dijit.byId('i18n_new_translation_' + obj.classname + '.' + field + num).getValue()
195                 });
196         
197                 openils.widget.TranslatorPopup.writeTranslation('create', trans_obj, obj, field, num);
198         }
199         
200         openils.widget.TranslatorPopup.writeTranslation = function (method, trans_obj, obj, field, num) {
201         
202                 OpenSRF.CachedClientSession('open-ils.permacrud').request({
203                         method : 'open-ils.permacrud.' + method + '.i18n',
204                         timeout: 10,
205                         params : [ ses, trans_obj ],
206                         onerror: function (r) {
207                                 //highlighter.editor_pane.red.play();
208                                 if (status_update) status_update( 'Problem saving translation for ' + obj[field]() );
209                         },
210                         oncomplete : function (r) {
211                                 var res = r.recv();
212                                 if ( res && res.content() ) {
213                                         //highlighter.editor_pane.green.play();
214                                         if (status_update) status_update( 'Saved changes to translation for ' + obj[field]() );
215         
216                                         if (method == 'delete') {
217                                                 dojo.NodeList(dojo.byId('translation_row_' + trans_obj.id())).orphan();
218                                         } else if (method == 'create') {
219                                                 var node = dojo.byId(field + '_translation_' + num);
220                                                 dijit.byId('i18n_new_locale_' + obj.classname + '.' + field + num).setValue(null);
221                                                 dijit.byId('i18n_new_translation_' + obj.classname + '.' + field + num).setValue(null);
222                                                 openils.widget.TranslatorPopup.renderTranslatorPopup(obj, field, num);
223                                         }
224         
225                                 } else {
226                                         //highlighter.editor_pane.red.play();
227                                         if (status_update) status_update( 'Problem saving translation for ' + obj[field]() );
228                                 }
229                         },
230                 }).send();
231         }
232
233 }
234
235