a99fa41692732b25b061f4f872dabb7f46fc8b0b
[Evergreen.git] / Open-ILS / web / js / dojo / openils / I18N.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.I18N"]) {
19
20     dojo._hasResource["openils.I18N"] = true;
21     dojo.provide("openils.I18N");
22     dojo.require("fieldmapper.dojoData");
23     dojo.require("DojoSRF");
24         dojo.require("dojo.data.ItemFileWriteStore");
25         dojo.require("dijit._Widget");
26         dojo.require("dijit._Templated");
27         dojo.require("dijit.layout.ContentPane");
28         dojo.require("dijit.Dialog");
29         dojo.require("dijit.form.Button");
30         dojo.require("dijit.form.TextBox");
31         dojo.require("dijit.form.ComboBox");
32
33
34     dojo.declare('openils.I18N', null, {});
35
36     openils.I18N.BaseLocales = {
37                 "en" : "English",
38                 "en_us" : "US English",
39                 "en_ca" : "Canadian English",
40                 "es" : "Spanish",
41                 "es_us" : "US Spanish",
42                 "fr" : "French",
43                 "fr_ca" : "Canadian French"
44         };
45
46         openils.I18N.localeStore = new dojo.data.ItemFileWriteStore( { data : {identifier : 'locale', label : 'label', items : [] } } );
47
48         for (var i in openils.I18N.BaseLocales) {
49                 openils.I18N.localeStore.newItem({ locale : i, label : openils.I18N.BaseLocales[i] });
50         }
51
52         openils.I18N.getTranslations = function ( obj /* Fieldmapper object */,  field /* Field to translate */, locale /* optional locale */) {
53                 var classname = obj.classname;
54
55                 // XXX need to derive identity field from IDL...
56                 var ident_field = fieldmapper[classname].Identifier || 'id';
57                 var ident_value = obj[ident_field]();
58
59                 var fielder_args = { query : { fq_field : classname + '.' + field, identity_value : ident_value } };
60                 if (locale) fielder_args.translation = locale;
61
62                 var hash_list = fieldmapper.standardRequest( [ 'open-ils.fielder', 'open-ils.fielder.i18n.atomic'], [ fielder_args ] );
63                 var obj_list = dojo.map( hash_list, function (t) { return new fieldmapper.i18n().fromHash( t ) } );
64
65                 if (locale) return obj_list[0];
66                 return obj_list;
67         }
68
69 //----------------------------------------------------------------
70
71     dojo.declare(
72                 'openils.I18N.translationWidget',
73                 [dijit._Widget, dijit._Templated],
74                 {
75
76                         templateString : "<span dojoAttachPoint='node'><div dojoType='dijit.form.DropDownButton'><span>Translate</span><div id='${field}_translation_${unique}' dojoType='dijit.TooltipDialog' onOpen='openils.I18N.translationWidget.renderTranslationPopup(${targetObject}, \"${field}\", \"${unique}\")' ><div dojoType='dijit.layout.ContentPane'><table><tbody class='translation_tbody_template' style='display:none; visiblity:hidden;'><tr><th>Locale</th><td class='locale'><div class='locale_combobox'></div></td><th>Translation</th><td class='translation'><div class='translation_textbox'></div></td><td><button class='create_button' style='display:none; visiblity:hidden;'>Create</button><button class='update_button' style='display:none; visiblity:hidden;'>Update</button><button class='delete_button' style='display:none; visiblity:hidden;'>Remove</button></td></tr></tbody><tbody class='translation_tbody'></tbody></table></div></div></div></span>",
77
78                         widgetsInTemplate: true,
79                         field : "",
80                         targetObject : "",
81                         unique : ""
82                 }
83         );
84
85         openils.I18N.translationWidget.renderTranslationPopup = function (obj, field, num) {
86                 var node = dojo.byId(field + '_translation_' + num);
87
88                 var trans_list = openils.I18N.getTranslations( obj, field );
89
90                 var trans_template = dojo.query('.translation_tbody_template', node)[0];
91                 var trans_tbody = dojo.query('.translation_tbody', node)[0];
92
93                 // Empty it
94                 while (trans_tbody.lastChild) trans_tbody.removeChild( trans_tbody.lastChild );
95
96                 for (var i in trans_list) {
97                         if (!trans_list[i]) continue;
98
99                         var trans_obj = trans_list[i];
100                         var trans_id = trans_obj.id();
101
102                         var trans_row = dojo.query('tr',trans_template)[0].cloneNode(true);
103                         trans_row.id = 'translation_row_' + trans_id;
104
105                         var old_dijit = dijit.byId('locale_' + trans_id);
106                         if (old_dijit) old_dijit.destroy();
107
108                         old_dijit = dijit.byId('translation_' + trans_id);
109                         if (old_dijit) old_dijit.destroy();
110
111                         dojo.query('.locale_combobox',trans_row).instantiate(
112                                 dijit.form.ComboBox,
113                                 { store:openils.I18N.localeStore,
114                                   searchAttr:'locale',
115                                   lowercase:true,
116                                   required:true,
117                                   id:'locale_' + trans_id,
118                                   value: trans_obj.translation(),
119                                   invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us',
120                                   regExp:'[a-z_]+'
121                                 }
122                         );
123
124                         dojo.query('.translation_textbox',trans_row).instantiate(
125                                 dijit.form.TextBox,
126                                 { required : true,
127                                   id:'translation_' + trans_id,
128                                   value: trans_obj.string()
129                                 }
130                         );
131
132                         dojo.query('.update_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
133                                 dijit.form.Button,
134                                 { onClick :
135                                         (function (trans_id, obj, field) {
136                                                 return function () { openils.I18N.translationWidget.updateTranslation(trans_id, obj, field, num) }
137                                         })(trans_id, obj, field) 
138                                 }
139                         );
140
141                         dojo.query('.delete_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
142                                 dijit.form.Button,
143                                 { onClick :
144                                         (function (trans_id, obj, field) {
145                                                 return function () { openils.I18N.translationWidget.removeTranslation(trans_id, obj, field, num) }
146                                         })(trans_id, obj, field) 
147                                 }
148                         );
149
150                         trans_tbody.appendChild( trans_row );
151                 }
152
153                 old_dijit = dijit.byId('i18n_new_locale_' + obj.classname + '.' + field + num);
154                 if (old_dijit) old_dijit.destroy();
155
156                 old_dijit = dijit.byId('i18n_new_translation_' + obj.classname + '.' + field + num);
157                 if (old_dijit) old_dijit.destroy();
158
159                 trans_row = dojo.query('tr',trans_template)[0].cloneNode(true);
160
161                 dojo.query('.locale_combobox',trans_row).instantiate(
162                         dijit.form.ComboBox,
163                         { store:openils.I18N.localeStore,
164                           searchAttr:'locale',
165                           id:'i18n_new_locale_' + obj.classname + '.' + field + num,
166                           lowercase:true,
167                           required:true,
168                           invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us',
169                           regExp:'[a-z_]+'
170                         }
171                 );
172
173                 dojo.query('.translation_textbox',trans_row).addClass('new_translation').instantiate(
174                         dijit.form.TextBox,
175                         { required : true,
176                           id:'i18n_new_translation_' + obj.classname + '.' + field + num
177                         }
178                 );
179
180                 dojo.query('.create_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
181                         dijit.form.Button,
182                         { onClick : function () { openils.I18N.translationWidget.createTranslation( obj, field, num) } }
183                 );
184
185                 trans_tbody.appendChild( trans_row );
186         }
187
188         openils.I18N.translationWidget.updateTranslation = function (trans_id, obj, field, num) {
189                 return openils.I18N.translationWidget.changeTranslation('update', trans_id, obj, field, num);
190         }
191         
192         openils.I18N.translationWidget.removeTranslation = function (trans_id, obj, field, num) {
193                 return openils.I18N.translationWidget.changeTranslation('delete', trans_id, obj, field, num);
194         }
195         
196         openils.I18N.translationWidget.changeTranslation = function (method, trans_id, obj, field, num) {
197         
198                 var trans_obj = new i18n().fromHash({
199                         ischanged : method == 'update' ? 1 : 0,
200                         isdeleted : method == 'delete' ? 1 : 0,
201                         id : trans_id,
202                         fq_field : obj.classname + '.' + field,
203                         identity_value : obj.id(),
204                         translation : dijit.byId('locale_' + trans_id).getValue(),
205                         string : dijit.byId('translation_' + trans_id).getValue()
206                 });
207         
208                 openils.I18N.translationWidget.writeTranslation(method, trans_obj, obj, field, num);
209         }
210         
211         openils.I18N.translationWidget.createTranslation = function (obj, field, num) {
212                 var node = dojo.byId(field + '_translation_' + num);
213         
214                 var trans_obj = new i18n().fromHash({
215                         isnew : 1,
216                         fq_field : obj.classname + '.' + field,
217                         identity_value : obj.id(),
218                         translation : dijit.byId('i18n_new_locale_' + obj.classname + '.' + field + num).getValue(),
219                         string : dijit.byId('i18n_new_translation_' + obj.classname + '.' + field + num).getValue()
220                 });
221         
222                 openils.I18N.translationWidget.writeTranslation('create', trans_obj, obj, field, num);
223         }
224         
225         openils.I18N.translationWidget.writeTranslation = function (method, trans_obj, obj, field, num) {
226         
227                 OpenSRF.CachedClientSession('open-ils.permacrud').request({
228                         method : 'open-ils.permacrud.' + method + '.i18n',
229                         timeout: 10,
230                         params : [ ses, trans_obj ],
231                         onerror: function (r) {
232                                 //highlighter.editor_pane.red.play();
233                                 if (status_update) status_update( 'Problem saving translation for ' + obj[field]() );
234                         },
235                         oncomplete : function (r) {
236                                 var res = r.recv();
237                                 if ( res && res.content() ) {
238                                         //highlighter.editor_pane.green.play();
239                                         if (status_update) status_update( 'Saved changes to translation for ' + obj[field]() );
240         
241                                         if (method == 'delete') {
242                                                 dojo.NodeList(dojo.byId('translation_row_' + trans_obj.id())).orphan();
243                                         } else if (method == 'create') {
244                                                 var node = dojo.byId(field + '_translation_' + num);
245                                                 dijit.byId('i18n_new_locale_' + obj.classname + '.' + field + num).setValue(null);
246                                                 dijit.byId('i18n_new_translation_' + obj.classname + '.' + field + num).setValue(null);
247                                                 openils.I18N.translationWidget.renderTranslationPopup(obj, field, num);
248                                         }
249         
250                                 } else {
251                                         //highlighter.editor_pane.red.play();
252                                         if (status_update) status_update( 'Problem saving translation for ' + obj[field]() );
253                                 }
254                         },
255                 }).send();
256         }
257
258 }
259
260