/*! jdat - v0.1.0 - 2014-01-04 * https://github.com/medihack/jdat * Copyright (c) 2014 Kai Schlamp; Licensed MIT */ var JDat = JDat || {}; !function($) { "use strict"; /* * Helpers */ JDat.extend = function(sub, base, methods) { var tmp = function(){}; tmp.prototype = base.prototype; sub.prototype = new tmp(); sub.prototype.constructor = sub; $.extend(sub.prototype, methods); } JDat.ColorHelper = { hex2hsv: function(hex) { var bigint = parseInt(hex.slice(1), 16); var r = (bigint >> 16) & 255; var g = (bigint >> 8) & 255; var b = bigint & 255; return this.rgb2hsv([r, g, b]); }, hsv2hex: function(hsv) { var rgb = this.hsv2rgb(hsv); return this.rgb2hex(rgb); }, // Credits to http://www.raphaeljs.com hsv2rgb: function(hsv) { var R, G, B, X, C; var h = (hsv[0] % 360) / 60; C = hsv[2] * hsv[1]; X = C * (1 - Math.abs(h % 2 - 1)); R = G = B = hsv[2] - C; h = ~~h; R += [C, X, 0, 0, X, C][h]; G += [X, C, C, X, 0, 0][h]; B += [0, 0, X, C, C, X][h]; var r = Math.floor(R * 255); var g = Math.floor(G * 255); var b = Math.floor(B * 255); return [r, g, b]; }, rgb2hex: function(rgb) { var r = rgb[0]; var g = rgb[1]; var b = rgb[2]; return "#" + (16777216 | b | (g << 8) | (r << 16)) .toString(16).slice(1); }, // r, g, b can be either in <0,1> range or <0,255> range. // Credits to http://www.raphaeljs.com rgb2hsv: function(rgb) { var r = rgb[0]; var g = rgb[1]; var b = rgb[2]; if (rgb[0] > 1 || rgb[1] > 1 || rgb[2] > 1) { r /= 255; g /= 255; b /= 255; } var h, s, v, c; v = Math.max(r, g, b); c = v - Math.min(r, g, b); h = (c == 0 ? null : v == r ? (g - b) / c + (g < b ? 6 : 0) : v == g ? (b - r) / c + 2 : (r - g) / c + 4); h = (h % 6) * 60; s = c == 0 ? 0 : c / v; return [h, s, v]; }, } /* * BaseField */ JDat.BaseField = (function() { var defaults = { id: null, binding: null, label: null, titleize: false, onSetup: null, onChange: null, onFinishChange: null, onUpdateView: null, onUpdateModel: null } var BaseField = function(el, options) { this._el = el; this._options = $.extend({}, defaults, options); this._render(); if (this._options.onSetup) { this._options.onSetup.call(this); } } BaseField.prototype = { constructor: BaseField, _initialize: function() { if (this._options.value) { this.value(this._options.value, false); } this.updateView(); }, _trigger: function(data, finishChange) { data.source = this; if (this._options.onUpdateModel) { this._options.onUpdateModel.call(this, this._options.model, this._options.binding, data.value, finishChange); } if (this._options.onChange) { this._options.onChange.call(this, data); } if (finishChange === undefined || finishChange) { if (this._options.onFinishChange) { this._options.onFinishChange.call(this, data); } } }, _template: function() { var fieldContainer = $('
') .appendTo(this._el); if (!this._options.label) return fieldContainer; var fieldPanel = $('
'); var fieldLabel = $(''); if (this._options.titleize) fieldLabel.addClass("jdat-title"); fieldContainer.append(fieldLabel) .append(fieldPanel); this.label(this._options.label); return fieldPanel; }, id: function() { return this._options.id; }, label: function(label) { if (label === undefined) { return this._options.label; } else { this._options.label = label; this._el.find(".jdat-field-label").text(label); } }, onChange: function(func) { if (func === undefined) { return this._options.onChange; } else { this._options.onChange = func; } }, show: function(complete) { var self = this; this._el.slideDown("fast", function() { $(this).show(); if ($.isFunction(complete)) { complete.call(self); } }); }, hide: function(complete) { var self = this; this._el.slideUp("fast", function() { $(this).hide(); if ($.isFunction(complete)) { complete.call(self); } }); }, enable: function() { this._el.find($(".jdat-field-disabler") .remove()); }, disable: function(loading) { var disabler = this._el.find(".jdat-field-disabler"); if (!disabler.length) { disabler = $('
') .appendTo(this._el); if (loading) { disabler.addClass("jdat-loading"); } } }, updateView: function() { if (this._options.onUpdateView) { this._options.onUpdateView.call(this, this._options.model, this._options.binding); } } } return BaseField; })(); /* * SectionField */ JDat.SectionField = (function() { var defaults = { label: "Section", title: true, closeable: true, closed: false, indent: true } var SectionField = function(el, options) { el.addClass("jdat-section"); var opts = $.extend({}, defaults, options); JDat.BaseField.call(this, el, opts); this._bindClose(); if (this._options.closed) { this.close(); } } JDat.extend(SectionField, JDat.BaseField, { _render: function() { if (this._options.title) { this._template().remove(); // removes the field panel this._el.find(".jdat-field-label") .addClass("jdat-section-title") .addClass("jdat-title"); if (this._options.closeable) { this._el.find(".jdat-field-container") .prepend($('
')); } } var sectionPanel = $('