Class: Switch

Defined in: vendor/assets/javascripts/styleguide/plugins/tbg-switch.js.coffee

Overview

SWITCH CLASS DEFINITION

Examples:

How to ensure plugin self initialises on element

<div data-switch="true"> ... </div>

How to group switches and allow them to change state of other switches in that group

<div data-switch-group="[group name]"> ... </div>

How to toggle switch state

<div data-switch-toggle="true"> ... </div>

Instance Method Summary

Constructor Details

- (void) constructor(el)

Construct a new Switch instance

Set this.parent, this.target, this.group and this.container elements for this.el; set this.toggle if attribute present

Parameters:

  • el (Object) HTMLElement, this.el, that acts as the switch

Instance Method Details

- (void) click()

Click handler - If this.parent isn't active, close group if this.group defined and change state to true; if this.parent is active and this.toggle set then change state to false otherwise return undefined

- (void) changeStateTo(action, elements)

Change active state of target content - add or remove activeClass from content and parent elements

Parameters:

  • action (Boolean) true to addClass to elements, false to removeClass from elements
  • elements (Array) array of jQuery wrapped HTMLElements - target content element and parent element

- (void) _closeGroup() (private)

Close group - if any content is active, get the target for that element's descendant link element and pass false and elements array [activeContent, activeEl] to switch group

- (Object) _getContent(el) (private)

Get target element to do switch on

Parameters:

  • el (Object) jQuery wrapped HTMLElement to get target element from

Returns:

  • (Object) — jQuery wrapped HTMLElement for el's target element