in-db translation Digit widget
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 17 Apr 2008 05:21:21 +0000 (05:21 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 17 Apr 2008 05:21:21 +0000 (05:21 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@9370 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/web/js/dojo/openils/I18N.js

index 4d7ce0d..f118045 100644 (file)
@@ -20,7 +20,11 @@ if(!dojo._hasResource["openils.I18N"]) {
     dojo._hasResource["openils.I18N"] = true;
     dojo.provide("openils.I18N");
     dojo.require("fieldmapper.dojoData");
+    dojo.require("DojoSRF");
        dojo.require("dojo.data.ItemFileWriteStore");
+       dojo.require("dijit._Widget");
+       dojo.require("dijit._Templated");
+
 
     dojo.declare('openils.I18N', null, {});
 
@@ -56,8 +60,195 @@ if(!dojo._hasResource["openils.I18N"]) {
                if (locale) return obj_list[0];
                return obj_list;
        }
+
+//----------------------------------------------------------------
+
+    dojo.declare(
+               'openils.I18N.translationWidget',
+               [dijit._Widget, dijit._Templated],
+               {
+
+                       templateString : "<span dojoAttachPoint='node'><div dojoType='dijit.form.DropDownButton'><span>Translate</span><div id='${field}_translation' dojoType='dijit.TooltipDialog' onOpen='openils.I18N.translationWidget.renderTranslationPopup(${targetObject}, \"${field}\")' ><div dojoType='dijit.layout.ContentPane'><table><tbody class='translation_tbody_template' style='display:none; visiblity:hidden;'><tr><th>Locale</th><td class='locale'><div class='locale_combobox'></div></td><th>Translation</th><td class='translation'><div class='translation_textbox'></div></td><td><button class='create_button' style='display:none; visiblity:hidden;'>Create</button><button class='update_button' style='display:none; visiblity:hidden;'>Update</button><button class='delete_button' style='display:none; visiblity:hidden;'>Remove</button></td></tr></tbody><tbody class='translation_tbody'></tbody></table></div></div></div></span>",
+
+                       widgetsInTemplate: true,
+                       field : "",
+                       targetObject : ""
+               }
+       );
+
+       openils.I18N.translationWidget.renderTranslationPopup = function (obj, field) {
+               var node = dojo.byId(field + '_translation');
+
+               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 );
+
+               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 :
+                                       (function (trans_id, obj, field) {
+                                               return function () { openils.I18N.translationWidget.updateTranslation(trans_id, obj, field) }
+                                       })(trans_id, obj, field) 
+                               }
+                       );
+
+                       dojo.query('.delete_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
+                               dijit.form.Button,
+                               { onClick :
+                                       (function (trans_id, obj, field) {
+                                               return function () { openils.I18N.translationWidget.removeTranslation(trans_id, obj, field) }
+                                       })(trans_id, obj, field) 
+                               }
+                       );
+
+                       trans_tbody.appendChild( trans_row );
+               }
+
+               old_dijit = dijit.byId('i18n_new_locale_' + obj.classname + '.' + field);
+               if (old_dijit) old_dijit.destroy();
+
+               old_dijit = dijit.byId('i18n_new_translation_' + obj.classname + '.' + field);
+               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_' + obj.classname + '.' + field,
+                         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_' + obj.classname + '.' + field
+                       }
+               );
+
+               dojo.query('.create_button',trans_row).style({ visibility : 'visible', display : 'inline'}).instantiate(
+                       dijit.form.Button,
+                       { onClick : function () { openils.I18N.translationWidget.createTranslation( obj, field) } }
+               );
+
+               trans_tbody.appendChild( trans_row );
+       }
+
+       openils.I18N.translationWidget.updateTranslation = function (trans_id, obj, field) {
+               return openils.I18N.translationWidget.changeTranslation('update', trans_id, obj, field);
+       }
+       
+       openils.I18N.translationWidget.removeTranslation = function (trans_id, obj, field) {
+               return openils.I18N.translationWidget.changeTranslation('delete', trans_id, obj, field);
+       }
+       
+       openils.I18N.translationWidget.changeTranslation = function (method, trans_id, obj, field) {
+       
+               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.I18N.translationWidget.writeTranslation(method, trans_obj, obj, field);
+       }
+       
+       openils.I18N.translationWidget.createTranslation = function (obj, field) {
+               var node = dojo.byId(field + '_translation');
+       
+               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).getValue(),
+                       string : dijit.byId('i18n_new_translation_' + obj.classname + '.' + field).getValue()
+               });
+       
+               openils.I18N.translationWidget.writeTranslation('create', trans_obj, obj, field);
+       }
        
-    //dojo.declare('openils.I18N.TranslationInput', [dijit.form.DropDownButton], { });
+       openils.I18N.translationWidget.writeTranslation = function (method, trans_obj, obj, field) {
+       
+               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');
+                                               dijit.byId('i18n_new_locale_' + obj.classname + '.' + field).setValue(null);
+                                               dijit.byId('i18n_new_translation_' + obj.classname + '.' + field).setValue(null);
+                                               openils.I18N.translationWidget.renderTranslationPopup(obj, field);
+                                       }
+       
+                               } else {
+                                       //highlighter.editor_pane.red.play();
+                                       if (status_update) status_update( 'Problem saving translation for ' + obj[field]() );
+                               }
+                       },
+               }).send();
+       }
+
 }