# Code coverage report for lib/attributes/value.js

## Statements: 86.89% (53 / 61) Branches: 77.36% (41 / 53) Functions: 92.31% (12 / 13) Lines: 91.07% (51 / 56) Ignored: none

All files » lib/attributes/ » value.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    1751

    1
    8
    8

    1

    8
    8
    24

    8

    7

    7
    6
    2

    5
    5

    5

    13

    13
    1

    12
    12

    12

    12
    20

    27
    25

    7

    7
    5

    7

    7

    7

    5

    41

    41
    21
    1
    20
    20

    20
    2

    18

    20
    6

    6

    14

    7
    7

    1
    8

    1
     var BaseAttribute = require("./script"),
    _bind = require("../utils/bind");

    /**
     */

    function ValueAttribute(options) {
      this._onInput = _bind(this._onInput, this);
      BaseAttribute.call(this, options);
    }

    /**
     */

    BaseAttribute.extend(ValueAttribute, {

      /**
       */

      _events: ["change","keyup","input"],

      /**
       */

      initialize: function() {
        var self = this;
        this._events.forEach(function(event) {
          self.node.addEventListener(event, self._onInput);
        });
      },

      /**
       */

      bind: function() {
        BaseAttribute.prototype.bind.call(this);

        var self = this;

        // TODO - move this to another attribute helper (more optimal)
        if (/^(text|password|email)$/.test(this.node.getAttribute("type"))) {
          this._autocompleteCheckInterval = setInterval(function() {
            self._onInput();
          }, process.browser ? 500 : 10);
        }
      },

      /**
       */

      unbind: function() {
        BaseAttribute.prototype.unbind.call(this);
        clearInterval(this._autocompleteCheckInterval);

        var self = this;
      },

      /**
       */

      update: function() {

        var model = this.model = this.currentValue;

        if (!model || !model.__isReference) {
          throw new Error("input value must be a reference. Make sure you have <~> defined");
        }

        Iif (this._modelBindings) this._modelBindings.dispose();
        Iif (!model) return;

        var self = this;

        this._modelBindings = this.view.watch(model.path, function(value) {
          self._elementValue(self._parseValue(value));
        }).trigger();
      },

      _parseValue: function(value) {
        if (value == null || value === "") return void 0;
        return value;
      },

      /**
       */

      test: function(attrValue) {
        return attrValue.length === 1;
      },

      /**
       */

      _onInput: function(event) {

        clearInterval(this._autocompleteCheckInterval);

        // ignore some keys
        if (event && (!event.keyCode || !~[27].indexOf(event.keyCode))) {
          event.stopPropagation();
        }

        var value = this._parseValue(this._elementValue());

        Iif (!this.model) return;

        if (String(this.model.value()) == String(value))  return;

        this.model.value(value);
      },

      /**
       */

      _elementValue: function(value) {

        var isCheckbox    = /checkbox/.test(this.node.type),
        isRadio           = /radio/.test(this.node.type),
        isRadioOrCheckbox = isCheckbox || isRadio,
        isInput           = Object.prototype.hasOwnProperty.call(this.node, "value") || /input|textarea|checkbox/.test(this.node.nodeName.toLowerCase());

        if (!arguments.length) {
          if (isCheckbox) {
            return Boolean(this.node.checked);
          } else Eif (isInput) {
            return this.node.value || "";
          } else {
            return this.node.innerHTML || "";
          }
        }

        if (value == null) {
          value = "";
        } else {
          clearInterval(this._autocompleteCheckInterval);
        }

        if (isRadioOrCheckbox) {
          Iif (isRadio) {
            if (String(value) === String(this.node.value)) {
              this.node.checked = true;
            }
          } else {
            this.node.checked = value;
          }
        } else if(String(value) !== this._elementValue()) {

          Eif (isInput) {
            this.node.value = value;
          } else {
            this.node.innerHTML = value;
          }
        }
      }
    });

    /**
     */

    ValueAttribute.test = function(value) {
      return typeof value === "object" && !value.buffered;
    };

    /**
     */

    module.exports = ValueAttribute;