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