Вход Регистрация
Файл: templates/backend/default/assets/plugins/bootstrap-colorpicker/js/bootstrap-colorpicker.js
Строк: 383
<?php
/* =========================================================
 * bootstrap-colorpicker.js 
 * http://www.eyecon.ro/bootstrap-colorpicker
 * =========================================================
 * Copyright 2012 Stefan Petre
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================= */

!function($) {

    
// Color object

    
var Color = function(val) {
        
this.value = {
            
h1,
            
s1,
            
b1,
            
a1
        
};
        
this.setColor(val);
    };

    
Color.prototype = {
        
constructorColor,
        
//parse a string to HSB
        
setColor: function(val) {
            
val val.toLowerCase();
            var 
that this;
            $.
each(CPGlobal.stringParsers, function(iparser) {
                var 
match parser.re.exec(val),
                        
values match && parser.parse(match),
                        
space parser.space || 'rgba';
                if (
values) {
                    if (
space === 'hsla') {
                        
that.value CPGlobal.RGBtoHSB.apply(nullCPGlobal.HSLtoRGB.apply(nullvalues));
                    } else {
                        
that.value CPGlobal.RGBtoHSB.apply(nullvalues);
                    }
                    return 
false;
                }
                return 
true;
            });
        },
        
setHue: function(h) {
            
this.value.h;
        },
        
setSaturation: function(s) {
            
this.value.s;
        },
        
setLightness: function(b) {
            
this.value.b;
        },
        
setAlpha: function(a) {
            
this.value.parseInt((a) * 10010) / 100;
        },
        
// HSBtoRGB from RaphaelJS
        // https://github.com/DmitryBaranovskiy/raphael/
        
toRGB: function(hsba) {
            if (!
h) {
                
this.value.h;
                
this.value.s;
                
this.value.b;
            }
            
*= 360;
            var 
RGBXC;
            
= (360) / 60;
            
s;
            
* (Math.abs(1));
            
C;

            
= ~~h;
            
+= [CX00XC][h];
            
+= [XCCX00][h];
            
+= [00XCCX][h];
            return {
                
rMath.round(255),
                
gMath.round(255),
                
bMath.round(255),
                
a|| this.value.a
            
};
        },
        
toHex: function(hsba) {
            var 
rgb this.toRGB(hsba);
            return 
'#' + ((<< 24) | (parseInt(rgb.r) << 16) | (parseInt(rgb.g) << 8) | parseInt(rgb.b)).toString(16).substr(1);
        },
        
toHSL: function(hsba) {
            if (!
h) {
                
this.value.h;
                
this.value.s;
                
this.value.b;
            }
            var 
h,
                    
= (s) * b,
                    
b;
            if (
&& <= 1) {
                
/= L;
            } else {
                
/= L;
            }
            
/= 2;
            if (
1) {
                
1;
            }
            return {
                
hH,
                
sS,
                
lL,
                
a|| this.value.a
            
};
        }
    };

    var 
_guid 0;

    
// Picker object

    
var Colorpicker = function(elementoptions) {
        
_guid++;
        
this.element = $(element).attr('data-colorpicker-guid'_guid);
        var 
format options.format || this.element.data('color-format') || 'hex';
        
this.format CPGlobal.translateFormats[format];
        
this.isInput this.element.is('input');
        
this.component this.element.is('.colorpicker-component') ? this.element.find('.add-on, .input-group-addon') : false;

        
this.picker = $(CPGlobal.template).attr('data-colorpicker-guid'_guid)
                .
appendTo('body')
                .
on('mousedown.colorpicker', $.proxy(this.mousedownthis));

        if (
this.isInput) {
            
this.element.on({
                
'focus.colorpicker': $.proxy(this.showthis),
                
'keyup.colorpicker': $.proxy(this.updatethis)
            });
        } else if (
this.component) {
            
this.component.on({
                
'click.colorpicker': $.proxy(this.showthis)
            });
        } else {
            
this.element.on({
                
'click.colorpicker': $.proxy(this.showthis)
            });
        }
        if (
format === 'rgba' || format === 'hsla') {
            
this.picker.addClass('alpha');
            
this.alpha this.picker.find('.colorpicker-alpha')[0].style;
        }

        if (
this.component) {
            
this.picker.find('.colorpicker-color').hide();
            
this.preview this.element.find('i')[0].style;
        } else {
            
this.preview this.picker.find('div:last')[0].style;
        }

        
this.base this.picker.find('div:first')[0].style;
        
this.update();

        $($.
proxy(function() {
            
this.element.trigger('create', [this]);
        }, 
this));
    };

    
Colorpicker.prototype = {
        
constructorColorpicker,
        
show: function(e) {
            
this.picker.show();
            
this.height this.component this.component.outerHeight() : this.element.outerHeight();
            
this.place();
            $(
window).on('resize.colorpicker', $.proxy(this.placethis));
            if (!
this.isInput) {
                if (
e) {
                    
e.stopPropagation();
                    
e.preventDefault();
                }
            }
            $(
document).on({
                
'mousedown.colorpicker': $.proxy(this.hidethis)
            });
            
this.element.trigger({
                
type'showPicker',
                
colorthis.color
            
});
        },
        
update: function() {
            var 
color this.isInput this.element.prop('value') : this.element.data('color');
            if (
typeof color === "undefined" || color === null) {
                
color '#ffffff';
            }
            
this.color = new Color(color);
            
this.picker.find('i')
                    .
eq(0).css({leftthis.color.value.100top100 this.color.value.100}).end()
                    .
eq(1).css('top'100 * (this.color.value.h)).end()
                    .
eq(2).css('top'100 * (this.color.value.a));
            
this.previewColor();
        },
        
hide: function() {
            
this.picker.hide();
            $(
window).off('resize'this.place);
            $(
document).off({
                
'mousedown'this.hide
            
});
            if (!
this.isInput) {
                if (
this.component) {
                    
//if the input value is empty, do not set any color
                    
if (this.element.find('input').val() !== '') {
                        
this.element.find('input').prop('value'this.format.call(this)).trigger('change');
                    }
                }
                
this.element.data('color'this.format.call(this));
            } else {
                
//if the input value is empty, do not set any color
                
if (this.element.val() !== '') {
                    
this.element.prop('value'this.format.call(this)).trigger('change');
                }
            }
            
this.element.trigger({
                
type'hidePicker',
                
colorthis.color
            
});
        },
        
place: function() {
            var 
offset this.component this.component.offset() : this.element.offset();
            
this.picker.css({
                
topoffset.top this.height,
                
leftoffset.left
            
});
        },
        
destroy: function() {
            $(
'.colorpicker[data-colorpicker-guid=' this.element.attr('data-colorpicker-guid') + ']').remove();
            
this.element.removeData('colorpicker').removeAttr('data-colorpicker-guid').off('.colorpicker');
            if (
this.component !== false) {
                
this.component.off('.colorpicker');
            }
            
this.element.trigger("destroy", [this]);
        },
        
setValue: function(value) {
            
// set the input or component value
            
if(this.isInput){
                
this.element.prop('value'value);
            }else{
                
this.element.find('input').val(value);
                
this.element.data('color'value);
            }
            
this.update();
            
this.element.trigger({
                
type'changeColor',
                
colorthis.color
            
});
        },
        
//preview color change
        
previewColor: function() {
            try {
                
this.preview.backgroundColor this.format.call(this);
            } catch (
e) {
                
this.preview.backgroundColor this.color.toHex();
            }
            
//set the color for brightness/saturation slider
            
this.base.backgroundColor this.color.toHex(this.color.value.h111);
            
//set te color for alpha slider
            
if (this.alpha) {
                
this.alpha.backgroundColor this.color.toHex();
            }
        },
        
pointernull,
        
slidernull,
        
mousedown: function(e) {
            
e.stopPropagation();
            
e.preventDefault();

            var 
target = $(e.target);

            
//detect the slider and set the limits and callbacks
            
var zone target.closest('div');
            if (!
zone.is('.colorpicker')) {
                if (
zone.is('.colorpicker-saturation')) {
                    
this.slider = $.extend({}, CPGlobal.sliders.saturation);
                }
                else if (
zone.is('.colorpicker-hue')) {
                    
this.slider = $.extend({}, CPGlobal.sliders.hue);
                }
                else if (
zone.is('.colorpicker-alpha')) {
                    
this.slider = $.extend({}, CPGlobal.sliders.alpha);
                } else {
                    return 
false;
                }
                var 
offset zone.offset();
                
//reference to knob's style
                
this.slider.knob zone.find('i')[0].style;
                
this.slider.left e.pageX offset.left;
                
this.slider.top e.pageY offset.top;
                
this.pointer = {
                    
lefte.pageX,
                    
tope.pageY
                
};
                
//trigger mousemove to move the knob to the current position
                
$(document).on({
                    
'mousemove.colorpicker': $.proxy(this.mousemovethis),
                    
'mouseup.colorpicker': $.proxy(this.mouseupthis)
                }).
trigger('mousemove');
            }
            return 
false;
        },
        
mousemove: function(e) {
            
e.stopPropagation();
            
e.preventDefault();
            var 
left Math.max(
                    
0,
                    
Math.min(
                    
this.slider.maxLeft,
                    
this.slider.left + ((e.pageX || this.pointer.left) - this.pointer.left)
                    )
                    );
            var 
top Math.max(
                    
0,
                    
Math.min(
                    
this.slider.maxTop,
                    
this.slider.top + ((e.pageY || this.pointer.top) - this.pointer.top)
                    )
                    );
            
this.slider.knob.left left 'px';
            
this.slider.knob.top top 'px';
            if (
this.slider.callLeft) {
                
this.color[this.slider.callLeft].call(this.colorleft 100);
            }
            if (
this.slider.callTop) {
                
this.color[this.slider.callTop].call(this.colortop 100);
            }
            
this.previewColor();

            
// Set input value on mousemove
            
if (!this.isInput) {
                try {
                    
this.element.find('input').val(this.format.call(this)).trigger('change');
                } catch (
e) {
                    
this.element.find('input').val(this.color.toHex()).trigger('change');
                }
            } else {
                try {
                    
this.element.val(this.format.call(this)).trigger('change');
                } catch (
e) {
                    
this.element.val(this.color.toHex()).trigger('change');
                }
            }

            
this.element.trigger({
                
type'changeColor',
                
colorthis.color
            
});
            return 
false;
        },
        
mouseup: function(e) {
            
e.stopPropagation();
            
e.preventDefault();
            $(
document).off({
                
mousemovethis.mousemove,
                
mouseupthis.mouseup
            
});
            return 
false;
        }
    };

    $.
fn.colorpicker = function(optionvalue) {
        return 
this.each(function() {
            var 
$this = $(this),
                    
data $this.data('colorpicker'),
                    
options typeof option === 'object' && option;
            if (!
data) {
                if (
option !== "destroy") {
                    
$this.data('colorpicker', (data = new Colorpicker(this, $.extend({}, $.fn.colorpicker.defaultsoptions))));
                }
            } else {
                if (
typeof option === 'string') {
                    
data[option](value);
                }
            }
        });
    };

    $.
fn.colorpicker.defaults = {
    };

    $.
fn.colorpicker.Constructor Colorpicker;

    var 
CPGlobal = {
        
// translate a format from Color object to a string
        
translateFormats: {
            
'rgb': function() {
                var 
rgb this.color.toRGB();
                return 
'rgb(' rgb.',' rgb.',' rgb.')';
            },
            
'rgba': function() {
                var 
rgb this.color.toRGB();
                return 
'rgba(' rgb.',' rgb.',' rgb.',' rgb.')';
            },
            
'hsl': function() {
                var 
hsl this.color.toHSL();
                return 
'hsl(' Math.round(hsl.360) + ',' Math.round(hsl.100) + '%,' Math.round(hsl.100) + '%)';
            },
            
'hsla': function() {
                var 
hsl this.color.toHSL();
                return 
'hsla(' Math.round(hsl.360) + ',' Math.round(hsl.100) + '%,' Math.round(hsl.100) + '%,' hsl.')';
            },
            
'hex': function() {
                return  
this.color.toHex();
            }
        },
        
sliders: {
            
saturation: {
                
maxLeft100,
                
maxTop100,
                
callLeft'setSaturation',
                
callTop'setLightness'
            
},
            
hue: {
                
maxLeft0,
                
maxTop100,
                
callLeftfalse,
                
callTop'setHue'
            
},
            
alpha: {
                
maxLeft0,
                
maxTop100,
                
callLeftfalse,
                
callTop'setAlpha'
            
}
        },
        
// HSBtoRGB from RaphaelJS
        // https://github.com/DmitryBaranovskiy/raphael/
        
RGBtoHSB: function(rgba) {
            
/= 255;
            
/= 255;
            
/= 255;

            var 
HSVC;
            
Math.max(rgb);
            
Math.min(rgb);
            
= (=== null :
                    
=== ? (b) / :
                    
=== ? (r) / :
                    (
g) / 4
                    
);
            
= ((360) % 6) * 60 360;
            
=== V;
            return {
h|| 1sSbVa|| 1};
        },
        
HueToRGB: function(pqh) {
            if (
0)
                
+= 1;
            else if (
1)
                
-= 1;

            if ((
6) < 1)
                return 
+ (p) * 6;
            else if ((
2) < 1)
                return 
q;
            else if ((
3) < 2)
                return 
+ (p) * ((3) - h) * 6;
            else
                return 
p;
        },
        
HSLtoRGB: function(hsla) {
            if (
0) {
                
0;
            }
            var 
q;
            if (
<= 0.5) {
                
* (s);
            } else {
                
- (s);
            }

            var 
q;

            var 
tr + (3);
            var 
tg h;
            var 
tb - (3);

            var 
Math.round(CPGlobal.HueToRGB(pqtr) * 255);
            var 
Math.round(CPGlobal.HueToRGB(pqtg) * 255);
            var 
Math.round(CPGlobal.HueToRGB(pqtb) * 255);
            return [
rgb|| 1];
        },
        
// a set of RE's that can match strings and generate color tuples.
        // from John Resig color plugin
        // https://github.com/jquery/jquery-color/
        
stringParsers: [
            {
                
re: /rgba?(s*(d{1,3})s*,s*(d{1,3})s*,s*(d{1,3})s*(?:,s*(d+(?:.d+)?)s*)?)/,
                
parse: function(execResult) {
                    return [
                        
execResult[1],
                        
execResult[2],
                        
execResult[3],
                        
execResult[4]
                    ];
                }
            },
            {
                
re: /rgba?(s*(d+(?:.d+)?)%s*,s*(d+(?:.d+)?)%s*,s*(d+(?:.d+)?)%s*(?:,s*(d+(?:.d+)?)s*)?)/,
                
parse: function(execResult) {
                    return [
                        
2.55 execResult[1],
                        
2.55 execResult[2],
                        
2.55 execResult[3],
                        
execResult[4]
                    ];
                }
            },
            {
                
re: /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/,
                
parse: function(execResult) {
                    return [
                        
parseInt(execResult[1], 16),
                        
parseInt(execResult[2], 16),
                        
parseInt(execResult[3], 16)
                    ];
                }
            },
            {
                
re: /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/,
                
parse: function(execResult) {
                    return [
                        
parseInt(execResult[1] + execResult[1], 16),
                        
parseInt(execResult[2] + execResult[2], 16),
                        
parseInt(execResult[3] + execResult[3], 16)
                    ];
                }
            },
            {
                
re: /hsla?(s*(d+(?:.d+)?)s*,s*(d+(?:.d+)?)%s*,s*(d+(?:.d+)?)%s*(?:,s*(d+(?:.d+)?)s*)?)/,
                
space'hsla',
                
parse: function(execResult) {
                    return [
                        
execResult[1] / 360,
                        
execResult[2] / 100,
                        
execResult[3] / 100,
                        
execResult[4]
                    ];
                }
            }
        ],
        
template'<div class="colorpicker dropdown-menu">' +
                
'<div class="colorpicker-saturation"><i><b></b></i></div>' +
                
'<div class="colorpicker-hue"><i></i></div>' +
                
'<div class="colorpicker-alpha"><i></i></div>' +
                
'<div class="colorpicker-color"><div /></div>' +
                
'</div>'
    
};

}(
window.jQuery);
?>
Онлайн: 0
Реклама