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

Commit

Permalink
Revert "Bug 858383 - [Keyboard] Integrate the new keyboard manager wi…
Browse files Browse the repository at this point in the history
…th switch" for gaia unit test failures

This reverts commit c602ea3.
  • Loading branch information
Ed Morley committed Aug 30, 2013
1 parent 884d0af commit 6a653be
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 342 deletions.
226 changes: 96 additions & 130 deletions apps/keyboard/js/keyboard.js
Expand Up @@ -125,8 +125,6 @@ const defaultInputMethod = {
displaysCandidates: function() { return false; }
};

var inputContext = null;

// The keyboard app can display different layouts for different languages
// We sometimes refer to these different layouts as "keyboards", so this single
// keyboard app can display many different keyboards. The currently displayed
Expand Down Expand Up @@ -236,15 +234,6 @@ const keyboardAlias = {
// XXX: ideally, this should be based on the current language,
const defaultKeyboardNames = ['en'];

const keyboardHashKey = [
'en', 'en-Dvorak', 'es', 'pt-BR', 'pl',
'cz', 'fr', 'de', 'nb', 'sk',
'tr', 'ru', 'sr-Cyrl', 'ar', 'he',
'el',
'zh-Hant-Zhuyin', 'zh-Hans-Pinyin', 'jp-kanji',
'numberLayout'
];

// If we get a focuschange event from mozKeyboard for an element with
// one of these types, we'll just ignore it.
const ignoredFormElementTypes = {
Expand Down Expand Up @@ -276,7 +265,7 @@ var suggestionsEnabled;
var correctionsEnabled;
var clickEnabled;
var vibrationEnabled;
var enabledKeyboardGroups = {};
var enabledKeyboardGroups;
var enabledKeyboardNames;
var isSoundEnabled;

Expand Down Expand Up @@ -324,8 +313,13 @@ function getKeyboardSettings() {
'audio.volume.notification': 7
};

// Add the keyboard group settings to our query, too.
for (var group in keyboardGroups)
settingsQuery['keyboard.layouts.' + group] = false;

// Now query the settings
getSettings(settingsQuery, function gotSettings(values) {

// Copy settings values to the corresponding global variables.
currentKeyboardName = values['keyboard.current'];
suggestionsEnabled = values['keyboard.wordsuggestion'];
Expand All @@ -339,17 +333,11 @@ function getKeyboardSettings() {

handleKeyboardSound();

// set default input method with hash value
if (window.location.hash !== '') {
var hashKey = window.location.hash.substring(1);

if (keyboardHashKey.indexOf(hashKey) !== -1) {
keyboardName = hashKey;
} else {
keyboardName = defaultKeyboardName;
}
} else {
keyboardName = defaultKeyboardName;
// Copy the keyboard group settings too
enabledKeyboardGroups = {};
for (var group in keyboardGroups) {
var settingName = 'keyboard.layouts.' + group;
enabledKeyboardGroups[settingName] = values[settingName];
}

// And create an array of all enabled keyboard layouts from the set
Expand Down Expand Up @@ -402,6 +390,21 @@ function initKeyboard() {
toShowKeyboardFTU = e.settingValue;
});

for (var group in keyboardGroups) {

var settingName = 'keyboard.layouts.' + group;

var createLayoutCallback = function createLayoutCallback(name) {
return function layoutCallback(e) {
enabledKeyboardGroups[name] = e.settingValue;
handleNewKeyboards();
}
};

navigator.mozSettings.addObserver(settingName,
createLayoutCallback(settingName));
}

// Initialize the rendering module
IMERender.init(getUpperCaseValue, isSpecialKeyObj);

Expand Down Expand Up @@ -447,38 +450,32 @@ function initKeyboard() {
attributes: true, attributeFilter: ['class', 'style', 'data-hidden']
});

window.addEventListener('hashchange', function() {
var inputMethodName = window.location.hash.substring(1);
setKeyboardName(inputMethodName);
resetKeyboard();
showKeyboard();
}, false);

// Handle resize events
window.addEventListener('resize', onResize);

// Need to listen to both mozvisibilitychange and oninputcontextchange,
// because we are not sure which will happen first and we will call
// showKeyboard() when mozHidden is false and we got inputContext
window.addEventListener('mozvisibilitychange', function visibilityHandler() {
var inputMethodName = window.location.hash.substring(1);
setKeyboardName(inputMethodName);
// Show or hide the keyboard when we get an focuschange event
// from the keyboard
var focusChangeTimeout = 0;
navigator.mozKeyboard.onfocuschange = function onfocuschange(evt) {
var state = evt.detail;
var type = state.type;

if (!document.mozHidden && inputContext) {
showKeyboard();
} else {
hideKeyboard();
}
});
// Skip the <select> element and inputs with type of date/time,
// handled in system app for now
if (!type || type in ignoredFormElementTypes)
return;

window.navigator.mozInputMethod.oninputcontextchange = function() {
inputContext = navigator.mozInputMethod.inputcontext;
if (!document.mozHidden && inputContext) {
showKeyboard();
// We can get multiple focuschange events in rapid succession
// so wait a bit before responding to see if we get another.
clearTimeout(focusChangeTimeout);
if (type === 'blur') {
focusChangeTimeout = setTimeout(function focusChangeTimeout() {
hideKeyboard();
}, FOCUS_CHANGE_DELAY);
} else {
hideKeyboard();
showKeyboard(state);
}
};

// Handle resize events
window.addEventListener('resize', onResize);
}

function handleKeyboardSound() {
Expand Down Expand Up @@ -694,7 +691,7 @@ function modifyLayout(keyboardName) {
}

// switch languages button
if (!layout['hidesSwitchKey']) {
if (enabledKeyboardNames.length > 1 && !layout['hidesSwitchKey']) {
space.ratio -= 1.5;
row.splice(c, 0, {
value: '&#x1f310;',
Expand Down Expand Up @@ -844,10 +841,8 @@ function renderKeyboard(keyboardName) {
var candidatePanel = document.getElementById('keyboard-candidate-panel');
var candidatePanelHeight = (candidatePanel) ?
candidatePanel.scrollHeight : 0;

var url = document.location.href + '#keyboard-test=' +
(IMERender.ime.scrollHeight - candidatePanelHeight);
window.open(url);
document.location.hash = 'show=' +
(IMERender.ime.scrollHeight - candidatePanelHeight);

redrawTimeout = window.setTimeout(drawKeyboard,
CANDIDATE_PANEL_SWITCH_TIMEOUT);
Expand Down Expand Up @@ -912,9 +907,11 @@ function setLayoutPage(newpage) {
// Inform about a change in the displayed application via mutation observer
// http://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/
function updateTargetWindowHeight(hide) {
var url = document.location.href +
'#keyboard-test=' + IMERender.ime.scrollHeight;
window.open(url);
if (IMERender.ime.dataset.hidden || hide) {
document.location.hash = 'hide';
} else {
document.location.hash = 'show=' + IMERender.ime.scrollHeight;
}
}

// Sends a delete code to remove last character
Expand Down Expand Up @@ -994,7 +991,7 @@ function showAlternatives(key) {

// Handle languages alternatives
if (keyObj.keyCode === SWITCH_KEYBOARD) {
showIMEList();
showKeyboardLayoutMenu(key);
return;
}

Expand Down Expand Up @@ -1300,6 +1297,7 @@ function startPress(target, coords, touchId) {
}
}


function inMenuLockedArea(lockedArea, coords) {
return (lockedArea &&
coords.pageY >= lockedArea.top &&
Expand Down Expand Up @@ -1463,7 +1461,7 @@ function endPress(target, coords, touchId) {
// If the user selected a new keyboard layout or quickly tapped the
// switch layouts button then switch to a new keyboard layout
if (target.dataset.keyboard || !wasShowingKeyboardLayoutMenu)
switchToNextIME();
switchKeyboard(target);
break;

// Expand / shrink the candidate panel
Expand Down Expand Up @@ -1593,24 +1591,13 @@ function switchKeyboard(target) {
// In practice, just about everything uses the latin input method now
// so this only occurs when the users switches from Hebrew or Arabic
// to a latin or cyrillic alphabet. XXX: See Bug 888076
navigator.mozInputMethod.mgmt.removeFocus();
navigator.mozKeyboard.removeFocus();
}
}

renderKeyboard(keyboardName); // And display it.
}

function switchToNextIME() {
var mgmt = navigator.mozInputMethod.mgmt;
mgmt.next();
}


function showIMEList() {
var mgmt = navigator.mozInputMethod.mgmt;
mgmt.showAll();
}

// Turn to default values
function resetKeyboard() {
// Don't call setLayoutPage because renderKeyboard() should be invoked
Expand All @@ -1622,59 +1609,59 @@ function resetKeyboard() {
isUpperCaseLocked = false;
}

// This is a wrapper around inputContext.sendKey()
// This is a wrapper around mozKeyboard.sendKey()
// We use it in the defaultInputMethod and in the interface object
// we pass to real input methods
function sendKey(keyCode) {
switch (keyCode) {
case KeyEvent.DOM_VK_BACK_SPACE:
case KeyEvent.DOM_VK_RETURN:
if (inputContext) {
inputContext.sendKey(keyCode, 0, 0);
}
window.navigator.mozKeyboard.sendKey(keyCode, 0);
break;

default:
if (inputContext) {
inputContext.sendKey(0, keyCode, 0);
}
window.navigator.mozKeyboard.sendKey(0, keyCode);
break;
}
}

function replaceSurroundingText(text, offset, length) {
if (inputContext) {
inputContext.replaceSurroundingText(text, offset, length);
} else {
console.warn('no inputContext for replaceSurroudingText');
}
}

// Set up the keyboard and its input method.
// This is called when we get an event from mozKeyboard.
// The state argument is the data passed with that event, and includes
// the input field type, its inputmode, its content, and the cursor position.
function showKeyboard(state) {
// If no keyboard has been selected yet, choose the first enabled one.
// This will also set the inputMethod
if (!keyboardName) {
setKeyboardName(defaultKeyboardName);
var newKeyboardName = currentKeyboardName;
// If the keyboard is not initialized or the layout has changed,
// set the new keyboard
if (keyboardName !== currentKeyboardName) {
// Make sure that currentKeyboardName is enabled. If not, use
// the first enabled keyboard as the default.
if (enabledKeyboardNames.indexOf(currentKeyboardName) == -1) {
// Update the keyboard.current setting with the first enabled keyboard
navigator.mozSettings.createLock().set({
'keyboard.current': enabledKeyboardNames[0]
});
newKeyboardName = enabledKeyboardNames[0];
}

// Now initialize that keyboard
setKeyboardName(newKeyboardName);
}

inputContext = navigator.mozInputMethod.inputcontext;
IMERender.showIME();

if (inputContext) {
currentInputMode = inputContext.inputMode;
currentInputType = mapInputType(inputContext.inputType);
} else {
console.error('Cannot get inputContext');
currentInputMode = '';
currentInputType = mapInputType('text');
}
currentInputMode = state.inputmode;
currentInputType = mapInputType(state.type);

resetKeyboard();

if (inputMethod.activate) {
inputMethod.activate(Keyboards[keyboardName].autoCorrectLanguage, state, {
suggest: suggestionsEnabled,
correct: correctionsEnabled
});
}

if (toShowKeyboardFTU) {
var dialog = document.getElementById('confirm-dialog');
dialog.hidden = false;
Expand All @@ -1685,34 +1672,9 @@ function showKeyboard(state) {
});
}

var state = {
type: inputContext.inputType,
inputmode: inputContext.inputMode,
selectionStart: inputContext.selectionStart,
selectionEnd: inputContext.selectionEnd,
value: ''
};

function doShowKeyboard() {
if (inputMethod.activate) {
inputMethod.activate(Keyboards[keyboardName].autoCorrectLanguage,
state, {
suggest: suggestionsEnabled,
correct: correctionsEnabled
});
}

// render the keyboard after activation, which will determine the state
// of uppercase/suggestion, etc.
renderKeyboard(keyboardName);
}

inputContext.getText().then(function gotText(value) {
state.value = value;
doShowKeyboard();
}, function failedToGetText() {
doShowKeyboard();
});
// render the keyboard after activation, which will determine the state
// of uppercase/suggestion, etc.
renderKeyboard(keyboardName);
}

// Hide keyboard
Expand Down Expand Up @@ -1787,10 +1749,14 @@ function loadIMEngine(name) {
},
setLayoutPage: setLayoutPage,
setUpperCase: setUpperCase,
resetUpperCase: resetUpperCase,
replaceSurroundingText: replaceSurroundingText
resetUpperCase: resetUpperCase
};

if (typeof navigator.mozKeyboard.replaceSurroundingText === 'function') {
glue.replaceSurroundingText =
navigator.mozKeyboard.replaceSurroundingText.bind(navigator.mozKeyboard);
}

script.addEventListener('load', function IMEngineLoaded() {
var engine = InputMethods[imEngine];
engine.init(glue);
Expand Down
2 changes: 1 addition & 1 deletion apps/keyboard/js/layout.js
Expand Up @@ -285,7 +285,7 @@ const Keyboards = {
]
}
},
'pt-BR': {
pt_BR: {
label: 'Portuguese',
menuLabel: 'Português',
imEngine: 'latin',
Expand Down

0 comments on commit 6a653be

Please sign in to comment.