-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscMap_jQuery.js
49 lines (49 loc) · 1.55 KB
/
scMap_jQuery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var siteCatalyst = {
init: function(el) {
if (typeof scMap != "undefined") {
$el = !el ? $('body') : $(el);
$('*[data-sc-item]:not(form)',$el).each($.proxy(function(idx,el) {
this.attachLinkEvents(el);
},this));
$('form[data-sc-item]',$el).each($.proxy(function(idx,el) {
this.attachFormSubmit($(el));
},this));
}
},
attachLinkEvents: function(el) {
ev = $(el).data('sc-event') ? $(el).data('sc-event') : 'click';
$(el).bind(ev,function(e) {
link = $(el).data('sc-url') ? $(el).data('sc-url') : e.target;
mapItem = $(el).data('sc-item');
for (prop in scMap[mapItem]){
s[prop] = siteCatalyst.replaceWildCards(scMap[mapItem][prop], el);
}
if (scMap[mapItem]) s.tl(link,scMap[mapItem]["type"],s['linkTrackName']);
});
},
attachFormSubmit: function(el) {
$(el).bind('submit',function(e) {
link = $(el).attr('action');
mapItem = $(el).data('sc-item');
for (prop in scMap[mapItem]) {
s[prop] = siteCatalyst.replaceWildCards(scMap[mapItem][prop], el);
}
if (scMap[mapItem]) s.tl(link,scMap[mapItem]["type"],s['linkTrackName']);
});
},
replaceWildCards: function(str,el) {
pattern = new RegExp(/{(.*?)}/g);
if (pattern.test(str)) {
wildcards = str.match(pattern);
for (i=0; i < wildcards.length; i++){
atr = (/{(.*?)}/).exec(wildcards[i])[1];
if ($(el).get(0).nodeName.toLowerCase() != 'form') {
str = str.replace(wildcards[i],$(el).attr(atr));
} else {
str = str.replace('{'+ atr +'}',$('*[name="'+ atr +'"]',el).val());
}
}
}
return str.replace(/\s+/g, '-');
}
};