/* sky.jquery.js 2013-03-27 ~ Seo, Jaehan SkyJS integrated jQuery Remark : Not compatible to SkyJS. */ // Class ////////////////////////////////////////////////////////////////////// var Class = function() { var obj = function() { if (this.init) this.init.apply(this, arguments); } if (arguments[0]) jQuery.extend(obj.prototype, arguments[0]); return obj; }; // Extend jQuery /////////////////////////////////////////////////////////////// (function($) { // Event $.eventStop = function(event) { event.preventDefault ? event.preventDefault() : event.returnValue = false; event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true; } $.eventOffset = function(event) { return { x : event.pageX, y : event.pageY } } // Element $.fn.visible = function() { return !($(this).css('display') == 'none'); } $.fn.setClass = function(className) { $(this).attr('class', className); return $(this); } $.fn.getSize = function() { return { width : $(this).width(), height : $(this).height() } } $.fn.setSize = function(width, height) { if (arguments.length == 1) { $(this).width(arguments[0].width); $(this).height(arguments[0].height); } else { $(this).width(width); $(this).height(height); } return $(this); } $.fn.getRect = function() { var p = $(this).offset(); var s = $(this).getSize(); return { left:p.left, top:p.top, right:p.left+s.width, bottom:p.top+s.height }; } $.fn.getCenter = function(x, y) { var objPos = $(this).offset(); var objSize = $(this).getSize(); var docSize = Util.getClientSize(); var docScroll = Util.getScrollOffset(); var x = (typeof x == 'number') ? x : (x === false ? objPos.left : docScroll.x+(Math.abs(docSize.width-objSize.width))/2); var y = (typeof y == 'number') ? y : (y === false ? objPos.top : docScroll.y+(Math.abs(docSize.height-objSize.height))/2); return { left:x, top:y, x:x, y:y }; } $.fn.setCenter = function(x, y) { $(this).offset($(this).getCenter(x, y)); } $.fn.hitTest = function(target) { var o = $(this).getRect(); var t = $(target).getRect(); return !(o.left > t.right || o.right < t.left || o.top > t.bottom || o.bottom < t.top); } $.fn.inTest = function(target) { var o = $(this).getRect(); var t = $(target).getRect(); return (o.left >= t.left && o.right <= t.right && o.top >= t.top && o.bottom <= t.bottom); } })(jQuery); // Extend Function //////////////////////////////////////////////////////////// jQuery.extend(Function.prototype, { bind : function(obj) { var func = this; var arg = jQuery.makeArray(arguments); arg.shift(); return function() { return func.apply(obj, arg.concat(jQuery.makeArray(arguments))); } }, bindForEvent : function(obj) { var func = this; return function(e) { var el = this; return func.call(obj, e, el); } } }); /* jQuery확장 함수 get방식의 querystring값을 반환한다. var val = jQuery.getQueryString("key", "디폴트값"); */ if ( typeof jQuery.getQueryString !== 'function' ) { jQuery.extend({ getQueryString: function( key, default_ ) { if (default_==null) default_= ""; key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regex = new RegExp("[\\?&]"+key+"=([^&#]*)"); var qs = regex.exec(window.location.href); if(qs == null) return default_; else return qs[1]; } }); } // Extend Array /////////////////////////////////////////////////////////////// jQuery.extend(Array.prototype, { exists : function(value) { return !!(this.search(value) > -1); }, search : function(value) { return jQuery.inArray(value, this); }, filter : function(func, applyResult) { if (typeof func != 'boolean' || typeof func != 'function') { return this; } var ret = []; if (typeof func == 'boolean') { applyResult = false; } this.each(function(value, i) { var result = (typeof func == 'boolean') ? (!value == !func) : func(value, i); if (applyResult) { if (result !== false) ret.push(result); } else { if (result) ret.push(value); } }); }, each : function(func) { if (typeof func == 'function') { for (var i=0; i]+>/gi, ''); }, validHTML : function() { return jQuery('
').html(this).html(); }, escapeHTML: function() { return jQuery('
').text(this).html(); }, unescapeHTML: function() { return jQuery('
').html(this.stripTags()).text(); }, decodeQuery : function(isUrl) { if (isUrl) { var tmp = this.substring(this.indexOf('?')+1).match(/^\??(.*)$/)[1].split('&'); } else { var tmp = this.match(/^\??(.*)$/)[1].split('&'); } var ret = {}; for (var i=0; i 15) { cipher = 15; } var min = parseInt('100000000000000'.substr(0, cipher), 10); var max = parseInt('999999999999999'.substr(0, cipher), 10); return (prefix) ? prefix+'_'+Util.random(min, max) : Util.random(min, max); }, // cookie getCookie : function(name) { var re = new RegExp(name + '=([^;]+)'); var value = re.exec(document.cookie); return (value != null) ? unescape(value[1]) : null; }, setCookie : function(name, value, expire, path, domain) { expire = parseInt(expire, 10); if (!path) path = '/'; if (expire) { var today = new Date(); var expiry = new Date(today.getTime() + expire * 1000); var cookie = name + '=' + escape(value) + '; expires=' + expiry.toGMTString() + '; path=' + path; } else { var cookie = name + '=' + escape(value) + '; path=' + path; } if (domain) cookie += '; domain=' + domain; document.cookie = cookie; }, // sessionStorage getStorage : function(name) { try { return sessionStorage.getItem(name); } catch (e) { return false; } }, setStorage : function(name, value) { try { return sessionStorage.setItem(name, value); } catch (e) { return false; } }, clearStorage : function() { try { return sessionStorage.clear(); } catch (e) { return false; } }, // link style sheet linkStyle : function(href, id) { if (id) { var link = jQuery('#' + id); if (!link.length) { link = jQuery(''); jQuery('head:first').append(link); } else { link.attr('href', href); } } else { link = jQuery(''); jQuery('head:first').append(link); } return link.get(0); }, // js path getJSPath : function(jsFile) { if (!jsFile) jsFile = 'sky.jquery.js'; return jQuery('script[src*="' + jsFile + '"]').attr('src').replace(/\/[^\/]+$/, '') || false; }, // rgb() to #hex rgbToHex : function(str) { str = str.replace(/[^0-9,]/g, '').split(','); str[0] = ('0' + parseInt(str[0], 10).toString(16).toLowerCase()).slice(-2); str[1] = ('0' + parseInt(str[1], 10).toString(16).toLowerCase()).slice(-2); str[2] = ('0' + parseInt(str[2], 10).toString(16).toLowerCase()).slice(-2); return ('#' + str.join('')); }, // debug dump : function(obj) { if (obj.toString && (typeof obj == 'string' || typeof obj == 'number' || obj instanceof Array)) { return obj.toString(); } else { var str = ''; for (x in obj) { str += x + ' : ' + obj[x] + "\n"; } return str; } } } Util.linkCSS = Util.linkStyle; // Common Function //////////////////////////////////////////////////////////// function $id(id) { if (jQuery.isArray(id)) { return jQuery(id).get(); } else { return (typeof id == 'string') ? document.getElementById(id) : id; } } function $v(obj) { var returnArray = false; var value = []; jQuery(obj).each(function() { switch (this.type.toLowerCase()) { case 'radio' : if (this.checked) { value.push(this.value); } break; case 'checkbox' : returnArray = true; if (this.checked) { value.push(this.value); } break; case 'select-multiple' : returnArray = true; value.merge(jQuery(this).val()); break; default : value.push(jQuery(this).val()); break; } }); if (returnArray || value.length > 1) { return value; } else { return value[0] || ''; } } function $vset(obj, value) { jQuery(obj).each(function() { var this_el = this; switch (this.type.toLowerCase()) { case 'radio' : if (this.value == value) { this.checked = true; if ( jQuery(this).checkboxradio ) { jQuery(this).checkboxradio("refresh"); } } break; case 'checkbox' : value = (value instanceof Array) ? value : [value]; jQuery(value).each(function(){ if (jQuery(this_el.form.elements[this_el.name]).attr('bitwise') != null) { if (parseInt(this_el.value, 10) & parseInt(this, 10)) { this_el.checked = true; } } else { if (this_el.value == this) { this_el.checked = true; } } if ( jQuery(this_el).checkboxradio ) { jQuery(this_el).checkboxradio("refresh"); } }); break; case 'select-one' : jQuery(this).find("option").each(function() { if (this.value == value) this.selected = true; }); if ( jQuery(this).selectmenu ) { jQuery(this).selectmenu("refresh"); } break; case 'select-multiple' : value = (value instanceof Array) ? value : [value]; jQuery(value).each(function(v) { jQuery(this_el).find("option").each(function() { if (this.value == v) this.selected = true; }); if ( jQuery(this).selectmenu ) { jQuery(this).selectmenu("refresh"); } }); break; default : jQuery(this).val(value); break; } }); }