3 * jQuery Progress Bar Config Plugin
5 * This is a plugin for generating a configuration that can be used in
6 * with the jQuery ProgressBar plugin. This generates an intuitive widget for
7 * choosing where color ranges start/end.
9 * Copyright (c) 2011 Open Source Lab
14 progressBarConfig: new function() {
26 this.construct = function(arg1) {
27 var arg_config = null;
33 return this.each(function() {
36 var config = undefined;
38 // create configuration
39 if (arg_config==null) {
40 config = $.extend({}, $.progressBarConfig.defaults, arg_config);
42 config = $.extend(this.config, arg_config);
44 if (input.val() != '') {
45 config.colors = $.parseJSON(input.val());
49 var colors = config.colors;
50 var width = config.width;
51 var slider = undefined;
53 this._init = function() {
54 // Process color information, also resize divs to fit the slider.
55 slider = $("<div class='slider'></div>");
56 for (var i in colors){
58 var div = $("<div class='section'></div>");
59 var color = colors[i][0];
60 var start = colors[i][1];
64 if (i < colors.length-1) {
65 div.append("<div class='handle right'></div>");
66 div.width((colors[i+1][1]-start)/100*width);
68 div.width((100-start)/100*width);
72 div.append("<div class='handle left'></div>");
77 slider.find('.handle').mousedown(control._mousedown);
82 var origin = undefined;
83 var moving = undefined;
84 var other = undefined;
85 var moving_origin = undefined;
86 var other_origin = undefined;
87 var reverse = undefined;
89 control._mousedown = function(event){
92 moving = bound.parent();
93 if (bound.hasClass('left')) {
94 other = moving.prev();
97 other = moving.next();
100 moving_origin = moving.width();
101 other_origin = other.width();
104 .mouseup(control._mouseup)
105 .mousemove(control._slide_react);
108 control._mouseup = function(event) {
109 event.preventDefault();
110 slider.unbind('mousemove');
116 .unbind('mousemove');
119 // XXX no JSON encoder so just manually encode array
120 var sections = slider.find('.section');
123 for (var i in colors) {
127 data += '["' + [colors[i][0]] + '",' + acc + ']';
128 acc += $(sections[i]).width()/width*100;
134 this._slide_react = function(event) {
135 var offset = event.pageX - origin;
138 if (offset > 1 && moving_origin-offset < 0) {
139 offset = moving_origin;
140 } else if (other_origin+offset < 0) {
141 offset = -1*other_origin;
143 moving.width(moving_origin-offset);
144 other.width(other_origin+offset);
146 if (offset > 1 && other_origin-offset < 0) {
147 offset = other_origin;
148 } else if (moving_origin+offset < 0) {
149 offset = -1*moving_origin;
151 moving.width(moving_origin+offset);
152 other.width(other_origin-offset);
163 progressBarConfig: $.progressBarConfig.construct