Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #18500 from zbraniecki/999138-clock-mozl10n-api-cl…
Browse files Browse the repository at this point in the history
…eanup

Bug 999138 - Clean up bootstrap mozL10n API use in Clock. r=mcav
  • Loading branch information
zbraniecki committed May 19, 2014
2 parents 688cf58 + 38eb37c commit c3362b4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 32 deletions.
15 changes: 0 additions & 15 deletions apps/clock/js/app.js
Expand Up @@ -3,7 +3,6 @@ define(function(require) {

var Tabs = require('tabs');
var View = require('view');
var mozL10n = require('l10n');
var PerformanceTestingHelper = require('shared/js/performance_testing_helper');
var rAF = mozRequestAnimationFrame || requestAnimationFrame;
/**
Expand All @@ -17,13 +16,8 @@ var App = {
this.tabs = new Tabs(document.getElementById('clock-tabs'));

window.addEventListener('hashchange', this);
window.addEventListener('localized', this);
window.addEventListener('visibilitychange', this);

// we wait for the app to be l10n ready before initializing, so call
// the onlocalized once at startup
this.onlocalized();

this.visible = !document.hidden;
this.panels = Array.prototype.map.call(
document.querySelectorAll('[data-panel-id]'),
Expand Down Expand Up @@ -126,15 +120,6 @@ var App = {
this.navigate({ hash: location.hash });
},

/**
* Reset the global localization params on the html element. Called when
* the language changes, and once on application startup.
*/
onlocalized: function(event) {
document.documentElement.lang = mozL10n.language.code;
document.documentElement.dir = mozL10n.language.direction;
},

/**
* Whenever the application gains/loses focus, inform the current panel of
* its visibility loss.
Expand Down
3 changes: 1 addition & 2 deletions apps/clock/js/panels/alarm_edit/main.js
Expand Up @@ -88,8 +88,7 @@ var AlarmEdit = function() {
// When the language changes, the value of 'weekStartsOnMonday'
// might change. Since that's more than a simple text string, we
// can't just use mozL10n.translate().
window.addEventListener('localized', this.updateL10n.bind(this));
this.updateL10n();
mozL10n.ready(this.updateL10n.bind(this));

this.buttons.close.addEventListener('click', handleDomEvent);
this.buttons.done.addEventListener('click', handleDomEvent);
Expand Down
2 changes: 1 addition & 1 deletion apps/clock/js/ring_view.js
Expand Up @@ -28,7 +28,7 @@ function RingView() {
PostMessageProxy.receive('ringView', this);

if (window.opener) {
mozL10n.ready(() => {
mozL10n.once(() => {
ChildWindowManager.fireReady();
});
}
Expand Down
2 changes: 1 addition & 1 deletion apps/clock/js/startup.js
Expand Up @@ -4,7 +4,7 @@ define('startup_init', function(require) {

var App = require('app');
var mozL10n = require('l10n');
mozL10n.ready(App.init.bind(App));
mozL10n.once(App.init.bind(App));
});

require(['require_config'], function() {
Expand Down
36 changes: 27 additions & 9 deletions apps/clock/test/unit/mocks/mock_moz_l10n.js
Expand Up @@ -10,26 +10,44 @@ define(function() {
}
};

var testDefaults = {};
var testDefaults = {'en-US': {}};
var currentLanguage = 'en-US';

// callbacks to be fired onready
var onreadycbs = [];

return {
/** For unit testing: */
setForTest: function(key, value) {
testDefaults[key] = value;
},
get: function get(key, params) {
if (key in testDefaults) {
return testDefaults[key];
var res = testDefaults[currentLanguage];

if (key in res) {
return res[key];
}
if (params) {
return key + JSON.stringify(params);
}
return key;
},
ready: function(cb) { setTimeout(cb); },
ready: function(cb) {
setTimeout(cb);
onreadycbs.push(cb);
},
translate: function() {},
localize: function() {},
DateTimeFormat: DateTimeFormat
DateTimeFormat: DateTimeFormat,

// for unit tests
setResources: function(lang, res) {
testDefaults[lang] = res;
},
language: {
set code(lang) {
currentLanguage = lang;
for (var cb of onreadycbs) {
cb();
}
}
},
};

});
13 changes: 9 additions & 4 deletions apps/clock/test/unit/panels/alarm_edit/alarm_edit_test.js
Expand Up @@ -31,6 +31,13 @@ suite('AlarmEditView', function() {
alarmListPanel = new AlarmListPanel(document.createElement('div'));
AlarmManager = _AlarmManager;
mozL10n = l10n;
mozL10n.setResources('en-US', {
'weekStartsOnMonday': '0',
});
mozL10n.setResources('fr', {
'weekStartsOnMonday': '1',
});

done();
});
});
Expand Down Expand Up @@ -190,8 +197,7 @@ suite('AlarmEditView', function() {
// Sunday gets moved to the end.
parent.appendChild(sunday);

mozL10n.setForTest('weekStartsOnMonday', '0');
window.dispatchEvent(new Event('localized'));
mozL10n.language.code = 'en-US';

assert.ok(!sunday.previousSibling, 'Sunday should be first (prev)');
assert.ok(sunday.nextSibling, 'Sunday should be first (next)');
Expand All @@ -203,8 +209,7 @@ suite('AlarmEditView', function() {
// Sunday goes first.
parent.insertBefore(sunday, parent.firstChild);

mozL10n.setForTest('weekStartsOnMonday', '1');
window.dispatchEvent(new Event('localized'));
mozL10n.language.code = 'fr';

assert.ok(sunday.previousSibling, 'Sunday should be last (prev)');
assert.ok(!sunday.nextSibling, 'Sunday should be last (next)');
Expand Down

0 comments on commit c3362b4

Please sign in to comment.