Sha256: 2fc620f96a614ad62412ecb5d1564488d51569a2afff8d28b604e75167e52cfb

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

import Component from './component';
import { trigger, createElement} from 'dolla';

export class Checkbox extends Component {
    static CSSClass = "uniformCheckbox";
    static type = 'checkbox';
    
    initialize (options) {
        if(options.input instanceof Element) {
          this.input = options.input
        } else {
          this.input = createElement('input', Object.assign({}, {
            type: this.constructor.type
          }, options.input)) // TODO filter options to dolla.HTML_ATTRIBUTES
        }
        
        if(!options.tabindex) this.el.tabIndex = 0;
        this.el.classList.add(this.constructor.CSSClass);

        this.listenTo(this.el, 'click', this.click);
        this.listenTo(this.input, 'change', this.change);
        this.listenTo(document, 'keyup', this.keyup);
        this.listenTo(document, 'keydown', this.keydown);
    }
    
    render () {
        this.input.style.display = "none";

        if(this.input.parentNode){
          this.input.parentNode.insertBefore(this.el, this.input.nextSibling);
        } else {
          this.el.append(this.input);
        }
        
        this.change()

        return this;
    }
    
    change () {
        this.el.classList.toggle('checked', this.input.checked);
    }
    
    click (e) {
        if (this.input.disabled) return;
        
        this.input.checked = !this.input.checked
        trigger(this.input, 'change');
        e.preventDefault();
    }
    
    keyup (e) {
        if(document.activeElement != this.el) return;
        if(e.keyCode != 32) return;

        e.preventDefault();
        this.click(e);
    }
    
    keydown(e) {
      if(e.keyCode == 32 && document.activeElement == this.el) {
        // Prevent Page Scroll
        e.preventDefault();
      }
    }
}

export class Radio extends Checkbox {
  static CSSClass = "uniformRadio";
}

export class Toggle extends Checkbox {
  static CSSClass = "uniformToggle";
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
uniform-ui-3.0.0.beta8 lib/assets/javascripts/uniform/checkbox.js
uniform-ui-3.0.0.beta5 lib/assets/javascripts/uniform/checkbox.js
uniform-ui-3.0.0.beta4 lib/assets/javascripts/uniform/checkbox.js