{"size": 2200, "ext": "js", "max_stars_repo_path": "src/check-box.js", "max_stars_repo_name": "meanie/angular-form-controls", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/check-box.js", "max_issues_repo_name": "meanie/angular-form-controls", "max_issues_repo_issues_event_min_datetime": "2020-07-17T01:18:08.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-08T19:47:05.000Z", "max_forks_repo_path": "src/check-box.js", "max_forks_repo_name": "meanie/angular-form-controls", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 20.3703703704, "max_line_length": 73, "alphanum_fraction": 0.5436363636}

/**
 * Module definition and dependencies
 */
angular.module('CheckBox.Component', [])

/**
 * Checkbox component
 */
.component('checkBox', {
  template:
    `<label class="CheckBox {{$ctrl.classes}}"
      ng-transclude
      ng-click="$ctrl.toggle($event)"
      ng-class="{checked: $ctrl.isChecked(), disabled: $ctrl.isDisabled}"
    ></label>`,
  require: {
    ngModel: 'ngModel',
  },
  transclude: true,
  bindings: {
    model: '<ngModel',
    onChange: '&',
    isInverse: '<',
    isUndefined: '<',
    isDisabled: '<ngDisabled',
  },

  /**
   * Component controller
   */
  controller($element, $formControls) {

    //Get instance
    let $ctrl = this;

    /**
     * On init
     */
    this.$onInit = function() {

      //Propagate classes
      this.classes = $element[0].className;
      $element[0].className = '';

      //Add checkbox wrapper class to parent component
      $element.addClass('CheckBoxWrapper');

      //Find label
      const $label = $element.find('label');

      //Propagate focus
      $element.on('focus', () => {
        $label[0].focus();
      });

      //Empty check override in order for ng-required to work properly
      this.ngModel.$isEmpty = function() {
        if ($ctrl.isInverse) {
          return !!$ctrl.model;
        }
        return !$ctrl.model;
      };
    };

    /**
     * On change
     */
    this.$onChanges = function(changes) {

      //Validate and mark as dirty if needed
      if (changes.model) {
        this.ngModel.$validate();
        if ($formControls.hasChanged(changes.model)) {
          this.ngModel.$setDirty();
        }
      }
    };

    /**
     * Toggle
     */
    this.toggle = function(event) {

      //Don't toggle when disabled or event default prevented
      if (this.isDisabled || event.defaultPrevented) {
        return;
      }

      //Determine value
      let value = !this.model;
      if (this.isUndefined && value === false) {
        value = undefined;
      }

      //Call on change handler
      this.onChange({value});
    };

    /**
     * Check if checked
     */
    this.isChecked = function() {
      return (this.isInverse ? !this.model : !!this.model);
    };
  },
});
