-
Notifications
You must be signed in to change notification settings - Fork 1
/
widget.js
71 lines (71 loc) · 2.46 KB
/
widget.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(function (win, doc, arg) {
var $ = win[arg.k] = {
'arg': arg, 'doc': doc, 'win': win, 'struc': {},
'func': (function () {
return {
close: function () {
$.struc.body.parentNode.removeChild($.struc.body);
},
listen: function (el, ev, fn) {
if (typeof $.win.addEventListener !== 'undefined') {
el.addEventListener(ev, fn, false);
} else if (typeof $.win.attachEvent !== 'undefined') {
el.attachEvent('on' + ev, fn);
}
},
behavior: function () {
$.func.listen($.struc.x, 'click', $.func.close);
},
presentation: function () {
var rules = $.arg.rules.join('\n'),
css = $.doc.createElement('STYLE');
css.type = 'text/css';
rules = rules.replace(/#_/g, '#' + $.arg.root + '_');
if (css.styleSheet) {
css.styleSheet.cssText = rules;
} else {
css.appendChild($.doc.createTextNode(rules));
}
$.doc.head.appendChild(css);
$.func.behavior();
},
structure: function (script) {
$.arg.config = script.getAttribute('data-config');
$.struc.body = $.doc.createElement('DIV');
$.struc.body.id = $.arg.root + '_body';
var span = $.doc.createElement('SPAN');
span.innerHTML = $.arg.config;
$.struc.body.appendChild(span);
$.struc.x = $.doc.createElement('A');
$.struc.x.id = $.arg.root + '_x';
$.struc.x.innerHTML = 'x';
$.struc.body.appendChild($.struc.x);
script.parentNode.insertBefore($.struc.body, script);
script.parentNode.removeChild(script);
$.func.presentation();
},
init: function () {
$.doc.head = $.doc.getElementsByTagName('HEAD')[0];
var script = $.doc.getElementsByTagName('SCRIPT'),
n = script.length, i;
for (i = 0; i < n; i = i + 1) {
if (script[i].src.match($.arg.src)) {
$.func.structure(script[i]);
break;
}
}
}
};
}())
};
$.func.init();
}(window, document, {
'root': '_' + new Date().getTime(),
'src': /widget.js$/,
'rules': [
'#_body { padding: 20px 40px; position: absolute; top: 0; right: 0;',
'background: #f00; color: #fff; }',
'#_x { position: absolute; top: 2px; right: 10px; color: #ff0;',
'cursor: pointer; }'
]
} ));