Source: components/settings.js

  1. define('application/components/settings', [
  2. 'rofl/widgets/component',
  3. 'rofl/widgets/label',
  4. 'rofl/widgets/container',
  5. 'rofl/lib/l10n',
  6. 'antie/runtimecontext',
  7. 'rofl/lib/utils',
  8. 'rofl/events/keyevent',
  9. 'rofl/analytics/web/google',
  10. 'rofl/widgets/horizontallist',
  11. 'application/widgets/clock',
  12. 'application/widgets/settings/button',
  13. 'antie/storageprovider',
  14. 'application/managers/feature',
  15. 'application/managers/session'
  16. ], function (
  17. Component,
  18. Label,
  19. Container,
  20. L10N,
  21. RuntimeContext,
  22. Utils,
  23. KeyEvent,
  24. GoogleAnalytics,
  25. HorizontalList,
  26. Clock,
  27. Button,
  28. StorageProvider,
  29. FeatureManager,
  30. SessionManager
  31. ) {
  32. 'use strict';
  33. var l10n = L10N.getInstance(),
  34. application = RuntimeContext.getCurrentApplication(),
  35. Settings,
  36. GA = GoogleAnalytics.getInstance(),
  37. device = RuntimeContext.getDevice(),
  38. sessionManager = SessionManager.getInstance(),
  39. version = application.getConfiguration().version,
  40. storage;
  41. Settings = Component.extend({
  42. /**
  43. * Initialises the component.
  44. */
  45. init: function init () {
  46. init.base.call(this, 'settings');
  47. storage = device.getStorage(StorageProvider.STORAGE_TYPE_SESSION, 'storage');
  48. this._build();
  49. this._parentalSetting = false;
  50. this._subtitlesSetting = false;
  51. this._lastFocusedWidget = null;
  52. this._createVersionLabel();
  53. this._getParentalSetting();
  54. this._getSubtitlesSetting();
  55. this._onKeyDownBound = Utils.bind(this._onKeyDown, this);
  56. this._onSelectBound = Utils.bind(this._onSelect, this);
  57. },
  58. /**
  59. * Creates the version label.
  60. *
  61. * @private
  62. */
  63. _createVersionLabel: function () {
  64. var versionText = l10n.get('menu.version') + ' ' + version,
  65. versionLabel = new Label({ text: versionText, classNames: ['version'] });
  66. this.appendChildWidget(versionLabel);
  67. },
  68. /**
  69. * Builds the component.
  70. *
  71. * @private
  72. */
  73. _build: function () {
  74. this._buildBackButton();
  75. this._buildGradient();
  76. this._buildTitle();
  77. this._buildClock();
  78. this._buildSettingsButtons();
  79. },
  80. /**
  81. * Builds the top bar list.
  82. *
  83. * @private
  84. */
  85. _buildBackButton: function () {
  86. var topBar = this._topBar = new HorizontalList(),
  87. branding = this._branding = new Container();
  88. branding.addClass('page-branding');
  89. topBar.addClass('topbar');
  90. topBar.appendChildWidget(branding);
  91. this.appendChildWidget(topBar);
  92. },
  93. /**
  94. * Builds the top bar list.
  95. *
  96. * @private
  97. */
  98. _buildGradient: function () {
  99. var gradient = this._gradient = new Container();
  100. gradient.addClass('gradient');
  101. this._topBar.appendChildWidget(gradient);
  102. },
  103. /**
  104. * Builds the title.
  105. *
  106. * @private
  107. */
  108. _buildTitle: function () {
  109. var title = this._title = new Label({ text: l10n.get('settings.title'), classNames: ['top-title'] });
  110. this._branding.appendChildWidget(title);
  111. },
  112. /**
  113. * Builds the clock.
  114. *
  115. * @private
  116. */
  117. _buildClock: function () {
  118. var clock = this._clock = new Clock();
  119. clock.addClass('header-clock');
  120. this._topBar.appendChildWidget(clock);
  121. },
  122. /**
  123. * Builds settings buttons.
  124. *
  125. * @private
  126. */
  127. _buildSettingsButtons: function () {
  128. var buttonsList = this._buttonsList = new HorizontalList('settings-actions'),
  129. buttonChildLock = this._buttonChildLock = new Button(l10n.get('settings.buttons.child_lock'), 'child-lock'),
  130. buttonSubtitles = this._buttonSubtitles = new Button(l10n.get('settings.buttons.subtitles'), 'subtitles'),
  131. buttonLogout = new Button(l10n.get('settings.buttons.logout'), 'logout');
  132. buttonChildLock.getLabelIcon().addClass('icon-lock');
  133. buttonSubtitles.getLabelIcon().addClass('icon-subtitles');
  134. buttonLogout.getLabelIcon().addClass('icon-logout');
  135. buttonChildLock.setOnOffValue(this._getParentalSetting());
  136. buttonSubtitles.setOnOffValue(this._getSubtitlesSetting());
  137. if (FeatureManager.getInstance().isParentalEnabled()) {
  138. buttonsList.appendChildWidget(buttonChildLock);
  139. }
  140. if (FeatureManager.getInstance().isSubtitlesEnabled()) {
  141. buttonsList.appendChildWidget(buttonSubtitles);
  142. }
  143. buttonsList.appendChildWidget(buttonLogout);
  144. this.appendChildWidget(buttonsList);
  145. },
  146. /**
  147. * Handle KeyEvent.
  148. *
  149. * @param {KeyEvent} e - KeyEvent instance.
  150. * @private
  151. */
  152. _onKeyDown: function (e) {
  153. switch (e.keyCode) {
  154. case KeyEvent.VK_LEFT:
  155. e.stopPropagation();
  156. application.focusMenu('main');
  157. break;
  158. case KeyEvent.VK_RIGHT:
  159. e.stopPropagation();
  160. break;
  161. case KeyEvent.VK_BACK:
  162. this._onClose();
  163. break;
  164. }
  165. },
  166. /**
  167. * Stores settings value to storage.
  168. *
  169. * @param {boolean} value - Value.
  170. * @private
  171. */
  172. _storeSubtitlesSetting: function (value) {
  173. storage.setItem('subtitles', value);
  174. this._subtitlesSetting = value;
  175. this._setEventListeners();
  176. this._buttonSubtitles.setOnOffValue(value);
  177. },
  178. /**
  179. * Returns subtitles setting.
  180. *
  181. * @returns {boolean|*} Subtitles setting.
  182. * @private
  183. */
  184. _getSubtitlesSetting: function () {
  185. this._subtitlesSetting = storage.getItem('subtitles');
  186. return this._subtitlesSetting;
  187. },
  188. /**
  189. * Stores settings value to storage.
  190. *
  191. * @param {boolean} value - Value.
  192. * @private
  193. */
  194. _storeParentalSetting: function (value) {
  195. storage.setItem('parental', value);
  196. this._parentalSetting = value;
  197. if (value) {
  198. sessionManager.setUserPin(null);
  199. }
  200. this._setEventListeners();
  201. this._buttonChildLock.setOnOffValue(value);
  202. },
  203. /**
  204. * Returns childlock setting.
  205. *
  206. * @returns {boolean|*} Childlock setting.
  207. * @private
  208. */
  209. _getParentalSetting: function () {
  210. this._parentalSetting = storage.getItem('parental');
  211. return this._parentalSetting;
  212. },
  213. /**
  214. * Callback when modal closes.
  215. *
  216. * @private
  217. */
  218. _closeModalCallback: function () {
  219. this._setEventListeners();
  220. this._lastFocusedWidget.focus();
  221. },
  222. /**
  223. * Reopens settings modal.
  224. *
  225. * @private
  226. */
  227. _reopenChildLockModal: function () {
  228. application.route('settingsmodal', {
  229. title: l10n.get('settings.parental.title'),
  230. subtitle: l10n.get('settings.parental.subtitle'),
  231. description: l10n.get('settings.parental.description'),
  232. onOff: this._parentalSetting,
  233. iconClass: 'icon-lock',
  234. pinCallback: Utils.bind(this._checkPIN, this),
  235. closeModalCallback: Utils.bind(this._closeModalCallback, this)
  236. });
  237. },
  238. /**
  239. * Check pin.
  240. *
  241. * @param {string} value - Pin value.
  242. * @private
  243. */
  244. _checkPIN: function (value) {
  245. application.route('parentalpin', {
  246. successCallback: Utils.bind(function () {
  247. this._lastFocusedWidget.focus();
  248. this._storeParentalSetting(value);
  249. }, this),
  250. escapeCallback: Utils.bind(this._reopenChildLockModal, this),
  251. description: l10n.get('settings.parental.modal_description'),
  252. errorCallback: Utils.bind(function (result) {
  253. if (Utils.getNested(result, 'resultObj', 'remainingAttempts') === 0) {
  254. this.focus();
  255. this._setEventListeners();
  256. }
  257. }, this)
  258. });
  259. },
  260. /**
  261. * OnSelect event.
  262. *
  263. * @param {Object} e - Event.
  264. * @private
  265. */
  266. _onSelect: function (e) {
  267. var selectedId = e.target.id;
  268. this._lastFocusedWidget = e.target;
  269. switch (selectedId) {
  270. case 'logout':
  271. application.route('logout');
  272. break;
  273. case 'child-lock':
  274. this._removeEventListeners();
  275. application.route('settingsmodal', {
  276. title: l10n.get('settings.parental.title'),
  277. subtitle: l10n.get('settings.parental.subtitle'),
  278. description: l10n.get('settings.parental.description'),
  279. onOff: this._parentalSetting,
  280. callback: Utils.bind(this._storeParentalSetting, this),
  281. iconClass: 'icon-lock',
  282. pinCallback: Utils.bind(this._checkPIN, this),
  283. closeModalCallback: Utils.bind(this._closeModalCallback, this)
  284. });
  285. break;
  286. case 'subtitles':
  287. this._removeEventListeners();
  288. application.route('settingsmodal', {
  289. title: l10n.get('settings.subtitles.title'),
  290. subtitle: l10n.get('settings.subtitles.title'),
  291. description: l10n.get('settings.subtitles.description'),
  292. onOff: this._subtitlesSetting,
  293. callback: Utils.bind(this._storeSubtitlesSetting, this),
  294. iconClass: 'icon-subtitles',
  295. closeModalCallback: Utils.bind(this._closeModalCallback, this)
  296. });
  297. break;
  298. }
  299. },
  300. /**
  301. * BeforeShow event.
  302. */
  303. onBeforeShow: function () {
  304. this._setEventListeners();
  305. GA.onPageView('settings');
  306. this._buttonsList.setActiveChildIndex(0);
  307. this._buttonsList.focus();
  308. application.hideLoader();
  309. this._lastFocusedWidget = application.getFocussedWidget();
  310. this._buttonChildLock.setOnOffValue(this._getParentalSetting());
  311. },
  312. /**
  313. * BeforeHide event.
  314. */
  315. onBeforeHide: function () {
  316. this._removeEventListeners();
  317. },
  318. /**
  319. * Sets the event listeners.
  320. *
  321. * @private
  322. */
  323. _setEventListeners: function () {
  324. if (this._listening) {
  325. return;
  326. }
  327. this._listening = true;
  328. this.addEventListener('keydown', this._onKeyDownBound);
  329. this._buttonsList.addEventListener('select', this._onSelectBound);
  330. },
  331. /**
  332. * Removes the event listeners.
  333. *
  334. * @private
  335. */
  336. _removeEventListeners: function () {
  337. if (this._listening) {
  338. this._listening = false;
  339. this.removeEventListener('keydown', this._onKeyDownBound);
  340. this.removeEventListener('select', this._onSelectBound);
  341. }
  342. },
  343. /**
  344. * Executes the close functionality.
  345. */
  346. _onClose: function () {
  347. // Go back and focus the player.
  348. application.focusMenu('main');
  349. }
  350. });
  351. return Settings;
  352. });