Skip to content

Commit

Permalink
feat(base): Annotate mdc-base for closure (#730)
Browse files Browse the repository at this point in the history
Resolves #331: Annotate mdc-base for closure
  • Loading branch information
lynnmercier authored May 26, 2017
1 parent 6c1043e commit e21ec90
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
}
},
"closureWhitelist": [
"mdc-animation"
"mdc-animation",
"mdc-base"
]
}
43 changes: 37 additions & 6 deletions packages/mdc-base/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@

import MDCFoundation from './foundation';

/**
* @template F
*/
export default class MDCComponent {

/**
* @param {!Element} root
* @return {!MDCComponent}
*/
static attachTo(root) {
// Subclasses which extend MDCBase should provide an attachTo() method that takes a root element and
// returns an instantiated component with its root set to that element. Also note that in the cases of
Expand All @@ -25,11 +33,18 @@ export default class MDCComponent {
return new MDCComponent(root, new MDCFoundation());
}

/**
* @param {!Element} root
* @param {!F} foundation
* @param {...?} args
*/
constructor(root, foundation = undefined, ...args) {
/** @private {!Element} */
this.root_ = root;
this.initialize(...args);
// Note that we initialize foundation here and not within the constructor's default param so that
// this.root_ is defined and can be used within the foundation class.
/** @private {!F} */
this.foundation_ = foundation === undefined ? this.getDefaultFoundation() : foundation;
this.foundation_.init();
this.initialSyncWithDOM();
Expand All @@ -41,6 +56,9 @@ export default class MDCComponent {
// initialized. Any additional arguments besides root and foundation will be passed in here.
}

/**
* @return {!F} foundation
*/
getDefaultFoundation() {
// Subclasses must override this method to return a properly configured foundation class for the
// component.
Expand All @@ -61,20 +79,33 @@ export default class MDCComponent {
this.foundation_.destroy();
}

// Wrapper method to add an event listener to the component's root element. This is most useful when
// listening for custom events.
/**
* Wrapper method to add an event listener to the component's root element. This is most useful when
* listening for custom events.
* @param {string} evtType
* @param {!Function} handler
*/
listen(evtType, handler) {
this.root_.addEventListener(evtType, handler);
}

// Wrapper method to remove an event listener to the component's root element. This is most useful when
// unlistening for custom events.
/**
* Wrapper method to remove an event listener to the component's root element. This is most useful when
* unlistening for custom events.
* @param {string} evtType
* @param {!Function} handler
*/
unlisten(evtType, handler) {
this.root_.removeEventListener(evtType, handler);
}

// Fires a cross-browser-compatible custom event from the component root of the given type,
// with the given data.
/**
* Fires a cross-browser-compatible custom event from the component root of the given type,
* with the given data.
* @param {string} evtType
* @param {!Object} evtData
* @param {boolean} shouldBubble
*/
emit(evtType, evtData, shouldBubble = false) {
let evt;
if (typeof CustomEvent === 'function') {
Expand Down
12 changes: 12 additions & 0 deletions packages/mdc-base/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,45 @@
* limitations under the License.
*/

/**
* @template A
*/
export default class MDCFoundation {

/** @return enum{cssClasses} */
static get cssClasses() {
// Classes extending MDCFoundation should implement this method to return an object which exports every
// CSS class the foundation class needs as a property. e.g. {ACTIVE: 'mdc-component--active'}
return {};
}

/** @return enum{strings} */
static get strings() {
// Classes extending MDCFoundation should implement this method to return an object which exports all
// semantic strings as constants. e.g. {ARIA_ROLE: 'tablist'}
return {};
}

/** @return enum{numbers} */
static get numbers() {
// Classes extending MDCFoundation should implement this method to return an object which exports all
// of its semantic numbers as constants. e.g. {ANIMATION_DELAY_MS: 350}
return {};
}

/** @return {!Object} */
static get defaultAdapter() {
// Classes extending MDCFoundation may choose to implement this getter in order to provide a convenient
// way of viewing the necessary methods of an adapter. In the future, this could also be used for adapter
// validation.
return {};
}

/**
* @param {!A} adapter
*/
constructor(adapter = {}) {
/** @private {!A} */
this.adapter_ = adapter;
}

Expand Down

0 comments on commit e21ec90

Please sign in to comment.