· 7 years ago · Mar 14, 2018, 04:28 AM
1// Extend the JQuery namespace with core utility methods for DOM manipulation.
2$.extend({
3
4 // Quick-create a dom element with attributes.
5 el : function(tagName, attributes) {
6 var el = document.createElement(tagName);
7 $(el).attr(attributes);
8 return el;
9 },
10
11 // See dc.View#setMode...
12 setMode : function(el, state, group) {
13 group = group || 'mode';
14 var re = new RegExp("\\w+_" + group + "(\\s|$)", 'g');
15 var mode = (state === null) ? "" : state + "_" + group;
16 var name = el.className.replace(re, '') + ' ' + mode;
17 name = name.replace(/\s\s/g, ' ');
18 el.className = name;
19 return mode;
20 },
21
22 // Align an element relative to a target element's coordinates. Forces the
23 // element to be absolutely positioned. Element must be visible.
24 // Position string format is: "top -right".
25 // You can pass an optional offset object with top and left offsets specified.
26 align : function(el, target, pos, offset) {
27 el = $(el);
28 target = $(target);
29 pos = pos || '';
30 offset = offset || {};
31 var scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0;
32 var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft || 0;
33 var clientWidth = document.documentElement.clientWidth;
34 var clientHeight = document.documentElement.clientHeight;
35
36 // var targPos = target.position();
37 var targOff = target.offset();
38 var b = {
39 left : targOff.left - scrollLeft,
40 top : targOff.top - scrollTop,
41 width : target.width(),
42 height : target.height()
43 };
44
45 var elb = {
46 width : el.width(),
47 height : el.height()
48 };
49
50 var left, top;
51
52 if (pos.indexOf('-left') >= 0) {
53 left = b.left;
54 } else if (pos.indexOf('left') >= 0) {
55 left = b.left - elb.width;
56 } else if (pos.indexOf('-right') >= 0) {
57 left = b.left + b.width - elb.width;
58 } else if (pos.indexOf('right') >= 0) {
59 left = b.left + b.width;
60 } else { // Centered.
61 left = b.left + (b.width - elb.width) / 2;
62 }
63
64 if (pos.indexOf('-top') >= 0) {
65 top = b.top;
66 } else if (pos.indexOf('top') >= 0) {
67 top = b.top - elb.height;
68 } else if (pos.indexOf('-bottom') >= 0) {
69 top = b.top + b.height - elb.height;
70 } else if (pos.indexOf('bottom') >= 0) {
71 top = b.top + b.height;
72 } else { // Centered.
73 top = b.top + (b.height - elb.height) / 2;
74 }
75
76 var constrain = (pos.indexOf('no-constraint') >= 0) ? false : true;
77
78 left += offset.left || 0;
79 top += offset.top || 0;
80
81 if (constrain) {
82 left = Math.max(scrollLeft, Math.min(left, scrollLeft + clientWidth - elb.width));
83 top = Math.max(scrollTop, Math.min(top, scrollTop + clientHeight - elb.height));
84 }
85
86 var offParent;
87 if (offParent = el.offsetParent()) {
88 left -= offParent.offset().left;
89 top -= offParent.offset().top;
90 }
91
92 $(el).css({position : 'absolute', left : left + 'px', top : top + 'px'});
93 return el;
94 },
95
96 // Javascript templating a-la ERB, pilfered from John Resig's
97 // "Secrets of the Javascript Ninja", page 83.
98 template : function(str, data) {
99 var fn = new Function('obj',
100 'var p=[],print=function(){p.push.apply(p,arguments);};' +
101 'with(obj){p.push(\'' +
102 str
103 .replace(/[\r\t\n]/g, " ")
104 .split("<%").join("\t")
105 .replace(/((^|%>)[^\t]*)'/g, "$1\r")
106 .replace(/\t=(.*?)%>/g, "',$1,'")
107 .split("\t").join("');")
108 .split("%>").join("p.push('")
109 .split("\r").join("\\'")
110 + "');}return p.join('');");
111 return data ? fn(data) : fn;
112 }
113
114});