Source: panels/root/themes_item.js

  1. /**
  2. * This module is used to show/hide themes menuItem based on the number of
  3. * current installed themes.
  4. *
  5. * @module ThemesItem
  6. */
  7. define(function(require) {
  8. 'use strict';
  9. var AppsCache = require('modules/apps_cache');
  10. function ThemesItem(element) {
  11. this._enabled = false;
  12. this._element = element;
  13. this._themeCount = 0;
  14. this._boundUpdateThemes = this._updateThemes.bind(this);
  15. this.init();
  16. }
  17. ThemesItem.prototype = {
  18. /**
  19. * Set current status of themesItem
  20. *
  21. * @access public
  22. * @param {Boolean} enabled
  23. * @memberOf ThemesItem
  24. */
  25. set enabled(enabled) {
  26. if (this._enabled === enabled) {
  27. return;
  28. } else {
  29. this._enabled = enabled;
  30. if (this._enabled) {
  31. this._updateThemeSectionVisibility();
  32. }
  33. }
  34. },
  35. /**
  36. * Get current status of themesItem
  37. *
  38. * @access public
  39. * @memberOf ThemesItem
  40. */
  41. get enabled() {
  42. return this._enabled;
  43. },
  44. /**
  45. * Initialization
  46. *
  47. * @access private
  48. * @memberOf ThemesItem
  49. * @return {Promise}
  50. */
  51. init: function() {
  52. var self = this;
  53. AppsCache.addEventListener('install', this._boundUpdateThemes);
  54. AppsCache.addEventListener('uninstall', this._boundUpdateThemes);
  55. return AppsCache.apps().then(function(apps) {
  56. apps.some(function(app) {
  57. if (self._isThemeApp(app)) {
  58. self._themeCount += 1;
  59. }
  60. });
  61. self._updateThemeSectionVisibility();
  62. });
  63. },
  64. /**
  65. * Check whether this app is theme app
  66. *
  67. * @param {Object} app
  68. * @returns {Boolean}
  69. * @memberOf ThemesItem
  70. */
  71. _isThemeApp: function(app) {
  72. var manifest = app.manifest || app.updateManifest;
  73. return manifest.role === 'theme';
  74. },
  75. /**
  76. * We have to update theme count based on incoming evt and
  77. * decide to show/hide or not.
  78. *
  79. * @param {Object} evt
  80. * @memberOf ThemesItem
  81. */
  82. _updateThemes: function(evt) {
  83. var app = evt && evt.application;
  84. var type = evt.type;
  85. if (this._isThemeApp(app)) {
  86. if (type === 'install') {
  87. this._themeCount += 1;
  88. } else if (type === 'uninstall') {
  89. this._themeCount -= 1;
  90. }
  91. this._updateThemeSectionVisibility();
  92. }
  93. },
  94. /**
  95. * Update theme section visibility based on _themeCount
  96. *
  97. * @memberOf ThemesItem
  98. */
  99. _updateThemeSectionVisibility: function() {
  100. this._element.hidden = (this._themeCount < 2);
  101. }
  102. };
  103. return function ctor_themesItem(element) {
  104. return new ThemesItem(element);
  105. };
  106. });