r113644 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113643‎ | r113644 | r113645 >
Date:18:17, 12 March 2012
Author:jdlrobson
Status:resolved (Comments)
Tags:
Comment:
add locale global for handling languages and message function

for the benefit of the phonegap app
Modified paths:
  • /trunk/extensions/MobileFrontend/javascripts/application.js (modified) (history)
  • /trunk/extensions/MobileFrontend/templates/ApplicationTemplate.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/javascripts/application.js
@@ -92,6 +92,9 @@
9393 init();
9494 return {
9595 init: init,
 96+ message: function(name) {
 97+ return locale[ name ];
 98+ },
9699 utils: utilities
97100 };
98101
Index: trunk/extensions/MobileFrontend/templates/ApplicationTemplate.php
@@ -61,8 +61,10 @@
6262 //<![CDATA[
6363 var title = "{$this->data['htmlTitle']}";
6464 var scriptPath = "{$this->data['wgScriptPath']}";
65 - var showText = "{$buttonShowText}";
66 - var hideText = "{$buttonHideText}";
 65+ var locale = {
 66+ "expand-section": "{$buttonShowText}",
 67+ "collapse-section": "{$buttonHideText}"
 68+ };
6769 //]]>
6870 </script>
6971 </head>

Follow-up revisions

RevisionCommit summaryAuthorDate
r113645update toggle.js to use message function introduced...jdlrobson18:18, 12 March 2012
r113695follow up to r113644#c32093...jdlrobson00:22, 13 March 2012
r114201MFT r113486, r113488, r113512, r113553, r113640, r113642, r113644, r113645, r...awjrichards22:18, 19 March 2012

Comments

#Comment by Krinkle (talk | contribs)   18:29, 12 March 2012
				var title = "{$this->data['htmlTitle']}";
				var scriptPath = "{$this->data['wgScriptPath']}";
-				var showText = "{$buttonShowText}";
-				var hideText = "{$buttonHideText}";
+				var locale = {
+					"expand-section": "{$buttonShowText}",
+					"collapse-section": "{$buttonHideText}"
+				};
 			</script>

These generic (global!) variables are becoming an issue. More then just a maintainability problem but a practical one as well. The browser has many default variables in the global 'window' object by default. One of the things many browsers put as alias into the window object are unique DOM element IDs.

So if any wiki or skin content would have <span id="locale"> .. </span> or <span id="title"> .. </span>, then this code is broken (some browsers allow overriding of those properties, some don't).

Code like this is asking to get broken.

My advice would be to either drop all front-end development temporarily and port to ResourceLoader asap, or spend more time on this.

#Comment by MaxSem (talk | contribs)   19:07, 12 March 2012

Mobile RL looks problematic for compatibility (many mobile browsers support JS, but choke on its amount in jQuery) and traffic reasons. Any ideas?

#Comment by Krinkle (talk | contribs)   22:59, 13 March 2012

See also bugzilla:31675. Afaik the decision was already made to port MobileFrontend to ResourceLoader, and move the bar of "get javascript" to "browser supports modern javascript" (instead of "browser supports basic javascript").

#Comment by Jdlrobson (talk | contribs)   19:52, 12 March 2012

What are people's thoughts on this -> r113644.diff http://jonrobson.me.uk/tmp/r113644.diff - as a possible solution using data attributes [1]

Note some mobiles do not support JSON which would be the other way to pass data from the php code to the javascript.

[1] http://www.w3.org/TR/html5/elements.html#embedding-custom-non-visible-data-with-the-data-attributes

#Comment by Krinkle (talk | contribs)   21:15, 12 March 2012

There's no need for work-around via DOM attributes. JavaScript objects work just fine and much easier and faster to manipulate.

#Comment by Krinkle (talk | contribs)   20:40, 12 March 2012

JSON parsing from a string is indeed a modern API that isn't reliably available in a cross-browser environment. However it doesn't make sense to pass a JSON string to a browser. Browsers have javascript engines and JSON (JavaScript Object Notation) as a literal rather than a string is (naturally) supported in all javascript engines (even old ones (IE6..) and/or handicapped mobile javascript engines, its in the fundamentals of the language, it's even used in the return value visible in the diff above).

Outputting 'var mwMobileFrontendData = ' . FormatJSON::encode( $frontendData ) . ';' will be fine. Where $frontendData would be something like:

$frotendData = array(
    'messages' => array(
        'expand-section' => ..,
    ),
    'config' => array(
        'pageTitle' => ..
        , 'scriptPath' => ..,
    ),
);

Then from a javascript file you'd have something like:

( function ( mfData ) {

    var mf = {
        msg: function (key) {
            return mfData.messages[key];
        }
    };
    window.mwMobileFrontend = mf;

}( mwMobileFrontendData ) );

Just a short example :)

#Comment by Jdlrobson (talk | contribs)   00:31, 13 March 2012

See follow up in r113695

Status & tagging log