Source: panels/keyboard_add_layouts/core.js

  1. /**
  2. * The module initializes a ListView displaying all installed layouts.
  3. * Implementation details please refer to {@link KeyboardAddLayoutsCore}.
  4. *
  5. * @module keyboard_add_layouts/core
  6. */
  7. define(function(require) {
  8. 'use strict';
  9. var ListView = require('modules/mvvm/list_view');
  10. var _ = navigator.mozL10n.get;
  11. /**
  12. * @alias module:keyboard_add_layouts/core
  13. * @class KeyboardAddLayoutsCore
  14. * @requires module:modules/settings_service
  15. * @requires module:modules/mvvm/list_view
  16. * @param {KeyboardContext} context
  17. The kyboard context providing installed keyboards.
  18. * @param {Function} template
  19. The template function used to render an installed
  20. keyboard.
  21. * @returns {KeyboardAddLayoutsCore}
  22. */
  23. function KeyboardAddLayoutsCore(context, template) {
  24. this._enabled = false;
  25. this._listView = null;
  26. this._keyboardContext = context;
  27. this._keyboardTemplate = template;
  28. }
  29. KeyboardAddLayoutsCore.prototype = {
  30. /**
  31. * The value indicates whether the module is responding. If it is false, the
  32. * UI stops reflecting the updates from the keyboard context.
  33. *
  34. * @access public
  35. * @memberOf KeyboardAddLayoutsCore.prototype
  36. * @type {Boolean}
  37. */
  38. get enabled() {
  39. return this._enabled;
  40. },
  41. set enabled(value) {
  42. this._enabled = value;
  43. if (this._listView) {
  44. this._listView.enabled = this._enabled;
  45. }
  46. // Disable all inner list views
  47. this._keyboardTemplate.listViews.forEach(function(listView) {
  48. listView.enabled = this._enabled;
  49. }.bind(this));
  50. },
  51. /**
  52. * @access private
  53. * @memberOf KeyboardAddLayoutsCore.prototype
  54. * @param {HTMLElement} listViewRoot
  55. * @param {ObservableArray} keyboards
  56. * @param {Function} keyboardTemplate
  57. */
  58. _initInstalledLayoutListView:
  59. function kal_initListView(listViewRoot, keyboards, keyboardTemplate) {
  60. this._listView = ListView(listViewRoot, keyboards, keyboardTemplate);
  61. },
  62. /**
  63. * The handler is invoked when users disable the must-have input type. In
  64. * the handler we navigate to the dialog.
  65. *
  66. * @access private
  67. * @memberOf KeyboardAddLayoutsCore.prototype
  68. * @param {Object} layout
  69. * @param {String} missingType
  70. */
  71. _showEnabledDefaultDialog: function kal_showDialog(layout, missingType) {
  72. require(['modules/dialog_service'], function(DialogService) {
  73. var type = _('keyboardType-' + missingType);
  74. DialogService.alert({
  75. id: 'defaultKeyboardEnabled',
  76. args: {
  77. layoutName: layout.inputManifest.name,
  78. appName: layout.manifest.name
  79. }
  80. }, {
  81. title: {
  82. id: 'mustHaveOneKeyboard',
  83. args: {
  84. type: type
  85. }
  86. }
  87. });
  88. });
  89. },
  90. /**
  91. * @access public
  92. * @memberOf KeyboardAddLayoutsCore.prototype
  93. * @param {Array} elements
  94. * Elements needed by this module.
  95. * @param {HTMLElement} elements.listViewRoot
  96. * The root element for the list view displaying the
  97. * installed keyboards.
  98. */
  99. init: function kal_onInit(elements) {
  100. var that = this;
  101. this._keyboardContext.init(function() {
  102. that._keyboardContext.keyboards(function(keyboards) {
  103. that._initInstalledLayoutListView(
  104. elements.listViewRoot, keyboards, that._keyboardTemplate);
  105. that.enabled = true;
  106. });
  107. });
  108. this._keyboardContext.defaultKeyboardEnabled(
  109. this._showEnabledDefaultDialog);
  110. }
  111. };
  112. return function ctor_kalCore(context, template) {
  113. return new KeyboardAddLayoutsCore(context, template);
  114. };
  115. });