From 6413fa7f9b3eb5dfa8638cc6cfa18358201b544a Mon Sep 17 00:00:00 2001 From: miker Date: Mon, 28 Apr 2008 05:08:58 +0000 Subject: [PATCH 1/1] made translation widget self-contained git-svn-id: svn://svn.open-ils.org/ILS/trunk@9473 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../js/dojo/openils/widget/TranslatorPopup.js | 350 +++++++++--------- 1 file changed, 180 insertions(+), 170 deletions(-) diff --git a/Open-ILS/web/js/dojo/openils/widget/TranslatorPopup.js b/Open-ILS/web/js/dojo/openils/widget/TranslatorPopup.js index d13c9a0b48..7c7262e7a3 100644 --- a/Open-ILS/web/js/dojo/openils/widget/TranslatorPopup.js +++ b/Open-ILS/web/js/dojo/openils/widget/TranslatorPopup.js @@ -30,6 +30,7 @@ if(!dojo._hasResource["openils.widget.TranslatorPopup"]) { dojo.require("dijit.form.Button"); dojo.require("dijit.form.TextBox"); dojo.require("dijit.form.ComboBox"); + dojo.require("dojox.jsonPath"); dojo.requireLocalization("openils.widget", "TranslatorPopup"); @@ -38,7 +39,7 @@ if(!dojo._hasResource["openils.widget.TranslatorPopup"]) { [dijit._Widget, dijit._Templated], { - templateString : "
Translate
", + templateString : "
Translate
", widgetsInTemplate: true, field : "", @@ -46,189 +47,198 @@ if(!dojo._hasResource["openils.widget.TranslatorPopup"]) { unique : "", postCreate : function () { - var nls = dojo.i18n.getLocalization("openils.widget", "TranslatorPopup"); - this.localeLabelNode.textContent = nls.locale; - this.translationLabelNode.textContent = nls.translation; - this.translateLabelNode.setLabel(nls.translate); - this.createButtonNode.textContent = nls.create; - this.updateButtonNode.textContent = nls.update; - this.removeButtonNode.textContent = nls.remove; - } - } - ); - - openils.widget.TranslatorPopup.renderTranslatorPopup = function (obj, field, num) { - var node = dojo.byId(field + '_translation_' + num); - - var trans_list = openils.I18N.getTranslations( obj, field ); - - var trans_template = dojo.query('.translation_tbody_template', node)[0]; - var trans_tbody = dojo.query('.translation_tbody', node)[0]; - // Empty it - while (trans_tbody.lastChild) trans_tbody.removeChild( trans_tbody.lastChild ); + dojo.connect(this.tooltipDialog, 'onOpen', this, 'renderTranslatorPopup'); - for (var i in trans_list) { - if (!trans_list[i]) continue; + this.nls = dojo.i18n.getLocalization("openils.widget", "TranslatorPopup"); - var trans_obj = trans_list[i]; - var trans_id = trans_obj.id(); + this.translateLabelNode.setLabel(this.nls.translate); - var trans_row = dojo.query('tr',trans_template)[0].cloneNode(true); - trans_row.id = 'translation_row_' + trans_id; + this.localeLabelNode.textContent = this.nls.locale; + this.translationLabelNode.textContent = this.nls.translation; - var old_dijit = dijit.byId('locale_' + trans_id); - if (old_dijit) old_dijit.destroy(); - - old_dijit = dijit.byId('translation_' + trans_id); - if (old_dijit) old_dijit.destroy(); - - dojo.query('.locale_combobox',trans_row).instantiate( - dijit.form.ComboBox, - { store:openils.I18N.localeStore, - searchAttr:'locale', - lowercase:true, - required:true, - id:'locale_' + trans_id, - value: trans_obj.translation(), - invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us', - regExp:'[a-z_]+' - } - ); + this.createButtonNode.textContent = this.nls.create; + this.updateButtonNode.textContent = this.nls.update; + this.removeButtonNode.textContent = this.nls.remove; + }, - dojo.query('.translation_textbox',trans_row).instantiate( - dijit.form.TextBox, - { required : true, - id:'translation_' + trans_id, - value: trans_obj.string() - } - ); - - dojo.query('.update_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate( - dijit.form.Button, - { onClick : - (function (trans_id, obj, field) { - return function () { openils.widget.TranslatorPopup.updateTranslation(trans_id, obj, field, num) } - })(trans_id, obj, field) + renderTranslatorPopup : function () { + + this._targetObject = dojox.jsonPath.query(window, '$.' + this.targetObject, {evalType:"RESULT"}); + + var node = dojo.byId(this.field + '_translation_' + this.unique); + + var trans_list = openils.I18N.getTranslations( this._targetObject, this.field ); + + var trans_template = dojo.query('.translation_tbody_template', node)[0]; + var trans_tbody = dojo.query('.translation_tbody', node)[0]; + + // Empty it + while (trans_tbody.lastChild) trans_tbody.removeChild( trans_tbody.lastChild ); + + for (var i in trans_list) { + if (!trans_list[i]) continue; + + var trans_obj = trans_list[i]; + var trans_id = trans_obj.id(); + + var trans_row = dojo.query('tr',trans_template)[0].cloneNode(true); + trans_row.id = 'translation_row_' + trans_id; + + var old_dijit = dijit.byId('locale_' + trans_id); + if (old_dijit) old_dijit.destroy(); + + old_dijit = dijit.byId('translation_' + trans_id); + if (old_dijit) old_dijit.destroy(); + + dojo.query('.locale_combobox',trans_row).instantiate( + dijit.form.ComboBox, + { store:openils.I18N.localeStore, + searchAttr:'locale', + lowercase:true, + required:true, + id:'locale_' + trans_id, + value: trans_obj.translation(), + invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us', + regExp:'[a-z_]+' + } + ); + + dojo.query('.translation_textbox',trans_row).instantiate( + dijit.form.TextBox, + { required : true, + id:'translation_' + trans_id, + value: trans_obj.string() + } + ); + + dojo.query('.update_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate( + dijit.form.Button, + { onClick : dojo.hitch( this, 'updateTranslation') } + ); + + dojo.query('.delete_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate( + dijit.form.Button, + { onClick : dojo.hitch( this, 'removeTranslation') } + ); + + trans_tbody.appendChild( trans_row ); + } - ); - - dojo.query('.delete_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate( - dijit.form.Button, - { onClick : - (function (trans_id, obj, field) { - return function () { openils.widget.TranslatorPopup.removeTranslation(trans_id, obj, field, num) } - })(trans_id, obj, field) - } - ); - - trans_tbody.appendChild( trans_row ); - } - - old_dijit = dijit.byId('i18n_new_locale_' + obj.classname + '.' + field + num); - if (old_dijit) old_dijit.destroy(); - - old_dijit = dijit.byId('i18n_new_translation_' + obj.classname + '.' + field + num); - if (old_dijit) old_dijit.destroy(); + + old_dijit = dijit.byId('i18n_new_locale_' + this._targetObject.classname + '.' + this.field + this.unique); + if (old_dijit) old_dijit.destroy(); + + old_dijit = dijit.byId('i18n_new_translation_' + this._targetObject.classname + '.' + this.field + this.unique); + if (old_dijit) old_dijit.destroy(); + + trans_row = dojo.query('tr',trans_template)[0].cloneNode(true); + + dojo.query('.locale_combobox',trans_row).instantiate( + dijit.form.ComboBox, + { store:openils.I18N.localeStore, + searchAttr:'locale', + id:'i18n_new_locale_' + this._targetObject.classname + '.' + this.field + this.unique, + lowercase:true, + required:true, + invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us', + regExp:'[a-z_]+' + } + ); + + dojo.query('.translation_textbox',trans_row).addClass('new_translation').instantiate( + dijit.form.TextBox, + { required : true, + id:'i18n_new_translation_' + this._targetObject.classname + '.' + this.field + this.unique + } + ); + + dojo.query('.create_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate( + dijit.form.Button, + { onClick : dojo.hitch( this, 'createTranslation') } + ); + + trans_tbody.appendChild( trans_row ); - trans_row = dojo.query('tr',trans_template)[0].cloneNode(true); + }, - dojo.query('.locale_combobox',trans_row).instantiate( - dijit.form.ComboBox, - { store:openils.I18N.localeStore, - searchAttr:'locale', - id:'i18n_new_locale_' + obj.classname + '.' + field + num, - lowercase:true, - required:true, - invalidMessage:'Specify locale as {languageCode}_{countryCode}, like en_us', - regExp:'[a-z_]+' + updateTranslation : function (t) { + return this.changeTranslation('update',t); + }, + + removeTranslation : function (t) { + return changeTranslation('delete',t); + }, + + changeTranslation : function (method, trans_id) { + + var trans_obj = new i18n().fromHash({ + ischanged : method == 'update' ? 1 : 0, + isdeleted : method == 'delete' ? 1 : 0, + id : trans_id, + fq_field : this._targetObject.classname + '.' + this.field, + identity_value : this._targetObject.id(), + translation : dijit.byId('locale_' + trans_id).getValue(), + string : dijit.byId('translation_' + trans_id).getValue() + }); + + this.writeTranslation(method, trans_obj); + }, + + createTranslation : function () { + var node = dojo.byId(this.field + '_translation_' + this.unique); + + var trans_obj = new i18n().fromHash({ + isnew : 1, + fq_field : this._targetObject.classname + '.' + this.field, + identity_value : this._targetObject.id(), + translation : dijit.byId('i18n_new_locale_' + this._targetObject.classname + '.' + this.field + this.unique).getValue(), + string : dijit.byId('i18n_new_translation_' + this._targetObject.classname + '.' + this.field + this.unique).getValue() + }); + + this.writeTranslation('create', trans_obj); + }, + + writeTranslation : function (method, trans_obj) { + + OpenSRF.CachedClientSession('open-ils.permacrud').request({ + method : 'open-ils.permacrud.' + method + '.i18n', + timeout: 10, + params : [ ses, trans_obj ], + onerror: function (r) { + //highlighter.editor_pane.red.play(); + if (status_update) status_update( 'Problem saving translation for ' + this._targetObject[this.field]() ); + }, + oncomplete : function (r) { + var res = r.recv(); + if ( res && res.content() ) { + //highlighter.editor_pane.green.play(); + if (status_update) status_update( 'Saved changes to translation for ' + this._targetObject[this.field]() ); + + if (method == 'delete') { + dojo.NodeList(dojo.byId('translation_row_' + trans_obj.id())).orphan(); + } else if (method == 'create') { + var node = dojo.byId(this.field + '_translation_' + this.unique); + dijit.byId('i18n_new_locale_' + this._targetObject.classname + '.' + this.field + this.unique).setValue(null); + dijit.byId('i18n_new_translation_' + this._targetObject.classname + '.' + this.field + this.unique).setValue(null); + this.renderTranslatorPopup(); + } + + } else { + //highlighter.editor_pane.red.play(); + if (status_update) status_update( 'Problem saving translation for ' + this._targetObject[this.field]() ); + } + }, + }).send(); } - ); - dojo.query('.translation_textbox',trans_row).addClass('new_translation').instantiate( - dijit.form.TextBox, - { required : true, - id:'i18n_new_translation_' + obj.classname + '.' + field + num - } - ); + } - dojo.query('.create_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate( - dijit.form.Button, - { onClick : function () { openils.widget.TranslatorPopup.createTranslation( obj, field, num) } } - ); + ); + + openils.widget.TranslatorPopup._unique = 1; - trans_tbody.appendChild( trans_row ); - } - openils.widget.TranslatorPopup.updateTranslation = function (trans_id, obj, field, num) { - return openils.widget.TranslatorPopup.changeTranslation('update', trans_id, obj, field, num); - } - - openils.widget.TranslatorPopup.removeTranslation = function (trans_id, obj, field, num) { - return openils.widget.TranslatorPopup.changeTranslation('delete', trans_id, obj, field, num); - } - - openils.widget.TranslatorPopup.changeTranslation = function (method, trans_id, obj, field, num) { - - var trans_obj = new i18n().fromHash({ - ischanged : method == 'update' ? 1 : 0, - isdeleted : method == 'delete' ? 1 : 0, - id : trans_id, - fq_field : obj.classname + '.' + field, - identity_value : obj.id(), - translation : dijit.byId('locale_' + trans_id).getValue(), - string : dijit.byId('translation_' + trans_id).getValue() - }); - - openils.widget.TranslatorPopup.writeTranslation(method, trans_obj, obj, field, num); - } - - openils.widget.TranslatorPopup.createTranslation = function (obj, field, num) { - var node = dojo.byId(field + '_translation_' + num); - - var trans_obj = new i18n().fromHash({ - isnew : 1, - fq_field : obj.classname + '.' + field, - identity_value : obj.id(), - translation : dijit.byId('i18n_new_locale_' + obj.classname + '.' + field + num).getValue(), - string : dijit.byId('i18n_new_translation_' + obj.classname + '.' + field + num).getValue() - }); - - openils.widget.TranslatorPopup.writeTranslation('create', trans_obj, obj, field, num); - } - - openils.widget.TranslatorPopup.writeTranslation = function (method, trans_obj, obj, field, num) { - - OpenSRF.CachedClientSession('open-ils.permacrud').request({ - method : 'open-ils.permacrud.' + method + '.i18n', - timeout: 10, - params : [ ses, trans_obj ], - onerror: function (r) { - //highlighter.editor_pane.red.play(); - if (status_update) status_update( 'Problem saving translation for ' + obj[field]() ); - }, - oncomplete : function (r) { - var res = r.recv(); - if ( res && res.content() ) { - //highlighter.editor_pane.green.play(); - if (status_update) status_update( 'Saved changes to translation for ' + obj[field]() ); - - if (method == 'delete') { - dojo.NodeList(dojo.byId('translation_row_' + trans_obj.id())).orphan(); - } else if (method == 'create') { - var node = dojo.byId(field + '_translation_' + num); - dijit.byId('i18n_new_locale_' + obj.classname + '.' + field + num).setValue(null); - dijit.byId('i18n_new_translation_' + obj.classname + '.' + field + num).setValue(null); - openils.widget.TranslatorPopup.renderTranslatorPopup(obj, field, num); - } - - } else { - //highlighter.editor_pane.red.play(); - if (status_update) status_update( 'Problem saving translation for ' + obj[field]() ); - } - }, - }).send(); - } } -- 2.43.2