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