[ppwcode] r6818 committed - Refactor for more multilang output widgets

2 views
Skip to first unread message

ppw...@googlecode.com

unread,
May 20, 2013, 10:36:08 AM5/20/13
to ppwco...@googlegroups.com
Revision: 6818
Author: jand...@gmail.com
Date: Mon May 20 07:35:46 2013
Log: Refactor for more multilang output widgets
http://code.google.com/p/ppwcode/source/detail?r=6818

Added:

/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/_MultiLangBundleParent.js
/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/_MultiLangOutput.js
/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/_MultiLangParent.js
Deleted:

/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/_MultiLangLabelParent.js
Modified:
/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/MultiLangLabel.js

=======================================
--- /dev/null
+++
/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/_MultiLangBundleParent.js
Mon May 20 07:35:46 2013
@@ -0,0 +1,42 @@
+define(["dojo/_base/declare", "./_MultiLangParent", "dojo/_base/kernel"],
+ function(declare, _MultiLangParent) {
+
+ var _MultiLangBundleParent = declare([_MultiLangParent], {
+ // summary:
+ // Optional parent for MultiLangParent. We can set
+ // - nlsParentDirectory: the directory containing the used nls
directory
+ // - bundleName: the name of the i18n file
+ // - lang; the locale, which can change
+ //
+ // All locales must be defined as extraLocale in dojoConfig.
+ // The actual i18n resource must be loaded using the i18n! plugin
syntax.
+ //
+ // If nlsParentDirectory is not set, we see if we have a `mid`
property on the Constructor.
+ // If so, we derive it from that, and cache it.
+
+ // nlsParentDirectory: String?
+ nlsParentDirectory: null,
+
+ // bundleName: String?
+ bundleName: null,
+
+ _getNlsParentDirectoryAttr: function() {
+ if ((!this.nlsParentDirectory) && this.constructor.mid) {
+ this.set("nlsParentDirectory",
_MultiLangBundleParent.dirFromMid(this.constructor.mid));
+ }
+ return this.nlsParentDirectory;
+ }
+
+ });
+
+ _MultiLangBundleParent.dirFromMid = function(mid) {
+ // summary:
+ // Helper function to get the directory from a MID
+ var parts = mid.split("/");
+ parts.pop();
+ return parts.join("/");
+ };
+
+ return _MultiLangBundleParent;
+ }
+);
=======================================
--- /dev/null
+++
/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/_MultiLangOutput.js
Mon May 20 07:35:46 2013
@@ -0,0 +1,123 @@
+define(["dojo/_base/declare", "./_MultiLangParent"],
+ function(declare, _MultiLangParent) {
+
+ return declare([_MultiLangParent], {
+ // summary:
+ // This widget is a superclass for widgets that show
(not-editable) a `value` in an i18n-ed way,
+ // and for which the representation language can change.
+ // `lang` is the locale, which can change. `value` is what is
shown.
+ // Setting anything re-renders.
+ //
+ // If `lang` does not have a meaningful value, we look upwards in
the widget
+ // tree for a value _MultiLangParent, and use its `lang`.
+ // Missing values are rendered as `missing`.
+ //
+ // If bindLang is true (the default), we bind lang on startup to
the lang of a _MultiLangParent,
+ // if there is one.
+ //
+ // All locales must be defined as extraLocale in dojoConfig.
+
+ // missing: string
+ // This string is used when there is no value to show.
+ missing: "?value?",
+
+ bindLang: true,
+ _parentLangHandle: null,
+
+ startup: function() {
+ this.inherited(arguments);
+ if (this.bindLang) {
+ this._bindLang();
+ }
+ this._output();
+ },
+
+ destroy: function() {
+ this.inherited(arguments);
+ if (this._parentLangHandle) {
+ this._parentLangHandle.remove();
+ this._parentLangHandle = null;
+ }
+ },
+
+ set: function(name, value){
+ // summary:
+ // Override and refresh output on value change.
+ // name:
+ // The property to set.
+ // value:
+ // The value to set in the property.
+ this.inherited(arguments);
+ if (this._created) {
+ this._output();
+ }
+ },
+
+ _bindLang: function() {
+ function parent(/*_WidgetBase*/ wb) {
+ if (!wb) {
+ return null
+ }
+ var parentWb = wb.getParent();
+ if (parentWb.isInstanceOf(_MultiLangParent)) {
+ return parentWb;
+ }
+ return parent(parentWb);
+ }
+
+ var self = this;
+ var parentWb = parent(self); // TODO do something with own
+ if (parentWb) {
+ self._parentLangHandle = parentWb.watch("lang",
function(propName, oldValue, newValue) {
+ if (oldValue !== newValue) {
+ self.set("lang", newValue);
+ }
+ });
+ }
+ },
+
+ _setBindLang: function(value) {
+ if (this.bindLang != value) {
+ if (this.bindLang && this._parentLangHandle) {
+ this._parentLangHandle.remove();
+ }
+ this._set("bindLang", value);
+ if (this.bindLang) {
+ this._bindLang();
+ }
+ }
+ },
+
+ _lookUpInWidgetHierarchy: function(/*String*/ propName, /*Function*/
Type) {
+ // summary:
+ // Lookup a meaningful value for `propName` up in the widget
hierarchy,
+ // in instances of `Type`, starting at this.
+ // If no meaningful value is found, undefined is returned
(although null, 0,
+ // false, and empty strings might have been encountered).
+
+ function lookup(/*_WidgetBase*/ wb) {
+ if (!wb) {
+ return undefined;
+ }
+ var result;
+ if (wb.isInstanceOf(Type)) { // this is too
+ result = wb.get(propName);
+ }
+ return result || lookup(wb.getParent());
+ }
+
+ return lookup(this);
+ },
+
+ _output: function(){
+ // summary:
+ // Produce the value-bound output.
+ // tags:
+ // protected
+
+ this._c_ABSTRACT();
+ }
+
+ });
+ }
+);
=======================================
--- /dev/null
+++
/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/_MultiLangParent.js
Mon May 20 07:35:46 2013
@@ -0,0 +1,17 @@
+define(["dojo/_base/declare", "dijit/_WidgetBase", "dojo/_base/kernel"],
+ function(declare, _WidgetBase, kernel) {
+
+ var _MultiLangParent = declare([_WidgetBase], {
+ // summary:
+ // Optional parent for _MultiLangOutput. Instances of
_MultiLangOutput might
+ // bind to the `lang` of instances of this type.
+ //
+ // All locales must be defined as extraLocale in dojoConfig.
+
+ lang: kernel.locale // default language is browser dependent
+
+ });
+
+ return _MultiLangParent;
+ }
+);
=======================================
---
/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/_MultiLangLabelParent.js
Sun May 19 16:04:37 2013
+++ /dev/null
@@ -1,44 +0,0 @@
-define(["dojo/_base/declare", "dijit/_WidgetBase", "dojo/_base/kernel"],
- function(declare, _WidgetBase, kernel) {
-
- var _MultiLangLabelParent = declare([_WidgetBase], {
- // summary:
- // Optional parent for MultiLangParent. We can set
- // - nlsParentDirectory: the directory containing the used nls
directory
- // - bundleName: the name of the i18n file
- // - lang; the locale, which can change
- //
- // All locales must be defined as extraLocale in dojoConfig.
- // The actual i18n resource must be loaded using the i18n! plugin
syntax.
- //
- // If nlsParentDirectory is not set, we see if we have a `mid`
property on the Constructor.
- // If so, we derive it from that, and cache it.
-
- // nlsParentDirectory: String?
- nlsParentDirectory: null,
-
- // bundleName: String?
- bundleName: null,
-
- lang: kernel.locale, // default language is browser dependent
-
- _getNlsParentDirectoryAttr: function() {
- if ((!this.nlsParentDirectory) && this.constructor.mid) {
- this.set("nlsParentDirectory",
_MultiLangLabelParent.dirFromMid(this.constructor.mid));
- }
- return this.nlsParentDirectory;
- }
-
- });
-
- _MultiLangLabelParent.dirFromMid = function(mid) {
- // summary:
- // Helper function to get the directory from a MID
- var parts = mid.split("/");
- parts.pop();
- return parts.join("/");
- };
-
- return _MultiLangLabelParent;
- }
-);
=======================================
--- /javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/MultiLangLabel.js
Sun May 19 16:04:37 2013
+++ /javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/MultiLangLabel.js
Mon May 20 07:35:46 2013
@@ -1,7 +1,7 @@
-define(["dojo/_base/declare", "./_MultiLangLabelParent", "dojo/_base/kernel", "dojo/i18n", "../xml"],
- function(declare, _MultiLangLabelParent, kernel, i18n, xml) {
+define(["dojo/_base/declare", "./_MultiLangOutput", "./_MultiLangBundleParent", "dojo/_base/kernel", "dojo/i18n", "../xml"],
+ function(declare, _MultiLangOutput, _MultiLangBundleParent, kernel,
i18n, xml) {

- return declare([_MultiLangLabelParent], {
+ return declare([_MultiLangOutput, _MultiLangBundleParent], {
// summary:
// Widget that is specially made to represent a i18n (nls) label
in a template,
// when multiple languages must be shown, and the language can
change dynamically.
@@ -12,7 +12,7 @@
// - lang; the locale, which can change
//
// If any of these are not a meaningful value, we look upwards in
the widget
- // tree for a value _MultiLangLabelParent, and use its values.
+ // tree for a value _MultiLangBundleParent, and use its values.
//
// If bindLang is true (the default), we bind lang on startup to
the lang of a parent, if there is one.
//
@@ -24,10 +24,6 @@
//
// Every set re-renders.

- // missing: string
- // This string is used when there is no value to show.
- missing: "?value?",
-
// escapeXml: Boolean
// Default is true.
escapeXml: true,
@@ -35,95 +31,16 @@
// label: String?
label: null,

- bindLang: true,
- _parentLangHandle: null,
-
- startup: function() {
- this.inherited(arguments);
- if (this.bindLang) {
- this._bindLang();
- }
- this._output();
- },
-
- destroy: function() {
- this.inherited(arguments);
- if (this._parentLangHandle) {
- this._parentLangHandle.remove();
- this._parentLangHandle = null;
- }
- },
-
- set: function(name, value){
- // summary:
- // Override and refresh output on value change.
- // name:
- // The property to set.
- // value:
- // The value to set in the property.
- this.inherited(arguments);
- if (this._created) {
- this._output();
- }
- },
-
- _bindLang: function() {
- function parent(/*_WidgetBase*/ wb) {
- if (!wb) {
- return null
- }
- var parentWb = wb.getParent();
- if (parentWb.isInstanceOf(_MultiLangLabelParent)) {
- return parentWb;
- }
- return parent(parentWb);
- }
-
- var self = this;
- var parentWb = parent(self);
- // TODO do something with own
- if (parentWb) {
- self._parentLangHandle = parentWb.watch("lang",
function(propName, oldValue, newValue) {
- if (oldValue !== newValue) {
- self.set("lang", newValue);
- }
- });
- }
- },
-
- _setBindLang: function(value) {
- if (this.bindLang != value) {
- if (this.bindLang && this._parentLangHandle) {
- this._parentLangHandle.remove();
- }
- this._set("bindLang", value);
- if (this.bindLang) {
- this._bindLang();
- }
- }
- },
-
- _output: function(){
+ _output: function() {
// summary:
// Produce the data-bound output, xml-escaped.
// tags:
// protected

- function lookup(/*_WidgetBase*/ wb, /*String*/ propName) {
- if (!wb) {
- return null;
- }
- var result;
- if (wb.isInstanceOf(_MultiLangLabelParent)) { // this is too
- result = wb.get(propName);
- }
- return result || lookup(wb.getParent(), propName);
- }
-
var render = this.missing;
- var nlsParentDir = lookup(this, "nlsParentDirectory");
- var bundle = lookup(this, "bundleName");
- var lang = lookup(this, "lang") || kernel.locale;
+ var nlsParentDir =
this._lookUpInWidgetHierarchy("nlsParentDirectory", _MultiLangBundleParent);
+ var bundle = this._lookUpInWidgetHierarchy("bundleName",
_MultiLangBundleParent);
+ var lang = this._lookUpInWidgetHierarchy("lang",
_MultiLangBundleParent) || kernel.locale;
if (nlsParentDir && bundle && this.label) {
try {
var labels = i18n.getLocalization(nlsParentDir, bundle, lang);
Reply all
Reply to author
Forward
0 new messages