/**
* Allows developers to request a keyboard be made available to the user.
*
* @name Lens.Keyboards
* @namespace
*/
define(["lib/jquery", "lib/underscore", "./keyboards/onscreen_keyboard",
"common/util/log", "./keyboards/phone_keyboard", "common/util/check"],
function($, _, OnscreenKeyboard,
Log, PhoneKeyboard, check) {
"use strict";
function phoneKB() {
var hashes = window.location.href.slice(window.location.href.indexOf("?") + 1).split("&");
for ( var i = 0; i < hashes.length; i++) {
var hash = hashes[i].split("=");
if((hash[0] === "phone") && (hash[1] === "true")) {
return true;
}
}
return false;
}
function availableKeyboards() {
if(phoneKB()) {
return [Keyboards.PHONE, Keyboards.ON_SCREEN];
}
return [Keyboards.ON_SCREEN];
}
var Keyboards = /** @lends Lens.Keyboards */ {
/**
* An onscreen keyboard. Always available.
*/
ON_SCREEN: 1,
/**
* The keyboard from a phone connected to LumuinAR.
*/
PHONE: 2,
/**
* Requests a keyboard. If a phone is connected to LuminAR, uses that
* phone's keyboard. Else, brings up an onscreen keyboard. There are
* two ways of listening to keyboard events: first, they are fired
* on the focussed DOM element; second, you can bind handlers
* directly using the {@link AbstractKeyboard} returned by this
* method.
*
* Lens' keyboards implements a dialect of the standard keyboard event
* specification. They fire <tt>lens:keypress</tt> events, not keyup and
* keydown events. In addition, these keypress events fired by Lens have
* all of the standard properties used to access the key: key, char,
* keyCode, charCode, and which. All of these properties have the same
* value, which is the printable character corresponding to the key
* press (taking into account whether the shift key is pressed), or
* a key code corresponding to a special key. Keypress
* events are fired only for printable keys; they are not fired for
* presses of the shift key, for example.
*
* If a Lens keyboard receives a keypress while a text input or textarea
* has focus, the pressed key will be added to the value of the input.
* So, to allow a user to type into a text input box, focus the element
* and request a keyboard.
*
* For a pre-built text input modal dialog, see
* {@link Lens.Components.Modal.textEntry}.
*
* @param {String | Element | jQuery} position
* Where to put the onscreen keyboard if one is used. Either
* a CSS selector, a DOM element, or a jQuery object.
*
* @return {AbstractKeyboard}
* A concrete subclass of {@link AbstractKeyboard}.
*/
request: function(position) {
check(position, check.Match.Elementish);
if(_.contains(availableKeyboards(), Keyboards.PHONE)) {
return PhoneKeyboard();
}
else {
return OnscreenKeyboard(position);
}
},
/**
* Returns an array of available keyboard types:
* {@link Keyboards.ON_SCREEN} and/or {@link Keyboards.PHONE}.
* @method
*/
available: availableKeyboards
};
Lens._addMember(Keyboards, "Keyboards");
return Keyboards;
});