forked from briancmcfarlane/webcards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcards-demo.js
executable file
·177 lines (150 loc) · 6.03 KB
/
webcards-demo.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var wc_demo = {
// object reference to the form holding the demo fields
theForm : null,
// labels collection
theLabels : null,
// object references related to textarea
theCounter : null,
theTextArea : null,
// object reference to recipient text box
theRecipient : null,
// object reference to sender text box
theSender : null,
// object reference to demo button
demoBtn : null,
// properties/object references for border images
borderImgs : null,
allBorderImgs : null,
chosenBorderImg : null,
borderImgIsSelected : false,
// properties/object references for font settings
theFonts : null,
allFonts : null,
fontIsSelected : false,
init : function() {
// locating element nodes and building collections
wc_demo.theForm = wc_global.getObj('demoForm');
wc_demo.theLabels = document.getElementsByTagName('label');
wc_demo.theCounter = wc_global.getObj('counter');
wc_demo.theTextArea = wc_global.getObj('msg');
wc_demo.theRecipient = wc_global.getObj('recip');
wc_demo.theSender = wc_global.getObj('sender');
wc_demo.demoBtn = wc_global.getObj('demo');
wc_demo.borderImgs = document.getElementsByTagName('img');
wc_demo.allBorderImgs = wc_demo.borderImgs.length;
wc_demo.theFonts = document.getElementsByName('txtstyle');
wc_demo.allFonts = wc_demo.theFonts.length;
// launching configuration functions
wc_demo.configCounter();
wc_demo.configImgs();
wc_demo.configDemoBtn();
},
// assign event handler to textarea
configCounter : function() {
wc_global.addEvent(this.theTextArea, 'keyup', wc_demo.updateCounter);
},
// update character count based on textarea data
updateCounter : function() {
var totalCharacters = 300 - this.value.length;
if (totalCharacters < 0) {
totalCharacters = 'over';
wc_demo.theCounter.className = 'overLimit';
wc_demo.demoBtn.disabled = true;
}
else {
wc_demo.theCounter.className = '';
wc_demo.demoBtn.disabled = false;
}
wc_demo.theCounter.value = totalCharacters;
},
// assign event handlers to links surrounding border images
configImgs : function() {
for (var i=0; i<this.allBorderImgs; i++) {
wc_global.addEvent(this.borderImgs[i].parentNode, 'click', this.addImgBorder);
}
},
// toggle off all image outset borders and then toggle on the border for the clicked image
addImgBorder : function(e) {
for (var i=0; i<wc_demo.allBorderImgs; i++) {
wc_demo.borderImgs[i].className = '';
}
this.firstChild.className = 'imgBorder';
wc_demo.chosenBorderImg = this.firstChild.getAttribute('alt');
wc_demo.borderImgIsSelected = true;
wc_demo.stopDefault(e);
},
configDemoBtn : function() {
wc_global.addEvent(this.demoBtn, 'click', this.validateSelections);
},
// checking for proper data existence
validateSelections : function() {
// font choice validation
if (!wc_demo.fontIsSelected) { wc_demo.theLabels[0].className = 'error'; }
for (var i=0; i<wc_demo.allFonts; i++) {
if (wc_demo.theFonts[i].checked == true) {
wc_demo.fontIsSelected = true;
var chosenFont = wc_demo.theFonts[i].value;
wc_demo.theLabels[0].className = '';
}
}
// border image validation
if (!wc_demo.borderImgIsSelected) { wc_demo.theLabels[1].className = 'error'; }
else { wc_demo.theLabels[1].className = ''; }
// recipient validation
if (wc_demo.theRecipient.value != '') {
var validRecipient = true;
wc_demo.theLabels[2].className = '';
}
else { wc_demo.theLabels[2].className = 'error'; }
// message validation
if (wc_demo.theTextArea.value != '') {
var validTextArea = true;
wc_demo.theLabels[3].className = '';
}
else { wc_demo.theLabels[3].className = 'error'; }
// sender validation
if (wc_demo.theSender.value != '') {
var validSender = true;
wc_demo.theLabels[4].className = '';
}
else { wc_demo.theLabels[4].className = 'error'; }
// final check
if (wc_demo.fontIsSelected && wc_demo.borderImgIsSelected && validRecipient && validTextArea && validSender) {
if (document.getElementById('errorMessage')) {
wc_demo.theForm.removeChild(document.getElementById('errorMessage'));
}
wc_demo.createCookie('font', chosenFont);
wc_demo.createCookie('border', wc_demo.chosenBorderImg);
wc_demo.createCookie('recipient', wc_demo.theRecipient.value);
wc_demo.createCookie('message', wc_demo.theTextArea.value);
wc_demo.createCookie('sender', wc_demo.theSender.value);
window.open("preview/demo-card.html","_blank","top=25,left=25,width=400,height=290,scrollbars=0,resizable=0,location=0,toolbar=0,status=0,menubar=0,directories=0");
}
else {
if (!document.getElementById('errorMessage')) {
var errorMsg = document.createElement('p');
errorMsg.id = 'errorMessage';
errorMsg.appendChild(document.createTextNode('Missing information is boldface and red.'));
wc_demo.theForm.insertBefore(errorMsg,wc_demo.theForm.firstChild)
}
}
},
// utility function for setting cookies
createCookie : function(name,value) {
var data = name + "=" + escape(value);
document.cookie = data;
},
// utility function for stopping structural markup default behavior; disables link hrefs in this case
stopDefault : function(e) {
if (!e) {e = window.event;}
if (!e.preventDefault) {
e.preventDefault = function() { this.returnValue = false; }
}
e.preventDefault();
return false;
}
}
// object detection and initializing functionality
if (wc_global.W3CDOM) {
wc_global.addEvent(window, 'load', wc_demo.init);
}