'));
});
},
_bindSlide: function() {
var self = this;
this._el.find(".jdat-slider-bg")
.mousedown(function(e) {
if (e.which !== 1) return;
var bg = self._el.find(".jdat-slider-bg");
var bgOffset = bg.offset();
var bgWidth = bg.width();
var min = self._options.min;
var max = self._options.max;
var adjustValue = function(x, finishChange) {
var bgLeft = bgOffset.left;
var value = min + (max - min) * ((x - bgLeft) / bgWidth);
self.value(value, true, finishChange);
}
adjustValue(e.pageX, false);
$(window).on("mousemove.jdatDrag", function(e) {
adjustValue(e.pageX, false);
return false;
});
$(window).one("mouseup", function(e) {
adjustValue(e.pageX, true);
$(window).off("mousemove.jdatDrag");
return false;
});
return false;
});
},
_bindInput: function() {
var self = this;
this._el.find("input")
.change(function(e) {
var value = $(e.currentTarget).val();
self.value(value, true, true);
return false;
})
.mousedown(function(e) {
if (e.which !== 1) return;
var startY = e.pageY;
var step = self._options.step;
var value = self._options.value;
var newValue = value;
$(window).on("mousemove.jdatDrag", function(e) {
var dy = startY - e.pageY;
newValue = dy / 10 * step + value;
self.value(newValue, true, false);
return false;
});
$(window).one("mouseup", function() {
self.value(newValue, true, true);
$(window).off("mousemove.jdatDrag");
return false;
});
});
},
_setSlider: function() {
var value = this._options.value;
var max = this._options.max;
var min = this._options.min;
var pct = (value - min) / (max - min);
var width = 100 * pct + "%";
this._el.find(".jdat-slider-fg").width(width);
},
_setInput: function() {
var value = this._options.value;
this._el.find("input").val(value);
},
label: function(label) {
if (label === undefined) {
return this._options.label;
}
else {
this._options.label = label;
this._el.find(".jdat-field-label").text(label);
}
},
max: function(max, trigger, finishChange) {
if (max === undefined) {
return this._options.max;
}
else {
this._options.max = max;
// only adjust value if outside range
if (this._options.value > max) {
this.value(this._options.value, trigger, finishChange);
}
}
},
min: function(min, trigger, finishChange) {
if (min === undefined) {
return this._options.min;
}
else {
this._options.min = min;
// only adjust value if outside range
if (this._options.value < min) {
this.value(this._options.value, trigger, finishChange);
}
}
},
step: function(step) {
if (step === undefined) {
return this._options.step;
}
else {
this._options.step = step;
}
},
value: function(value, trigger, finishChange) {
if (value === undefined) {
return this._options.value;
}
else {
value = Number(value);
if (isNaN(value)) {
value = this._options.value;
trigger = false;
}
if (value < this._options.min) {
value = this._options.min;
}
else if (value > this._options.max) {
value = this._options.max;
}
var step = this._options.step;
if (value % step != 0) {
// TODO investigate further
var str = String(step);
var p = 0;
if (str.indexOf(".") > -1) {
p = str.length - str.indexOf(".") - 1;
}
var tenTo = Math.pow(10, p);
value = Math.round(value / step) * step;
value = Math.round(value * tenTo) / tenTo;
}
var prevValue = this._options.value;
this._options.value = value;
this._setSlider();
this._setInput();
if (trigger) {
var data = {value: value, previous: prevValue};
this._trigger(data, finishChange);
}
}
}
});
return SliderField;
})();
}(jQuery);
!function($) { "use strict";
/*
* StringField
*/
JDat.StringField = (function() {
var defaults = {
label: "String",
value: "",
maxLength: 100,
minLength: 0,
regexp: null,
strip: false
}
var StringField = function(el, options) {
el.addClass("jdat-string");
var opts = $.extend({}, defaults, options);
JDat.BaseField.call(this, el, opts);
this._bindInput();
this._initialize();
}
JDat.extend(StringField, JDat.BaseField, {
_render: function() {
this._template()
.append($('
'));
},
_bindInput: function() {
var self = this;
this._el.find("input")
.change(function(e) {
var value = $(e.currentTarget).val();
self.value(value, true, true);
return false;
})
.keyup(function(e) {
if (e.keyCode != 13) { // only on non return key
var value = $(e.currentTarget).val();
self.value(value, true, false);
}
});
},
value: function(string, trigger, finishChange) {
if (string === undefined) {
return this._options.value;
}
else {
var valid = true;
if (this._options.strip) {
string = string.replace(/^\s+|\s+$/g, '');
}
var l = string.length;
if (l > this._options.maxLength || l < this._options.minLength) {
valid = false;
}
var regexp = this._options.regexp;
if (regexp && !regexp.test(string)) {
valid = false;
}
if (!valid) {
string = this._options.value;
trigger = false;
}
var prevString = this._options.value;
this._options.value = string;
this._el.find('input').val(string);
if (trigger) {
var data = {value: string, previous: prevString};
this._trigger(data, finishChange);
}
}
},
minLength: function(minLength) {
if (minLength === undefined) {
return this._options.minLength;
}
else {
minLength = Number(minLength);
this._options.minLength = minLength;
}
},
maxLength: function(maxLength) {
if (maxLength === undefined) {
return this._options.maxLength;
}
else {
maxLength = Number(maxLength);
this._options.maxLength = maxLength;
}
},
strip: function(strip) {
if (strip === undefined) {
return this._options.stip;
}
else {
strip = Boolean(strip);
this._options.strip = strip;
}
},
regexp: function(regexp) {
if (regexp === undefined) {
return this._options.regexp;
}
else {
regexp = RegExp(regexp);
this._options.regexp = regexp;
}
}
});
return StringField;
})();
}(jQuery);
!function($) { "use strict";
/*
* ToggleField
*/
JDat.ToggleField = (function() {
var defaults = {
label: "Toggle",
value: false
}
var ToggleField = function(el, options) {
el.addClass("jdat-toggle");
var opts = $.extend({}, defaults, options);
JDat.BaseField.call(this, el, opts);
this._bindToggle();
this._initialize();
}
JDat.extend(ToggleField, JDat.BaseField, {
_render: function() {
this._template()
.append($('
')
.append($('
')
.text("OFF")));
},
_bindToggle: function() {
var self = this;
this._el.find(".jdat-toggle-bg")
.click(function(e) {
self.value(!self.value(), true, true);
return false;
});
},
value: function(checked, trigger, finishChange) {
if (checked === undefined) {
return this._options.value;
}
else {
var checked = Boolean(checked);
var toggle = this._el.find(".jdat-toggle-fg");
if (checked) {
toggle.addClass("jdat-toggle-on");
toggle.text("ON");
}
else {
toggle.removeClass("jdat-toggle-on");
toggle.text("OFF");
}
this._options.value = checked;
var data = {value: checked};
if (trigger) {
this._trigger(data, finishChange);
}
}
}
});
return ToggleField;
})();
}(jQuery);