squid.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*!
  2. * Squid 总结、积累、收集的一些基础方法
  3. * http://www.cnblogs.com/typeof/
  4. *
  5. * 所有plugins、swing所做的扩展都是基于Squid对象
  6. */
  7. (function(window) {
  8. //定义squid
  9. var Squid = function() {},
  10. slice = Array.prototype.slice,
  11. splice = Array.prototype.splice,
  12. trim = String.prototype.trim;
  13. //浅度拷贝
  14. Squid.extend = function() {
  15. var target = arguments[0],
  16. i = 1,
  17. length = arguments.length,
  18. options,
  19. prop;
  20. if(typeof target !== 'object') {
  21. target = {};
  22. }
  23. if(length === i) {
  24. target = this;
  25. --i;
  26. }
  27. for(; i < length; i++) {
  28. if((options = arguments[i]) != null) {
  29. for(prop in options) {
  30. target[prop] = options[prop];
  31. }
  32. }
  33. }
  34. return target;
  35. };
  36. //DOM 方法
  37. Squid.extend({
  38. getElementById: function(id) {
  39. var elem = document.getElementById(id);
  40. if(elem && elem.parentNode) {
  41. if(elem.id !== id) {
  42. elem = null;
  43. var elems = document.getElementsByTagName(id),
  44. i = 0,
  45. length = elems.length,
  46. cur;
  47. for(; i < length; i++) {
  48. cur = elems[i];
  49. if(cur.id === id) {
  50. elem = cur;
  51. break;
  52. }
  53. }
  54. }
  55. return elem;
  56. }
  57. },
  58. getElementsByTagName: function(tagName, context) {
  59. context = context || document;
  60. var elems = context.getElementsByTagName(tagName),
  61. r = [];
  62. try {
  63. r = slice.call(elems, 0);
  64. }catch(e) {
  65. var i = 0,
  66. length = elems.length,
  67. elem;
  68. for(; i < length; i++) {
  69. elem = elems[i];
  70. r.push(elem);
  71. }
  72. }
  73. return r;
  74. },
  75. getElementsByClassName: function(className, context) {
  76. //执行上下文,默认为document
  77. context = context || document;
  78. var r = [];
  79. if(context.getElementsByClassName) {
  80. var elems = context.getElementsByClassName(className);
  81. r = slice.call(elems, 0);
  82. }else{
  83. var elems,
  84. elem,
  85. i = 0,
  86. length;
  87. elems = context.getElementsByTagName('*');
  88. length = elems.length;
  89. for(; i < length; i++) {
  90. elem = elems[i];
  91. if(elem.className && (" " + elem.className + " ").replace(/[\t\n\r]/g, " ").indexOf(className) >= 0) {
  92. r.push(elem);
  93. }
  94. }
  95. }
  96. return r;
  97. },
  98. children: function(elem) {
  99. },
  100. first: function(elem) {
  101. return this.sibling(elem.firstChild)[0];
  102. },
  103. next: function(elem) {
  104. return this.nth(elem, 2, 'nextSibling');
  105. },
  106. siblings: function(elem) {
  107. return this.sibling(elem.parentNode.firstChild, elem);
  108. },
  109. nth: function(cur, r, dir) {
  110. r = r || 1;
  111. var i = 0;
  112. for(; cur; cur = cur[dir]) {
  113. if(cur.nodeType === 1 && ++i === r) {
  114. break;
  115. }
  116. }
  117. return cur;
  118. },
  119. sibling: function(n, elem) {
  120. var r = [];
  121. for(; n; n = n.nextSibling) {
  122. if(n.nodeType === 1 && n !== elem) {
  123. r.push(n);
  124. }
  125. }
  126. return r;
  127. },
  128. isVisible: function(elem) {
  129. return !this.isHidden(elem);
  130. },
  131. isHidden: function(elem) {
  132. if(elem) {
  133. var width = elem.offsetWidth,
  134. height = elem.offsetHeight;
  135. return (width === 0 && height === 0) || elem.style.display === 'none'
  136. }
  137. },
  138. getOffset: function(elem) {
  139. var body = document.body,
  140. docElem = document.documentElement;
  141. if('getBoundingClientRect' in document.documentElement) {
  142. var box;
  143. try {
  144. box = elem.getBoundingClientRect()
  145. }catch(e) {}
  146. if(!box || !elem.parentNode) {
  147. return box ? {top: box.top, left: box.left} : {top: 0, left: 0}
  148. }
  149. var win = window,
  150. clientTop = docElem.clientTop || body.clientTop || 0,
  151. clientLeft = docElem.clientLeft || body.clientLeft || 0,
  152. scrollTop = win.pageYOffset || docElem.scrollTop || body.scrollTop,
  153. scrollLeft = win.pageXOffset || docElem.scrollLeft || body.scrollLeft,
  154. top = box.top + scrollTop - clientTop,
  155. left = box.left + scrollLeft - clientLeft;
  156. return {top: top, left: left}
  157. }else{
  158. var offsetParent = elem.offsetParent,
  159. top = elem.offsetTop,
  160. left = elem.offsetLeft;
  161. while((elem = elem.parentNode) && elem !== body && elem !== docElem) {
  162. if(elem === offsetParent) {
  163. top = elem.offsetTop
  164. left = elem.offsetLeft
  165. offsetParent = elem.offsetParent
  166. }
  167. }
  168. return {top: top, left: left}
  169. }
  170. },
  171. position: function(elem) {
  172. if(!elem) {
  173. return null;
  174. }
  175. var offsetParent = elem.offsetParent,
  176. offset = this.getOffset(elem),
  177. parentOffset = this.getOffset(offsetParent);
  178. return {
  179. top: offset.top - parentOffset.top,
  180. left: offset.left - parentOffset.left
  181. };
  182. }
  183. });
  184. //全局缓存对象
  185. var cache = {};
  186. Squid.extend({
  187. guid: 1,
  188. uuid: 0,
  189. expando: 'squid' + (Math.random() + '').replace(/\D/g, ''),
  190. data: function(elem, name) {
  191. var internalKey = squid.expando,
  192. id,
  193. thisCache,
  194. r;
  195. id = elem[squid.expando];
  196. if(!id) {
  197. elem[squid.expando] = id = ++squid.uuid;
  198. }
  199. if(!cache[id]) {
  200. cache[id] = {};
  201. }
  202. if(typeof name === 'object' || typeof name === 'function') {
  203. cache[id][internalKey] = name;
  204. }
  205. thisCache = cache[id];
  206. if(!thisCache[internalKey]) {
  207. thisCache[internalKey] = {};
  208. }
  209. thisCache = thisCache[internalKey];
  210. if(name === 'events' && !thisCache[name]) {
  211. return thisCache[internalKey] && thisCache[internalKey].events;
  212. }
  213. if(typeof name === 'string') {
  214. r = thisCache[name];
  215. }else{
  216. r = thisCache;
  217. }
  218. return r;
  219. },
  220. removeData: function(elem, name) {
  221. var internalKey = squid.expando,
  222. id = elem[squid.expando],
  223. internalCache;
  224. if(!cache[id]) {
  225. return;
  226. }
  227. try{
  228. delete cache[id];
  229. }catch(e) {
  230. cache[id] = null;
  231. }
  232. if(elem.removeAttribute) {
  233. elem.removeAttribute(squid.expando);
  234. }else{
  235. elem[squid.expando] = null;
  236. }
  237. }
  238. });
  239. //事件处理
  240. Squid.event = {
  241. add: function(elem, type, handler) {
  242. var elemData,
  243. events,
  244. eventHandler,
  245. handleObj,
  246. handlers;
  247. if(elem.nodeType === 3 || elem.nodeType === 8) {
  248. return;
  249. }
  250. if(!handler) {
  251. return;
  252. }
  253. if(!handler.guid) {
  254. handler.guid = squid.guid++;
  255. }
  256. elemData = squid.data(elem);
  257. if(!elemData) {
  258. return;
  259. }
  260. events = elemData.events;
  261. eventHandler = elemData.handle;
  262. if(!events) {
  263. elemData.events = events = {};
  264. }
  265. if(!eventHandler) {
  266. elemData.handle = eventHandler = function(event) {
  267. return squid.event.handle.apply(eventHandler.elem, arguments);
  268. };
  269. eventHandler.elem = elem;
  270. }
  271. handleObj = {
  272. handler: handler
  273. };
  274. handleObj.type = type;
  275. if(!handleObj.guid) {
  276. handleObj.guid = handler.guid;
  277. }
  278. handlers = events[type];
  279. if(!handlers) {
  280. handlers = events[type] = [];
  281. if(elem.addEventListener) {
  282. elem.addEventListener(type, eventHandler, false);
  283. }else if(elem.attachEvent){
  284. elem.attachEvent('on' + type, eventHandler);
  285. }
  286. }
  287. handlers.push(handleObj);
  288. },
  289. remove: function(elem, type, handler) {
  290. var elemData,
  291. events,
  292. eventType,
  293. i = 0,
  294. handleObj;
  295. if(elem.nodeType === 3 || elem.nodeType === 8) {
  296. return;
  297. }
  298. elemData = squid.data(elem);
  299. events = elemData.events;
  300. if(!events) {
  301. return;
  302. }
  303. eventType = events[type] || [];
  304. if(!handler) {
  305. for(; i < eventType.length; i++) {
  306. handleObj = eventType[i];
  307. squid.event.remove(elem, type, handleObj.handler);
  308. eventType.splice(i--, 1);
  309. }
  310. }
  311. if(eventType.length === 0) {
  312. if(elem.removeEventListener) {
  313. elem.removeEventListener(type, elemData.handle, false);
  314. }else if(elem.detachEvent) {
  315. elem.detachEvent('on' + type, elemData.handle);
  316. }
  317. }
  318. squid.removeData(elem);
  319. },
  320. handle: function(event) {
  321. var i = 0,
  322. length,
  323. handlers,
  324. handleObj,
  325. args = Array.prototype.slice.call(arguments, 0);
  326. event = squid.event.fix(event);
  327. handlers = ((squid.data(this, 'events') || {})[event.type] || []).slice(0);
  328. args[0] = event;
  329. length = handlers.length;
  330. for(; i < length; i++) {
  331. handleObj = handlers[i];
  332. event.handler = handleObj.handler;
  333. event.handleObj = handleObj;
  334. handleObj.handler.apply(this, args);
  335. }
  336. },
  337. fix: function(event) {
  338. if(!event.target) {
  339. event.target = event.srcElement || document;
  340. }
  341. if(event.target.nodeType === 3) {
  342. event.target = event.target.parentNode;
  343. }
  344. if(!event.relateTarget && event.fromElement) {
  345. event.relateTarget = event.fromElement === event.target ? event.toElement : event.fromElement;
  346. }
  347. if(!event.preventDefault) {
  348. event.preventDefault = function() {
  349. event.returnValue = false;
  350. };
  351. }
  352. if(!event.stopPropagation) {
  353. event.stopPropagation = function() {
  354. event.cancelBubble = true;
  355. };
  356. }
  357. if(event.pageX == null) {
  358. var eventDoc = event.target.ownerDocument || document,
  359. doc = eventDoc.documentElment,
  360. body = eventDoc.body;
  361. event.pageX = event.clientX + (doc && document.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
  362. event.pageY = event.clientY + (doc && document.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
  363. }
  364. return event;
  365. }
  366. };
  367. //事件处理
  368. Squid.extend({
  369. on: function(elem, type, handler) {
  370. return this.event.add(elem, type, handler);
  371. },
  372. off: function(elem, type, handler) {
  373. return this.event.remove(elem, type, handler);
  374. },
  375. //把函数放到特定上下文执行
  376. proxy: function(fn, context) {
  377. var slice = Array.prototype.slice,
  378. args = slice.call(arguments, 2),
  379. proxy = function() {
  380. return fn.apply(context, args.concat(slice.call(arguments)));
  381. };
  382. proxy.guid = fn.guid = fn.guid || proxy.guid || squid.guid++;
  383. return proxy;
  384. }
  385. });
  386. var fnThrottleId = 0;
  387. //工具函数
  388. Squid.extend({
  389. trim: trim ? function(text) {
  390. return trim.call(text);
  391. } : function(text) {
  392. return text.toString().replace(/^\s+/, '').replace(/\s+$/, '');
  393. },
  394. throttle: function(fn, delay, context) {
  395. delay = delay || 100;
  396. context = context || null;
  397. return function() {
  398. var args = arguments;
  399. clearTimeout(fnThrottleId);
  400. fnThrottleId = setTimeout(function() {
  401. fn.apply(context, args);
  402. }, delay);
  403. };
  404. },
  405. isEmpty: function(obj) {
  406. for(var prop in obj) {
  407. return false;
  408. }
  409. return true;
  410. }
  411. });
  412. //基于Squid扩展的插件
  413. Squid.plugin = function() {};
  414. //解决浏览器对默认组件渲染不一样
  415. Squid.swing = function() {};
  416. window.squid = Squid;
  417. })(window);