-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlimit-post-titles.js
63 lines (60 loc) · 1.9 KB
/
limit-post-titles.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
/*-----------------------------------
| Jesus is coming. Look busy.
------------------------------------*/
(function(){
var Limiter = function(){
var limiter = {
limit: window.sr_post_titles.limit,
bootstrap: function(){
this.el = document.getElementById('titlewrap');
this.submitBtn = document.getElementById('publish');
this.postForm = document.getElementById('post');
if(this.el) this.appendCounter();
},
appendCounter: function(){
this.counter = document.createElement('span');
this.counter.appendChild(document.createTextNode(''));
this.counter.id = 'sr-title-limiter';
this.el.appendChild(this.counter);
this.titleInput = this.el.getElementsByTagName('input')[0];
this.run();
},
setCounter: function(){
this.counter.childNodes[0].nodeValue = this.getLength();
this.setClasses();
},
setClasses: function(){
if(parseInt(this.getLength(), 10) < 0){
this.counter.style.color = 'red';
this.submitBtn.className = 'button button-primary-disabled button-large';
} else {
this.counter.style.color = '#999';
this.submitBtn.className = 'button button-primary button-large';
}
},
getLength: function(){
return this.limit-this.titleInput.value.length;
},
checkLimit: function(e){
if(this.getLength() < 1){
window.alert('Your title must be below '+this.limit+' characters.');
e.stopImmediatePropagation();
e.preventDefault();
return false;
}
},
run: function(){
this.setCounter();
this.titleInput.addEventListener('keyup', this.setCounter.bind(this));
this.submitBtn.addEventListener('click', this.checkLimit.bind(this));
this.postForm.addEventListener('submit', this.checkLimit.bind(this));
}
};
this.init = function(){
limiter.bootstrap();
};
};
// push our script to the end of the callstack
var limiter = new Limiter();
window.setTimeout(limiter.init, 0);
})();