CGI-WebToolkit

 view release on metacpan or  search on metacpan

t/private/javascripts/jscolor.js  view on Meta::CPAN

/**
 * jscolor, JavaScript Color Picker
 *
 * @version 1.2.3
 * @license http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
 * @author  Honza Odvarko <honza@odvarko.cz>
 * @created 2008-06-15
 * @updated 2009-02-25
 * @link    http://jscolor.com
 */


var jscolor = {


	dir : '', // location of jscolor directory (leave empty to autodetect)
	bindClass : 'color', // class name
	binding : true, // automatic binding via <input class="...">
	preloading : true, // use image preloading?


	install : function() {
		jscolor.addEvent(window, 'load', jscolor.init)
	},


	init : function() {
		if(jscolor.binding) {
			jscolor.bind()
		}
		if(jscolor.preloading) {
			jscolor.preload()
		}
	},


	getDir : function() {
		if(!jscolor.dir) {
			var detected = jscolor.detectDir()
			jscolor.dir = detected!=false ? detected : 'jscolor/'
		}
		return jscolor.dir
	},


	detectDir : function() {
		var base = location.href

		var e = document.getElementsByTagName('base')
		for(var i=0; i<e.length; i++) {
			if(e[i].href) base = e[i].href
		}

		var e = document.getElementsByTagName('script')
		for(var i=0; i<e.length; i++) {
			if(e[i].src && /(^|\/)jscolor\.js([?#].*)?$/i.test(e[i].src)) {
				var src = new jscolor.URI(e[i].src)
				var srcAbs = src.toAbsolute(base)
				srcAbs.path = srcAbs.path.replace(/[^\/]+$/, '') // remove filename
				delete srcAbs.query
				delete srcAbs.fragment
				return srcAbs.toString()
			}
		}
		return false
	},


	bind : function() {
		var matchClass = new RegExp('(^|\\s)('+jscolor.bindClass+')\\s*(\\{[^}]*\\})?', 'i')
		var e = document.getElementsByTagName('input')
		for(var i=0; i<e.length; i++) {
			var m
			if(!e[i].color && e[i].className && (m = e[i].className.match(matchClass))) {
				var prop = {}
				if(m[3]) {
					try {
						eval('prop='+m[3])
					} catch(eInvalidProp) {}
				}
				e[i].color = new jscolor.color(e[i], prop)
			}
		}
	},


	preload : function() {
		for(var fn in jscolor.imgRequire) {
			jscolor.loadImage(fn)
		}
	},


	images : {
		pad : [ 181, 101 ],
		sld : [ 16, 101 ],
		cross : [ 15, 15 ],
		arrow : [ 7, 11 ]
	},


	imgRequire : {},
	imgLoaded : {},


	requireImage : function(filename) {
		jscolor.imgRequire[filename] = true
	},


	loadImage : function(filename) {
		if(!jscolor.imgLoaded[filename]) {
			jscolor.imgLoaded[filename] = new Image()
			jscolor.imgLoaded[filename].src = jscolor.getDir()+filename
		}
	},


	fetchElement : function(mixed) {
		return typeof(mixed) == 'string' ? document.getElementById(mixed) : mixed
	},

t/private/javascripts/jscolor.js  view on Meta::CPAN

			} while(e1 = e1.offsetParent)
		}
		while((e2 = e2.parentNode) && e2.nodeName != 'BODY') {
			x -= e2.scrollLeft
			y -= e2.scrollTop
		}
		return [x, y]
	},


	getElementSize : function(e) {
		return [e.offsetWidth, e.offsetHeight]
	},


	getMousePos : function(e) {
		if(!e) e = window.event
		if(typeof e.pageX == 'number') {
			return [e.pageX, e.pageY]
		} else if(typeof e.clientX == 'number') {
			return [
				e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
				e.clientY + document.body.scrollTop + document.documentElement.scrollTop
			]
		}
	},


	getViewPos : function() {
		if(typeof window.pageYOffset == 'number') {
			return [window.pageXOffset, window.pageYOffset]
		} else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
			return [document.body.scrollLeft, document.body.scrollTop]
		} else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
			return [document.documentElement.scrollLeft, document.documentElement.scrollTop]
		} else {
			return [0, 0]
		}
	},


	getViewSize : function() {
		if(typeof window.innerWidth == 'number') {
			return [window.innerWidth, window.innerHeight]
		} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
			return [document.body.clientWidth, document.body.clientHeight]
		} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			return [document.documentElement.clientWidth, document.documentElement.clientHeight]
		} else {
			return [0, 0]
		}
	},


	URI : function(uri) { // See RFC3986

		this.scheme = null
		this.authority = null
		this.path = ''
		this.query = null
		this.fragment = null

		this.parse = function(uri) {
			var m = uri.match(/^(([A-Za-z][0-9A-Za-z+.-]*)(:))?((\/\/)([^\/?#]*))?([^?#]*)((\?)([^#]*))?((#)(.*))?/)
			this.scheme = m[3] ? m[2] : null
			this.authority = m[5] ? m[6] : null
			this.path = m[7]
			this.query = m[9] ? m[10] : null
			this.fragment = m[12] ? m[13] : null
			return this
		}

		this.toString = function() {
			var result = ''
			if(this.scheme != null) result = result + this.scheme + ':'
			if(this.authority != null) result = result + '//' + this.authority
			if(this.path != null) result = result + this.path
			if(this.query != null) result = result + '?' + this.query
			if(this.fragment != null) result = result + '#' + this.fragment
			return result
		}

		this.toAbsolute = function(base) {
			var base = new jscolor.URI(base)
			var r = this
			var t = new jscolor.URI

			if(base.scheme == null) return false

			if(r.scheme != null && r.scheme.toLowerCase() == base.scheme.toLowerCase()) {
				r.scheme = null
			}

			if(r.scheme != null) {
				t.scheme = r.scheme
				t.authority = r.authority
				t.path = removeDotSegments(r.path)
				t.query = r.query
			} else {
				if(r.authority != null) {
					t.authority = r.authority
					t.path = removeDotSegments(r.path)
					t.query = r.query
				} else {
					if(r.path == '') {
						t.path = base.path
						if(r.query != null) {
							t.query = r.query
						} else {
							t.query = base.query
						}
					} else {
						if(r.path.substr(0,1) == '/') {
							t.path = removeDotSegments(r.path)
						} else {
							if(base.authority != null && base.path == '') {
								t.path = '/'+r.path
							} else {
								t.path = base.path.replace(/[^\/]+$/,'')+r.path
							}
							t.path = removeDotSegments(t.path)
						}
						t.query = r.query
					}
					t.authority = base.authority
				}
				t.scheme = base.scheme
			}
			t.fragment = r.fragment

			return t
		}

		function removeDotSegments(path) {
			var out = ''
			while(path) {
				if(path.substr(0,3)=='../' || path.substr(0,2)=='./') {
					path = path.replace(/^\.+/,'').substr(1)
				} else if(path.substr(0,3)=='/./' || path=='/.') {
					path = '/'+path.substr(3)
				} else if(path.substr(0,4)=='/../' || path=='/..') {
					path = '/'+path.substr(4)
					out = out.replace(/\/?[^\/]*$/, '')
				} else if(path=='.' || path=='..') {
					path = ''
				} else {
					var rm = path.match(/^\/?[^\/]*/)[0]
					path = path.substr(rm.length)
					out = out + rm
				}
			}
			return out
		}

		if(uri) {
			this.parse(uri)
		}

	},


	/*
	 * Usage example:
	 * var myColor = new jscolor.color(myInputElement)
	 */

	color : function(target, prop) {


		this.required = true // refuse empty values?
		this.adjust = true // adjust value to uniform notation?
		this.hash = false // prefix color with # symbol?
		this.caps = true // uppercase?
		this.valueElement = target // value holder
		this.styleElement = target // where to reflect current color
		this.hsv = [0, 0, 1] // read-only  0-6, 0-1, 0-1
		this.rgb = [1, 1, 1] // read-only  0-1, 0-1, 0-1

		this.pickerOnfocus = true // display picker on focus?
		this.pickerMode = 'HSV' // HSV | HVS
		this.pickerPosition = 'bottom' // left | right | top | bottom
		this.pickerFace = 10 // px
		this.pickerFaceColor = 'ThreeDFace' // CSS color
		this.pickerBorder = 1 // px
		this.pickerBorderColor = 'none' //'ThreeDHighlight ThreeDShadow ThreeDShadow ThreeDHighlight' // CSS color
		this.pickerInset = 1 // px
		this.pickerInsetColor = 'none' //'ThreeDShadow ThreeDHighlight ThreeDHighlight ThreeDShadow' // CSS color
		this.pickerZIndex = 10000



( run in 0.595 second using v1.01-cache-2.11-cpan-364913b4093 )