Bez popisu

software-keyboard.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Copyright 2013-2015 ASIAL CORPORATION
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import util from './util';
  14. import MicroEvent from './microevent';
  15. var softwareKeyboard = new MicroEvent();
  16. softwareKeyboard._visible = false;
  17. var onShow = function onShow() {
  18. softwareKeyboard._visible = true;
  19. softwareKeyboard.emit('show');
  20. };
  21. var onHide = function onHide() {
  22. softwareKeyboard._visible = false;
  23. softwareKeyboard.emit('hide');
  24. };
  25. var bindEvents = function bindEvents() {
  26. if (typeof Keyboard !== 'undefined') {
  27. // https://github.com/martinmose/cordova-keyboard/blob/95f3da3a38d8f8e1fa41fbf40145352c13535a00/README.md
  28. Keyboard.onshow = onShow;
  29. Keyboard.onhide = onHide;
  30. softwareKeyboard.emit('init', { visible: Keyboard.isVisible });
  31. return true;
  32. } else if (typeof cordova.plugins !== 'undefined' && typeof cordova.plugins.Keyboard !== 'undefined') {
  33. // https://github.com/driftyco/ionic-plugins-keyboard/blob/ca27ecf/README.md
  34. window.addEventListener('native.keyboardshow', onShow);
  35. window.addEventListener('native.keyboardhide', onHide);
  36. softwareKeyboard.emit('init', { visible: cordova.plugins.Keyboard.isVisible });
  37. return true;
  38. }
  39. return false;
  40. };
  41. var noPluginError = function noPluginError() {
  42. util.warn('ons-keyboard: Cordova Keyboard plugin is not present.');
  43. };
  44. document.addEventListener('deviceready', function () {
  45. if (!bindEvents()) {
  46. if (document.querySelector('[ons-keyboard-active]') || document.querySelector('[ons-keyboard-inactive]')) {
  47. noPluginError();
  48. }
  49. softwareKeyboard.on = noPluginError;
  50. }
  51. });
  52. export default softwareKeyboard;