Source: control-plusminus.js

/**
  * Construct a new Plus/Minus control.
  *
  * @class
  * @param {Home} home the home that this control belongs to.
  * @param {String} id the id of this control. Updates will be POSTed to
  *                    <code>home.control_endpoint/id</code>
  * @param {Object} options options for this control:
  * <ul>
  *   <li>
  *     <code>name</code> - String, required - friendly name for this control
  *   </li>
  *   <li>
  *     <code>min_value</code> - float, required - minimum value.
  *   </li>
  *   <li>
  *     <code>max_value</code> - float, required - maximum value.
  *   </li>
  *   <li>
  *     <code>step</code> - float, required - amount to add / subtract when
  *     clicking the + / - buttons.
  *   </li>
  *   <li>
  *     <code>current</code> - float, required - initial value.
  *   </li>
  * </ul>
  */
function PlusMinusControl(home, id, options) {
  "use strict";
  this.home = home;
  this.id = id;
  this.options = options;
  var template_source = $("#control-plusminus-template").html();
  this.template = Handlebars.compile(template_source);
}

PlusMinusControl.prototype.render = function() {
  "use strict";
  this.html = $(this.template(this.options));
  var thiz = this;
  this.input = this.html.find("input")
      .val(this.options.current)
      .on("change", function() {
        var jq = $(this);
        var val = parseFloat(jq.val());
        if (isNaN(val)) {
          val = thiz.options.max_value / 2 + thiz.options.min_value / 2;
          jq.val(val);
        }
        if (val < thiz.options.min_value) {
          val = thiz.options.min_value;
          jq.val(val);
        } else if (val > thiz.options.max_value) {
          val = thiz.options.max_value;
          jq.val(val);
        }
        $.post(thiz.home.options.control_endpoint + thiz.id, {"value": val});
      });
  this.html.find("button").on("click", function() {
    var val = parseFloat(thiz.input.val());
    if (isNaN(val)) {
      val = thiz.options.max_value / 2 + thiz.options.min_value / 2;
    }
    var jq = $(this);
    if (jq.hasClass("minus")) {
      val = val - thiz.options.step;
    } else if (jq.hasClass("plus")) {
      val = val + thiz.options.step;
    }
    thiz.input.val(val);
    thiz.input.trigger("change");
  });
  return this.html;
};

Home.register_control("plus-minus", PlusMinusControl);