Source: pages/settings-page.js

'use strict';

  new ExceptionHandler();

  Polymer({
    is: 'settings-page',

    behaviors: [
      Chrome.LocalizeBehavior,
    ],

    properties: {
      selectedTab: {
        type: Number,
        value: 0,
        notify: true,
      },

      enabled: {
        type: Boolean,
        value: true,
        notify: true,
      },

      showTimeValue: {
        type: Number,
        value: 1,
        notify: true,
      },

      menuHidden: {
        type: Boolean,
        computed: '_computeMenuHidden(selectedTab)',
      },
    },

    ready: function() {
      this.set('selectedTab', 0);
    },

    /**
     * Deselect the given {@link app.PhotoSource}
     * @param {string} useName - Name of <setting-toggle>
     */
    deselectPhotoSource: function(useName) {
      this._setPhotoSourceChecked(useName, false);
    },

    /**
     * Event: select all {@link app.PhotoSource} objects tapped
     * @private
     */
    _selectAllTapped: function() {
      this._setPhotoSourcesChecked(true);
    },

    /**
     * Event: deselect all {@link app.PhotoSource} objects tapped
     * @private
     */
    _deselectAllTapped: function() {
      this._setPhotoSourcesChecked(false);
    },

    /**
     * Event: restore default settings tapped
     * @private
     */
    _restoreDefaultsTapped: function() {
      Chrome.Msg.send(Chrome.Msg.RESTORE_DEFAULTS).catch(() => {});
    },

    /**
     * Event: Process the background permission
     * @private
     */
    _chromeBackgroundTapped() {
      const isSet = !Chrome.Storage.get('allowBackground');
      const perm = app.Permissions.BACKGROUND;
      const isAllowed = app.Permissions.isAllowed(perm);
      const errTitle = Chrome.Locale.localize('err_optional_permissions');
      if (isSet && !isAllowed) {
        app.Permissions.request(perm).catch((err) => {
          Chrome.Log.error(err.message,
              'settings-page._chromeBackgroundTapped', errTitle);
        });
      } else if (!isSet && isAllowed) {
        app.Permissions.remove(perm).catch((err) => {
          Chrome.Log.error(err.message,
              'settings-page._chromeBackgroundTapped', errTitle);
        });
      }
    },

    /**
     * Set checked state of a {@link app.PhotoSource}
     * @param {string} useName - source name
     * @param {boolean} state - checked state
     * @private
     */
    _setPhotoSourceChecked: function(useName, state) {
      const query = `[name=${useName}]`;
      const el = document.querySelector(query);
      if (el && !useName.includes('useGoogle')) {
        el.setChecked(state);
      }
    },

    /**
     * Set checked state of all {@link app.PhotoSource} objects
     * @param {boolean} state - checked state
     * @private
     */
    _setPhotoSourcesChecked: function(state) {
      const useNames = app.PhotoSources.getUseKeys();
      useNames.forEach((useName) => {
        this._setPhotoSourceChecked(useName, state);
      });
    },

  /**
     * Computed property: Set menu icons visibility
     * @param {int} selectedTab - the current tab
     * @returns {boolean} true if menu should be visible
     * @private
     */
    _computeMenuHidden: function(selectedTab) {
      return (selectedTab !== 2);
    },

    _getUnit: function(name, min, max, step, mult) {
      return {
        'name': Chrome.Locale.localize(name),
        'min': min, 'max': max, 'step': step, 'mult': mult,
      };
    },

    /**
     * Computed binding: Set disabled state of largeTime toggle
     * @param {boolean} enabled - enabled state of screensaver
     * @param {number} showTimeValue - showTime value
     * @returns {boolean} true if disabled
     * @private
     */
    _computeLargeTimeDisabled: function(enabled, showTimeValue) {
      let ret = false;
      if (!enabled || (showTimeValue === 0)) {
        ret = true;
      }
      return ret;
    },

    /**
     * Computed binding: idle time values
     * @returns {Array} Array of menu items
     * @private
     */
    _computeWaitTimeUnits: function() {
      return [
        this._getUnit('minutes', 1, 60, 1, 1),
        this._getUnit('hours', 1, 24, 1, 60),
        this._getUnit('days', 1, 365, 1, 1440),
      ];
    },

    /**
     * Computed binding: transition time values
     * @returns {Array} Array of menu items
     * @private
     */
    _computeTransitionTimeUnits: function() {
      return [
        this._getUnit('seconds', 4, 60, 1, 1),
        this._getUnit('minutes', 1, 60, 1, 60),
        this._getUnit('hours', 1, 24, 1, 3600),
        this._getUnit('days', 1, 365, 1, 86400),
      ];
    },

    /**
     * Computed binding: photo sizing values
     * @returns {Array} Array of menu items
     * @private
     */
    _computePhotoSizingMenu: function() {
      return [
        Chrome.Locale.localize('menu_letterbox'),
        Chrome.Locale.localize('menu_zoom'),
        Chrome.Locale.localize('menu_frame'),
        Chrome.Locale.localize('menu_full'),
        Chrome.Locale.localize('menu_random'),
      ];
    },

    /**
     * Computed binding: photo transition values
     * @returns {Array} Array of menu items
     * @private
     */
    _computePhotoTransitionMenu: function() {
      return [
        Chrome.Locale.localize('menu_scale_up'),
        Chrome.Locale.localize('menu_fade'),
        Chrome.Locale.localize('menu_slide_from_right'),
        Chrome.Locale.localize('menu_slide_down'),
        Chrome.Locale.localize('menu_spin_up'),
        Chrome.Locale.localize('menu_slide_up'),
        Chrome.Locale.localize('menu_slide_from_bottom'),
        Chrome.Locale.localize('menu_slide_right'),
        Chrome.Locale.localize('menu_random'),
      ];
    },

    /**
     * Computed binding: time format values
     * @returns {Array} Array of menu items
     * @private
     */
    _computeTimeFormatMenu: function() {
      return [
        Chrome.Locale.localize('no'),
        Chrome.Locale.localize('menu_12_hour'),
        Chrome.Locale.localize('menu_24_hour'),
      ];
    },
  });