-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_si.js
156 lines (134 loc) · 3.77 KB
/
_si.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// GPLv2 License - Copyright (c) 2014 Dave Martin
_si.survey = function () {
var d = document, h = d.getElementsByTagName('head')[0],
id = '_si';
function addCss () {
var c = d.createElement( 'link' );
c.rel = 'stylesheet';
c.type = 'text/css';
c.href = '_si/_si.css';
c.media = 'all';
h.appendChild( c );
}
function addSurvey() {
var s = d.createElement( 'div' );
s.id = id;
s.innerHTML = '<form id="_si-form" name="_si-form" method="post">' +
'<span>' + encodeHTML(_si.settings.question) + '</span>' +
addSurveyField() +
'<input type="submit" value="Send" />' +
'<a href="#" onclick="javascript:_si.survey.hide();">hide</a>' +
'</form>';
d.body.appendChild( s );
setTimeout(function () {
d.getElementById( id ).setAttribute("class", "active");
}, 500);
document.forms["_si-form"].onsubmit = function(){
// Submit form
var url = "_si/process.php",
fields = serializeFields(),
http = new XMLHttpRequest();
http.open("POST", url, true);
http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
http.send(fields);
// Show thanks Message
document.getElementById('_si-form').innerHTML = '<div class="thanks">Thank you!</div>';
// Hide survey after 2 seconds
setTimeout(function () {
toggleSurvey( false );
}, 2000);
return false;
}
}
function addSurveyField() {
switch(_si.settings.type) {
case 'checkbox':
return radioCheckboxBuilder('checkbox');
break;
case 'radio':
return radioCheckboxBuilder('radio');
break;
case 'textarea':
return '<textarea id="si_q" name="si_q"></textarea>';
break;
default:
return '<input type="text" id="si_q" name="si_q" />';
}
}
function encodeHTML(str) {
return str.replace(/</g, '<').replace(/&/g, '&').replace(/"/g, '"');
}
function radioCheckboxBuilder(type) {
var o = _si.settings.options.length,
cb = '';
if ( o === 0 )
return radioCheckboxOption(type, '', 'Radio Option');
for ( var i = 0; i < o; i++ ) {
cb += radioCheckboxOption(type, i, _si.settings.options[i]);
}
return cb;
}
function radioCheckboxOption(type, id, value) {
var id = 'si_q' + id,
name = 'si_q';
if ('checkbox' === type) {
name = 'si_q[]';
}
return '<label for="' + id + '"><input type="' + type + '" id="' + id + '" name="' + name + '" value="' + value + '" />' + encodeHTML(value) + '</label>';
}
function radioCheckboxSelected(c) {
s = 'si_q=';
for (var i = 0; i < c.length; i++) {
if (c[i].checked) {
s += encodeURIComponent(c[i].value) + '|';
}
}
return s;
}
function serializeFields() {
var form = document.forms["_si-form"],
s = '';
switch(_si.settings.type) {
case 'checkbox':
s = radioCheckboxSelected(form.elements['si_q[]'])
break;
case 'radio':
s = radioCheckboxSelected(form.elements['si_q'])
break;
case 'text':
case 'textarea':
s = 'si_q=' + encodeURIComponent(document.getElementById('si_q').value);
break;
}
return s;
}
function toggleSurvey( show ) {
var right = ( show ) ? 0 : -324;
d.getElementById( id ).style.right = right + 'px';
}
return {
init: function () {
// Uses localStorage to track who's seen the survey
// Thus older browsers (<= IE7) will be excluded from seeing it
if (typeof localStorage !== "undefined") {
var viewed = localStorage.getItem(_si.settings.question);
if ( !viewed ) {
addCss();
addSurvey();
setTimeout(function () {
toggleSurvey( true );
}, 300);
// Mark as viewed
localStorage.setItem(_si.settings.question, true);
}
}
},
hide: function () {
toggleSurvey( false );
},
show: function () {
toggleSurvey( true );
}
}
} ();
_si.survey.init();