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