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 #25209 from mnjul/bug_1074613_normalize_alt_char_a…
Browse files Browse the repository at this point in the history
…nd_overwrite_chars

Bug 1072191, 1074613 - Layout normalization follow-ups. r=timdream
  • Loading branch information
Min-Zhong "John" Lu committed Oct 20, 2014
2 parents 58823c3 + 42541c0 commit dae2dc4
Show file tree
Hide file tree
Showing 6 changed files with 514 additions and 614 deletions.
113 changes: 4 additions & 109 deletions apps/keyboard/js/keyboard/layout_loader.js
Expand Up @@ -145,15 +145,13 @@ Keyboards.telLayout = {

var LayoutLoader = function(app) {
this.app = app;
this._normalizer = null;
};

LayoutLoader.prototype.SOURCE_DIR = './js/layouts/';

LayoutLoader.prototype.start = function() {
this._initializedLayouts = {};
this._layoutsPromises = {};
this._normalizer = new LayoutNormalizer();
this.initLayouts();
};

Expand All @@ -167,8 +165,10 @@ LayoutLoader.prototype.initLayouts = function() {
if (this._initializedLayouts[layoutName]) {
console.warn('LayoutLoader: ' + layoutName + ' is overwritten.');
}
this._initializedLayouts[layoutName] = Keyboards[layoutName];
this._normalizeLayout(layoutName);

var layoutNormalizer = new LayoutNormalizer(Keyboards[layoutName]);
layoutNormalizer.normalize();
this._initializedLayouts[layoutName] = layoutNormalizer.normalizedLayout;

// Create a promise so that these panels can be loaded async
// even if they are not loaded with file of their name.
Expand All @@ -179,111 +179,6 @@ LayoutLoader.prototype.initLayouts = function() {
}
};

// In order to keep the commit log sane, some amendments of the
// layout JS structure are fix here in runtime instead of hardcoded.
// TODO: normalize the layout files and maybe remove this function.
LayoutLoader.prototype._normalizeLayout = function(layoutName) {
var layout = this.getLayout(layoutName);

var pages;
if ('pages' in layout) {
pages = layout.pages;
} else {
pages = layout.pages = [];
}

if (!pages[0]) {
pages[0] = {};

// These are properties of the basic page that previously put in the layout
// itself. We should move them to the page object and remove them from
// the layout object.
['alt', 'keys', 'upperCase', 'width', 'keyClassName',
'typeInsensitive', 'textLayoutOverwrite',
'needsCommaKey', 'secondLayout', 'specificCssRule'
].forEach(function(prop) {
if (layout[prop]) {
pages[0][prop] = layout[prop];
delete layout[prop];
}
});
}

// Normalize key properties such that other modules can deal with them easily
// Also, go through each pages and inspect it's "alt" property;
// we want to normalize our existing mixed notations into arrays.
pages.forEach(function(page) {
this._normalizer.normalizePageKeys(page, layout);

// XXX: move alt char normalization to the normalizer
var alt = page.alt = page.alt || {};
var upperCase = page.upperCase = page.upperCase || {};
var altKeys = Object.keys(alt);
altKeys.forEach(function(key) {
var alternatives = alt[key];

// Split alternatives
// If the alternatives are delimited by spaces, it means that one or more
// of them is more than a single character long.
if (!Array.isArray(alternatives)) {
if (alternatives.indexOf(' ') !== -1) {
alternatives = alternatives.split(' ');

// If there is just a single multi-character alternative, it will have
// trailing whitespace which we have to discard here.
if (alternatives.length === 2 && alternatives[1] === '') {
alternatives.pop();
}
} else {
// No spaces, so all of the alternatives are single characters
alternatives = alternatives.split('');
}
}

alt[key] = alternatives;

var upperCaseKey = upperCase[key] || key.toUpperCase();
if (!alt[upperCaseKey]) {
var needDifferentUpperCaseLockedAlternatives = false;
// Creating an array for upper case too.
// XXX: The original code does not respect page.upperCase here.
alt[upperCaseKey] = alternatives.map(function(key) {
if (key.length === 1) {
return key.toUpperCase();
}

// The 'l·l' key in the Catalan layout needs to be
// 'L·l' in upper case mode and 'L·L' in upper case locked mode.
// (see http://bugzil.la/896363#c19)
// If that happens we will create a different array for
// upper case locked mode.

// Last chance for figuring out if we need a different list of
// alternatives; if key.substr(1) has no upper case form,
// (e.g. R$ key) we should be able to skip this.
needDifferentUpperCaseLockedAlternatives =
needDifferentUpperCaseLockedAlternatives ||
(key.substr(1).toUpperCase() !== key.substr(1));

// We only capitalize the first character of the key in
// the normalization here.
return key[0].toUpperCase() + key.substr(1);
});

// If we really need an special upper case locked alternatives,
// do it here and attach that as a property of the
// alt[upperCaseKey] array/object. Noted that this property of the array
// can't be represented in JSON so it's not visible in JSON.stringify().
if (needDifferentUpperCaseLockedAlternatives) {
alt[upperCaseKey].upperCaseLocked = alternatives.map(function(key) {
return key.toUpperCase();
});
}
}
}, this);
}, this);
};

LayoutLoader.prototype.getLayout = function(layoutName) {
return this._initializedLayouts[layoutName];
};
Expand Down
37 changes: 14 additions & 23 deletions apps/keyboard/js/keyboard/layout_manager.js
Expand Up @@ -246,7 +246,6 @@ LayoutManager.prototype._updateCurrentPage = function() {
var imeSwitchKey = {
value: '🌐', // U+1F310 GLOBE WITH MERIDIANS
uppercaseValue: '🌐',
ratio: 1,
keyCode: this.KEYCODE_SWITCH_KEYBOARD,
className: 'switch-key',
isSpecialKey: true
Expand Down Expand Up @@ -287,7 +286,6 @@ LayoutManager.prototype._updateCurrentPage = function() {
if (!page.typeInsensitive) {
var periodKey = {
value: '.',
ratio: 1,
keyCode: 46,
keyCodeUpper: 46,
lowercaseValue: '.',
Expand All @@ -299,6 +297,7 @@ LayoutManager.prototype._updateCurrentPage = function() {
}



var modifyType = 'default';
// We have different rules to handle the default layout page and
// symbol/alternate page.
Expand Down Expand Up @@ -333,7 +332,6 @@ LayoutManager.prototype._updateCurrentPage = function() {
// Add '/' key when we are at the default page
spaceKeyRow.splice(spaceKeyCount, 0, {
value: '/',
ratio: 1,
keyCode: 47,
keyCodeUpper: 47,
lowercaseValue: '/',
Expand All @@ -352,7 +350,6 @@ LayoutManager.prototype._updateCurrentPage = function() {
// Add '@' key when we are at the default page
spaceKeyRow.splice(spaceKeyCount, 0, {
value: '@',
ratio: 1,
keyCode: 64,
keyCodeUpper: 64,
lowercaseValue: '@',
Expand Down Expand Up @@ -381,22 +378,20 @@ LayoutManager.prototype._updateCurrentPage = function() {
// set explicitly.
if (overwrites[','] !== false &&
(!needsSwitchingKey || page.needsCommaKey)) {
var commaKey = {
value: ',',
ratio: 1,
keyCode: 44,
keyCodeUpper: 44,
lowercaseValue: ',',
uppercaseValue: ',',
isSpecialKey: false
};

var commaKey;

if (overwrites[',']) {
commaKey.value = overwrites[','];
commaKey.keyCode = overwrites[','].charCodeAt(0);
commaKey.keyCodeUpper = overwrites[','].charCodeAt(0);
commaKey.lowercaseValue = overwrites[','];
commaKey.uppercaseValue = overwrites[','];
commaKey = overwrites[','];
} else {
commaKey = {
value: ',',
keyCode: 44,
keyCodeUpper: 44,
lowercaseValue: ',',
uppercaseValue: ',',
isSpecialKey: false
};
}

spaceKeyObject.ratio -= 1;
Expand All @@ -407,11 +402,7 @@ LayoutManager.prototype._updateCurrentPage = function() {
// Only add peroid key if we are asked to.
if (overwrites['.'] !== false) {
if (overwrites['.']) {
periodKey.value = overwrites['.'];
periodKey.keyCode = overwrites['.'].charCodeAt(0);
periodKey.keyCodeUpper = overwrites['.'].charCodeAt(0);
periodKey.lowercaseValue = overwrites['.'];
periodKey.uppercaseValue = overwrites['.'];
periodKey = overwrites['.'];
}

spaceKeyObject.ratio -= 1;
Expand Down

0 comments on commit dae2dc4

Please sign in to comment.