Home | History | Annotate | Download | only in settings
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 cr.define('print_preview', function() {
      6   'use strict';
      7 
      8   /**
      9    * Base class for print option section components.
     10    * @constructor
     11    * @extends {print_preview.Component}
     12    */
     13   function SettingsSection() {
     14     print_preview.Component.call(this);
     15 
     16     /**
     17      * Cached "hasCollapsibleContent" status for COLLAPSIBLE_CONTENT_CHANGED
     18      * notification.
     19      * @private {?boolean}
     20      */
     21     this.hasCollapsibleContentCached_ = null;
     22 
     23     /**
     24      * Whether content of this section should be collapsed or not.
     25      * @private {boolean}
     26      */
     27     this.collapseContent_ = true;
     28   };
     29 
     30   /**
     31    * Event types dispatched by this class.
     32    * @enum {string}
     33    */
     34   SettingsSection.EventType = {
     35     COLLAPSIBLE_CONTENT_CHANGED:
     36         'print_preview.SettingsSection.COLLAPSIBLE_CONTENT_CHANGED'
     37   };
     38 
     39   SettingsSection.prototype = {
     40     __proto__: print_preview.Component.prototype,
     41 
     42     /** @return {boolean} Whether this section should be displayed or not. */
     43     isAvailable: function() {
     44       throw Error('Abstract method not overridden');
     45     },
     46 
     47     /**
     48      * @return {boolean} Whether this section has a content which can be
     49      *     collapsed/expanded.
     50      */
     51     hasCollapsibleContent: function() {
     52       throw Error('Abstract method not overridden');
     53     },
     54 
     55     /** @param {boolean} isEnabled Whether this component is enabled. */
     56     set isEnabled(isEnabled) {
     57       throw Error('Abstract method not overridden');
     58     },
     59 
     60     /**
     61      * @return {boolean} Whether the content of this section should be
     62      *     collapsed.
     63      */
     64     get collapseContent() {
     65       return this.collapseContent_;
     66     },
     67 
     68     /**
     69      * @param {boolean} collapseContent Whether the content of this section
     70      *     should be collapsed, even if this section is available.
     71      * @param {boolean} noAnimation Whether section visibility transition
     72      *     should not be animated.
     73      */
     74     setCollapseContent: function(collapseContent, noAnimation) {
     75       this.collapseContent_ = collapseContent && this.hasCollapsibleContent();
     76       this.updateUiStateInternal(noAnimation);
     77     },
     78 
     79     /** @override */
     80     enterDocument: function() {
     81       print_preview.Component.prototype.enterDocument.call(this);
     82       this.isAvailable_ = this.isAvailable();
     83       if (!this.isAvailable())
     84         fadeOutOption(this.getElement(), true);
     85     },
     86 
     87     /**
     88      * Updates the component appearance according to the current state.
     89      * @param {boolean=} opt_noAnimation Whether section visibility transition
     90      *     should not be animated.
     91      * @protected
     92      */
     93     updateUiStateInternal: function(opt_noAnimation) {
     94       var hasCollapsibleContent = this.hasCollapsibleContent();
     95       var changed = this.hasCollapsibleContentCached_ != hasCollapsibleContent;
     96       this.hasCollapsibleContentCached_ = hasCollapsibleContent;
     97 
     98       if (this.isSectionVisibleInternal())
     99         fadeInOption(this.getElement(), opt_noAnimation);
    100       else
    101         fadeOutOption(this.getElement(), opt_noAnimation);
    102 
    103       if (changed) {
    104         cr.dispatchSimpleEvent(
    105             this, SettingsSection.EventType.COLLAPSIBLE_CONTENT_CHANGED);
    106       }
    107     },
    108 
    109     /**
    110      * @return {boolean} Whether this section should be displayed or not.
    111      * @protected
    112      */
    113     isSectionVisibleInternal: function() {
    114       return this.isAvailable() && !this.collapseContent_;
    115     }
    116   };
    117 
    118   // Export
    119   return {
    120     SettingsSection: SettingsSection
    121   };
    122 });
    123