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