-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.contactable.js
191 lines (168 loc) · 7.32 KB
/
jquery.contactable.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* contactable 1.5 - jQuery Ajax contact form
*
* Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Revision: $Id: jquery.contactable.min.js 2012-05-26 $
*
*/
(function(jQuery){
// Define the new for the plugin ans how to call it
jQuery.fn.contactable = function(options) {
// Set default options
var defaults = {
url: 'mail.php',
header: '',
name: 'Name',
email: 'Email',
dropdownTitle: '',
dropdownOptions: ['General', 'Website bug', 'Feature request'],
message : 'Message',
subject : 'A contactable message',
submit : 'SEND',
recievedMsg : 'Thank you for your message',
notRecievedMsg : 'Sorry but your message could not be sent, try again later',
footer: 'Please feel free to get in touch, we value your feedback',
hideOnSubmit: true
};
var options = jQuery.extend(defaults, options);
return this.each(function() {
// Create the form and inject it into the DOM
var dropdown = ''
, filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
, dropdownLen = options.dropdownOptions.length
, i;
// Add select option if applicable
if(options.dropdownTitle) {
dropdown += '<p><label for="contactable-dropdown">'+options.dropdownTitle+' </label><br /><select name="dropdown" id="contactable-dropdown" class="contactable-dropdown">';
for(i=0; i < dropdownLen; i++) {
dropdown += '<option value="'+options.dropdownOptions[i]+'">'+options.dropdownOptions[i]+'</option>';
}
dropdown += '</select></p>';
}
// Form layout
/*
* <div id="contactable-inner"></div>
* <form id="contactable-contactForm" method="" action="">
* <div id="contactable-loading"></div>
* <div id="contactable-callback"></div>
* <div class="contactable-holder">
* <p class="contactable-header">Header text</p>
* <p>
* <label for="contactable-name">Name<span class="contactable-green"> * </span></label><br />
* <input id="contactable-name" class="contactable-contact contactable-validate" name="name" />
* </p>
* <p>
* <label for="contactable-email"> Email address <span class="contactable-green"> * </span></label><br />
* <input id="contactable-email" class="contactable-contact contactable-validate" name="email" />
* </p>
* <p>
* <label for="contactable-message"> Message <span class="contactable-green"> * </span></label><br />
* <textarea id="contactable-message" name="message" class="contactable-message contactable-validate" rows="4" cols="30" ></textarea>
* </p>
* <p>
* <input class="contactable-submit" type="submit" value="Submit"/>
* </p>
* <p class="contactable-footer">Footer text</p>
* </div>
* </form>
*/
jQuery(this).html('<div id="contactable-inner"></div><form id="contactable-contactForm" method="" action=""><div id="contactable-loading"></div><div id="contactable-callback"></div><div class="contactable-holder"><p class="contactable-header">'+options.header+'</p><p><label for="contactable-name">'+options.name+'<span class="contactable-green"> * </span></label><br /><input id="contactable-name" class="contactable-contact contactable-validate" name="name" /></p><p><label for="contactable-email">'+options.email+' <span class="contactable-green"> * </span></label><br /><input id="contactable-email" class="contactable-contact contactable-validate" name="email" /></p>'+dropdown+'<p><label for="contactable-message">'+options.message+' <span class="contactable-green"> * </span></label><br /><textarea id="contactable-message" name="message" class="contactable-message contactable-validate" rows="4" cols="30" ></textarea></p><p><input class="contactable-submit" type="submit" value="'+options.submit+'"/></p><p class="contactable-footer">'+options.footer+'</p></div></form>');
// hide header or footer when empty
if(options.header.length === 0) {
jQuery(this).find(".contactable-header").hide();
}
if(options.footer.length === 0) {
jQuery(this).find(".contactable-footer").hide();
}
// Toggle the form visibility
jQuery.fn.toggleClick = function() {
var functions = arguments, iteration = 0
return this.click(function() {
functions[iteration].apply(this, arguments)
iteration = (iteration + 1) % functions.length
})
}
jQuery('#contactable-inner').toggleClick(function() {
jQuery('#contactable-overlay').css({display: 'block'});
jQuery(this).animate({"marginLeft": "-=5px"}, "2000");
jQuery('#contactable-contactForm').animate({"marginLeft": "-=0px"}, "2000");
jQuery(this).animate({"marginLeft": "+=387px"}, "4000");
jQuery('#contactable-contactForm').animate({"marginLeft": "+=390px"}, "4000");
},
function() {
jQuery('#contactable-contactForm').animate({"marginLeft": "-=390px"}, "4000");
jQuery(this).animate({"marginLeft": "-=387px"}, "4000").animate({"marginLeft": "+=5px"}, "2000");
jQuery('#contactable-overlay').css({display: 'none'});
});
// Submit the form
jQuery("#contactable-contactForm").submit(function() {
// Validate the entries
var valid = true
, params;
//Remove any previous errors
jQuery("#contactable-contactForm .contactable-validate").each(function() {
jQuery(this).removeClass('contactable-invalid');
});
// Loop through required field
jQuery("#contactable-contactForm .contactable-validate").each(function() {
// Check the min length
if(jQuery(this).val().length < 2) {
jQuery(this).addClass("contactable-invalid");
valid = false;
}
//Check email is valid
if (!filter.test(jQuery("#contactable-contactForm #contactable-email").val())) {
jQuery("#contactable-contactForm #contactable-email").addClass("contactable-invalid");
valid = false;
}
});
if(valid === true) {
submitForm();
}
return false;
});
function submitForm() {
// Display loading animation
jQuery('.contactable-holder').hide();
jQuery('#contactable-loading').show();
// Trigger form submission if form is valid
jQuery.ajax({
type: 'POST',
url: options.url,
data: {
subject:options.subject,
name:jQuery('#contactable-name').val(),
email:jQuery('#contactable-email').val(),
issue:jQuery('#contactable-dropdown').val(),
message:jQuery('#contactable-message').val()
},
success: function(data) {
// Hide loading animation
jQuery('#contactable-loading').css({display:'none'});
// Check for a valid server side response
if( data.response === 'success') {
jQuery('#contactable-callback').show().append(options.recievedMsg);
if(options.hideOnSubmit === true) {
//hide the tab after successful submition if requested
jQuery('#contactable-inner').click();
}
} else {
jQuery('#contactable-callback').show().append(options.notRecievedMsg);
setTimeout(function(){
jQuery('.contactable-holder').show();
jQuery('#contactable-callback').hide().html('');
},2000);
}
},
error:function(e){
jQuery('#contactable-loading').css({display:'none'});
jQuery('#contactable-callback').show().append(options.notRecievedMsg);
}
});
}
});
};
})(jQuery);