1/** 2 * Core 3 * @package Cornerstone 4 * @author Archetyped 5 */ 6 7var CNR = {}; 8 9(function($){ 10/* Quick Hide */ 11 12$('html').addClass('js'); 13 14/* Classes */ 15 16/* CNR */ 17CNR = { 18 prefix: 'cnr', 19 context: [], //Context 20 options: { //Options 21 }, 22 23 extend: function(member, data) { 24 if ( $.type(member) == 'string' && $.isPlainObject(data) ) { 25 //Add initial member 26 var obj = {}; 27 obj[member] = $.extend({}, data); 28 $.extend(this, obj); 29 30 if ( member in this ) { 31 //Add additional objects 32 var args = ( arguments.length > 2 ) ? [].slice.apply(arguments).slice(2) : []; 33 args.unshift(this[member]); 34 //Add base properties 35 args.push({ 36 base: CNR, 37 parent: this, 38 extend: this.extend 39 }); 40 $.extend.apply($, args); 41 } 42 } 43 }, 44 45 /* Prefix */ 46 47 /** 48 * Retrieve valid separator 49 * If supplied argument is not a valid separator, use default separator 50 * @param string (optional) sep Separator text 51 * @return string Separator text 52 */ 53 get_sep: function(sep) { 54 if ( typeof sep == 'undefined' || sep == null ) 55 sep = ''; 56 57 return ( $.type(sep) == 'string' ) ? sep : '_'; 58 }, 59 60 /** 61 * Retrieve prefix 62 * @param string (optional) sep Separator text 63 * @return string Prefix (with separator if specified) 64 */ 65 get_prefix: function(sep) { 66 return ( this.prefix && this.prefix.length ) ? this.prefix + this.get_sep(sep) : ''; 67 }, 68 69 /** 70 * Check if string is prefixed 71 */ 72 has_prefix: function(val, sep) { 73 return ( $.type(val) == 'string' && val.length && val.indexOf(this.get_prefix(sep)) === 0 ); 74 }, 75 76 /** 77 * Add Prefix to value 78 * @param string val Value to add prefix to 79 * @param string sep (optional) Separator (Default: `_`) 80 * @param bool (optional) once If text should only be prefixed once (Default: true) 81 */ 82 add_prefix: function(val, sep, once) { 83 if ( typeof sep == 'undefined' ) 84 sep = '_'; 85 once = ( typeof once == 'undefined' ) ? true : !!once; 86 if ( once && this.has_prefix(val, sep) ) 87 return val; 88 return this.get_prefix(sep) + val; 89 } 90} 91 92/* Utilities */ 93CNR.extend('util', { 94 /** 95 * Return formatted string 96 */ 97 sprintf: function() { 98 var format = '', 99 params = []; 100 if (arguments.length < 1) 101 return format; 102 if (arguments.length == 1) { 103 format = arguments[0]; 104 return format; 105 } 106 params = arguments.slice(1); 107 return format; 108 }, 109 110 /* Request */ 111 112 /** 113 * Retrieve valid context 114 * @return array Context 115 */ 116 get_context: function() { 117 //Valid context 118 if ( !$.isArray(this.base.context) ) 119 this.base.context = []; 120 //Return context 121 return this.base.context; 122 }, 123 124 /** 125 * Check if a context exists in current request 126 * If multiple contexts are supplied, result will be TRUE if at least ONE context exists 127 * 128 * @param string|array ctx Context to check for 129 * @return bool TRUE if context exists, FALSE otherwise 130 */ 131 is_context: function(ctx) { 132 var ret = false; 133 //Validate context 134 if ( typeof ctx == 'string' ) 135 ctx = [ctx]; 136 if ( $.isArray(ctx) && this.arr_intersect(this.get_context(), ctx).length ) { 137 ret = true; 138 } 139 return ret; 140 }, 141 142 /* DOM */ 143 144 /** 145 * Strip selector (ID, class, etc.) 146 * @param string id_value Original element 147 */ 148 esc_selector: function(id_value) { 149 return id_value.toString().replace(/(\[|\])/gi, '\\$1'); 150 }, 151 152 /** 153 * Find common elements of 2 arrays 154 * @param array arr1 First array 155 * @param array arr2 Second array 156 * @return array Elements common to both arrays 157 */ 158 arr_intersect: function(arr1, arr2) { 159 var ret = []; 160 if ( arr1 == arr2 ) { 161 return arr2; 162 } 163 if ( !$.isArray(arr2) || !arr2.length || !arr1.length ) { 164 return ret; 165 } 166 //Compare elements in arrays 167 var a1; 168 var a2; 169 var val; 170 if ( arr1.length < arr2.length ) { 171 a1 = arr1; 172 a2 = arr2; 173 } else { 174 a1 = arr2; 175 a2 = arr1; 176 } 177 178 for ( var x = 0; x < a1.length; x++ ) { 179 //Add mutual elements into intersection array 180 val = a1[x]; 181 if ( a2.indexOf(val) != -1 && ret.indexOf(val) == -1 )
182 ret.push(val); 183 } 184 185 //Return intersection results 186 return ret; 187 } 188}); 189 190})(jQuery); //END CNR init
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.