a35995aace71db53fb0fca66e1c6d3b3111c52d6
[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' 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>",
77
78                         widgetsInTemplate: true,
79                         field : "",
80                         targetObject : ""
81                 }
82         );
83
84         openils.I18N.translationWidget.renderTranslationPopup = function (obj, field) {
85                 var node = dojo.byId(field + '_translation');
86
87                 var trans_list = openils.I18N.getTranslations( obj, field );
88
89                 var trans_template = dojo.query('.translation_tbody_template', node)[0];
90                 var trans_tbody = dojo.query('.translation_tbody', node)[0];
91
92                 // Empty it
93                 while (trans_tbody.lastChild) trans_tbody.removeChild( trans_tbody.lastChild );
94
95                 for (var i in trans_list) {
96                         if (!trans_list[i]) continue;
97
98                         var trans_obj = trans_list[i];
99                         var trans_id = trans_obj.id();
100
101                         var trans_row = dojo.query('tr',trans_template)[0].cloneNode(true);
102                         trans_row.id = 'translation_row_' + trans_id;
103
104                         var old_dijit = dijit.byId('locale_' + trans_id);
105                         if (old_dijit) old_dijit.destroy();
106
107                         old_dijit = dijit.byId('translation_' + trans_id);
108                         if (old_dijit) old_dijit.destroy();
109
110                         dojo.query('.locale_combobox',trans_row).instantiate(
111                                 dijit.form.ComboBox,
112                                 { store:openils.I18N.localeStore,
113                                   searchAttr:'locale',
114                                   lowercase:true,
115                                   required:true,
116                                   id:'locale_' + trans_id,
117                                   value: trans_obj.translation(),
118                                   invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us',
119                                   regExp:'[a-z_]+'
120                                 }
121                         );
122
123                         dojo.query('.translation_textbox',trans_row).instantiate(
124                                 dijit.form.TextBox,
125                                 { required : true,
126                                   id:'translation_' + trans_id,
127                                   value: trans_obj.string()
128                                 }
129                         );
130
131                         dojo.query('.update_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
132                                 dijit.form.Button,
133                                 { onClick :
134                                         (function (trans_id, obj, field) {
135                                                 return function () { openils.I18N.translationWidget.updateTranslation(trans_id, obj, field) }
136                                         })(trans_id, obj, field) 
137                                 }
138                         );
139
140                         dojo.query('.delete_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
141                                 dijit.form.Button,
142                                 { onClick :
143                                         (function (trans_id, obj, field) {
144                                                 return function () { openils.I18N.translationWidget.removeTranslation(trans_id, obj, field) }
145                                         })(trans_id, obj, field) 
146                                 }
147                         );
148
149                         trans_tbody.appendChild( trans_row );
150                 }
151
152                 old_dijit = dijit.byId('i18n_new_locale_' + obj.classname + '.' + field);
153                 if (old_dijit) old_dijit.destroy();
154
155                 old_dijit = dijit.byId('i18n_new_translation_' + obj.classname + '.' + field);
156                 if (old_dijit) old_dijit.destroy();
157
158                 trans_row = dojo.query('tr',trans_template)[0].cloneNode(true);
159
160                 dojo.query('.locale_combobox',trans_row).instantiate(
161                         dijit.form.ComboBox,
162                         { store:openils.I18N.localeStore,
163                           searchAttr:'locale',
164                           id:'i18n_new_locale_' + obj.classname + '.' + field,
165                           lowercase:true,
166                           required:true,
167                           invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us',
168                           regExp:'[a-z_]+'
169                         }
170                 );
171
172                 dojo.query('.translation_textbox',trans_row).addClass('new_translation').instantiate(
173                         dijit.form.TextBox,
174                         { required : true,
175                           id:'i18n_new_translation_' + obj.classname + '.' + field
176                         }
177                 );
178
179                 dojo.query('.create_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
180                         dijit.form.Button,
181                         { onClick : function () { openils.I18N.translationWidget.createTranslation( obj, field) } }
182                 );
183
184                 trans_tbody.appendChild( trans_row );
185         }
186
187         openils.I18N.translationWidget.updateTranslation = function (trans_id, obj, field) {
188                 return openils.I18N.translationWidget.changeTranslation('update', trans_id, obj, field);
189         }
190         
191         openils.I18N.translationWidget.removeTranslation = function (trans_id, obj, field) {
192                 return openils.I18N.translationWidget.changeTranslation('delete', trans_id, obj, field);
193         }
194         
195         openils.I18N.translationWidget.changeTranslation = function (method, trans_id, obj, field) {
196         
197                 var trans_obj = new i18n().fromHash({
198                         ischanged : method == 'update' ? 1 : 0,
199                         isdeleted : method == 'delete' ? 1 : 0,
200                         id : trans_id,
201                         fq_field : obj.classname + '.' + field,
202                         identity_value : obj.id(),
203                         translation : dijit.byId('locale_' + trans_id).getValue(),
204                         string : dijit.byId('translation_' + trans_id).getValue()
205                 });
206         
207                 openils.I18N.translationWidget.writeTranslation(method, trans_obj, obj, field);
208         }
209         
210         openils.I18N.translationWidget.createTranslation = function (obj, field) {
211                 var node = dojo.byId(field + '_translation');
212         
213                 var trans_obj = new i18n().fromHash({
214                         isnew : 1,
215                         fq_field : obj.classname + '.' + field,
216                         identity_value : obj.id(),
217                         translation : dijit.byId('i18n_new_locale_' + obj.classname + '.' + field).getValue(),
218                         string : dijit.byId('i18n_new_translation_' + obj.classname + '.' + field).getValue()
219                 });
220         
221                 openils.I18N.translationWidget.writeTranslation('create', trans_obj, obj, field);
222         }
223         
224         openils.I18N.translationWidget.writeTranslation = function (method, trans_obj, obj, field) {
225         
226                 OpenSRF.CachedClientSession('open-ils.permacrud').request({
227                         method : 'open-ils.permacrud.' + method + '.i18n',
228                         timeout: 10,
229                         params : [ ses, trans_obj ],
230                         onerror: function (r) {
231                                 //highlighter.editor_pane.red.play();
232                                 if (status_update) status_update( 'Problem saving translation for ' + obj[field]() );
233                         },
234                         oncomplete : function (r) {
235                                 var res = r.recv();
236                                 if ( res && res.content() ) {
237                                         //highlighter.editor_pane.green.play();
238                                         if (status_update) status_update( 'Saved changes to translation for ' + obj[field]() );
239         
240                                         if (method == 'delete') {
241                                                 dojo.NodeList(dojo.byId('translation_row_' + trans_obj.id())).orphan();
242                                         } else if (method == 'create') {
243                                                 var node = dojo.byId(field + '_translation');
244                                                 dijit.byId('i18n_new_locale_' + obj.classname + '.' + field).setValue(null);
245                                                 dijit.byId('i18n_new_translation_' + obj.classname + '.' + field).setValue(null);
246                                                 openils.I18N.translationWidget.renderTranslationPopup(obj, field);
247                                         }
248         
249                                 } else {
250                                         //highlighter.editor_pane.red.play();
251                                         if (status_update) status_update( 'Problem saving translation for ' + obj[field]() );
252                                 }
253                         },
254                 }).send();
255         }
256
257 }
258
259