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