1 /* ---------------------------------------------------------------------------
2 * Copyright (C) 2008 Georgia Public Library Service
3 * Copyright (C) 2008 Equinox Software, Inc
4 * Mike Rylander <miker@esilibrary.com>
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.
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 * ---------------------------------------------------------------------------
18 if(!dojo._hasResource["openils.widget.TranslatorPopup"]) {
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");
38 'openils.widget.TranslatorPopup',
39 [dijit._Widget, dijit._Templated],
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; visiblity: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; visiblity:hidden;'><span dojoAttachPoint='createButtonNode'/></button><button class='update_button' style='display:none; visiblity:hidden;'><span dojoAttachPoint='updateButtonNode'/></button><button class='delete_button' style='display:none; visiblity:hidden;'><span dojoAttachPoint='removeButtonNode'/></button></td></tr></tbody><tbody class='translation_tbody'></tbody></table></div></div></div></span>",
44 widgetsInTemplate: true,
49 postCreate : function () {
51 dojo.connect(this.tooltipDialog, 'onOpen', this, 'renderTranslatorPopup');
53 this.nls = dojo.i18n.getLocalization("openils.widget", "TranslatorPopup");
55 this.translateLabelNode.setLabel(this.nls.translate);
57 this.localeLabelNode.textContent = this.nls.locale;
58 this.translationLabelNode.textContent = this.nls.translation;
60 this.createButtonNode.textContent = this.nls.create;
61 this.updateButtonNode.textContent = this.nls.update;
62 this.removeButtonNode.textContent = this.nls.remove;
65 renderTranslatorPopup : function () {
67 this._targetObject = dojox.jsonPath.query(window, '$.' + this.targetObject, {evalType:"RESULT"});
69 var node = dojo.byId(this.field + '_translation_' + this.unique);
71 var trans_list = openils.I18N.getTranslations( this._targetObject, this.field );
73 var trans_template = dojo.query('.translation_tbody_template', node)[0];
74 var trans_tbody = dojo.query('.translation_tbody', node)[0];
77 while (trans_tbody.lastChild) trans_tbody.removeChild( trans_tbody.lastChild );
79 for (var i in trans_list) {
80 if (!trans_list[i]) continue;
82 var trans_obj = trans_list[i];
83 var trans_id = trans_obj.id();
85 var trans_row = dojo.query('tr',trans_template)[0].cloneNode(true);
86 trans_row.id = 'translation_row_' + trans_id;
88 var old_dijit = dijit.byId('locale_' + trans_id);
89 if (old_dijit) old_dijit.destroy();
91 old_dijit = dijit.byId('translation_' + trans_id);
92 if (old_dijit) old_dijit.destroy();
94 dojo.query('.locale_combobox',trans_row).instantiate(
96 { store:openils.I18N.localeStore,
100 id:'locale_' + trans_id,
101 value: trans_obj.translation(),
102 invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us',
107 dojo.query('.translation_textbox',trans_row).instantiate(
110 id:'translation_' + trans_id,
111 value: trans_obj.string()
115 dojo.query('.update_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
117 { onClick : dojo.hitch( this, 'updateTranslation') }
120 dojo.query('.delete_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
122 { onClick : dojo.hitch( this, 'removeTranslation') }
125 trans_tbody.appendChild( trans_row );
129 old_dijit = dijit.byId('i18n_new_locale_' + this._targetObject.classname + '.' + this.field + this.unique);
130 if (old_dijit) old_dijit.destroy();
132 old_dijit = dijit.byId('i18n_new_translation_' + this._targetObject.classname + '.' + this.field + this.unique);
133 if (old_dijit) old_dijit.destroy();
135 trans_row = dojo.query('tr',trans_template)[0].cloneNode(true);
137 dojo.query('.locale_combobox',trans_row).instantiate(
139 { store:openils.I18N.localeStore,
141 id:'i18n_new_locale_' + this._targetObject.classname + '.' + this.field + this.unique,
144 invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us',
149 dojo.query('.translation_textbox',trans_row).addClass('new_translation').instantiate(
152 id:'i18n_new_translation_' + this._targetObject.classname + '.' + this.field + this.unique
156 dojo.query('.create_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
158 { onClick : dojo.hitch( this, 'createTranslation') }
161 trans_tbody.appendChild( trans_row );
165 updateTranslation : function (t) {
166 return this.changeTranslation('update',t);
169 removeTranslation : function (t) {
170 return changeTranslation('delete',t);
173 changeTranslation : function (method, trans_id) {
175 var trans_obj = new i18n().fromHash({
176 ischanged : method == 'update' ? 1 : 0,
177 isdeleted : method == 'delete' ? 1 : 0,
179 fq_field : this._targetObject.classname + '.' + this.field,
180 identity_value : this._targetObject.id(),
181 translation : dijit.byId('locale_' + trans_id).getValue(),
182 string : dijit.byId('translation_' + trans_id).getValue()
185 this.writeTranslation(method, trans_obj);
188 createTranslation : function () {
189 var node = dojo.byId(this.field + '_translation_' + this.unique);
191 var trans_obj = new i18n().fromHash({
193 fq_field : this._targetObject.classname + '.' + this.field,
194 identity_value : this._targetObject.id(),
195 translation : dijit.byId('i18n_new_locale_' + this._targetObject.classname + '.' + this.field + this.unique).getValue(),
196 string : dijit.byId('i18n_new_translation_' + this._targetObject.classname + '.' + this.field + this.unique).getValue()
199 this.writeTranslation('create', trans_obj);
202 writeTranslation : function (method, trans_obj) {
204 OpenSRF.CachedClientSession('open-ils.permacrud').request({
205 method : 'open-ils.permacrud.' + method + '.i18n',
207 params : [ ses, trans_obj ],
208 onerror: function (r) {
209 //highlighter.editor_pane.red.play();
210 if (status_update) status_update( 'Problem saving translation for ' + this._targetObject[this.field]() );
212 oncomplete : function (r) {
214 if ( res && res.content() ) {
215 //highlighter.editor_pane.green.play();
216 if (status_update) status_update( 'Saved changes to translation for ' + this._targetObject[this.field]() );
218 if (method == 'delete') {
219 dojo.NodeList(dojo.byId('translation_row_' + trans_obj.id())).orphan();
220 } else if (method == 'create') {
221 var node = dojo.byId(this.field + '_translation_' + this.unique);
222 dijit.byId('i18n_new_locale_' + this._targetObject.classname + '.' + this.field + this.unique).setValue(null);
223 dijit.byId('i18n_new_translation_' + this._targetObject.classname + '.' + this.field + this.unique).setValue(null);
224 this.renderTranslatorPopup();
228 //highlighter.editor_pane.red.play();
229 if (status_update) status_update( 'Problem saving translation for ' + this._targetObject[this.field]() );
239 openils.widget.TranslatorPopup._unique = 1;