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