-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.hurt.js.coffee
38 lines (36 loc) · 1.47 KB
/
jquery.hurt.js.coffee
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
$ ->
$.fn.hurt = (options) ->
settings = $.extend({
# These are the defaults.
swearWords: ['fuck off', 'kill yourself', 'mother fucker', 'hate you', 'go to hell', 'leave me alone',
'you are dead'], #array of words/sentences if found plugin will warn
errorMessage: 'Kind of language you are using can actually <b>HURT</b> someone. Please reconsider.' #message to show
classOnElement: 'hurt' #class will be added to input text or textarea where user is typing
onEvent: 'focusout' #or keyup, keydown
errorTemplate: '<div class="text-warning">errorMessage</div>'
}, options);
settings.errorTemplate = '<div id="hurt-error">' + settings.errorTemplate.replace('errorMessage',
settings.errorMessage) + '</div>'
errorDisplayed = false
removeError = ($element) ->
if(errorDisplayed)
$('#hurt-error').remove()
$element.removeClass(settings.classOnElement)
errorDisplayed = false
displayError = ($element) ->
if(!errorDisplayed)
$('#hurt-error').remove()
$element.addClass(settings.classOnElement)
$(settings.errorTemplate).insertBefore($element)
errorDisplayed = true
this.on(settings.onEvent, ->
$this = $(this)
$.each(settings.swearWords, (index, word) ->
if($this.val().toLowerCase().indexOf(word.toLowerCase()) >= 0)
displayError($this)
false
else
removeError($this)
true
)
)