Skip to content

Commit b3fd88d

Browse files
committed
build 1.0.0
1 parent dc00378 commit b3fd88d

File tree

3 files changed

+108
-26
lines changed

3 files changed

+108
-26
lines changed

dist/superplaceholder.js

Lines changed: 104 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/*! superplaceholder.js - v0.1.2 - 2018-03-28
1+
/*! superplaceholder.js - v1.0.0 - 2019-01-04
22
* http://kushagragour.in/lab/superplaceholderjs/
3-
* Copyright (c) 2018 Kushagra Gour; Licensed CC-BY-ND-4.0 */
3+
* Copyright (c) 2019 Kushagra Gour; Licensed CC-BY-ND-4.0 */
44

55
(function() {
66
var test = document.createElement('input');
@@ -15,14 +15,23 @@
1515
return obj;
1616
}
1717

18+
var Actions = Object.freeze({
19+
START: 'start',
20+
STOP: 'stop',
21+
NOTHING: false
22+
});
23+
1824
var defaults = {
1925
letterDelay: 100, //milliseconds
2026
sentenceDelay: 1000, //milliseconds
2127
loop: false,
2228
startOnFocus: true,
2329
shuffle: false,
2430
showCursor: true,
25-
cursor: '|'
31+
cursor: '|',
32+
autoStart: false,
33+
onFocusAction: Actions.START,
34+
onBlurAction: Actions.STOP
2635
};
2736

2837
// Constructor: PlaceHolder
@@ -31,33 +40,70 @@
3140
this.texts = texts;
3241
options = options || {};
3342
this.options = extend(defaults, options);
43+
// Translate deprecated `startOnFocus` option to new ones.
44+
if (!this.options.startOnFocus) {
45+
// TODO: add deprecation message
46+
console.warn(
47+
'Superplaceholder.js: `startOnFocus` option has been deprecated. Please use `onFocusAction`, `onBlurAction` and `autoStart`'
48+
);
49+
50+
this.options.autoStart = true;
51+
this.options.onFocusAction = Actions.NOTHING;
52+
this.options.onBlurAction = Actions.NOTHING;
53+
}
3454
this.timeouts = [];
55+
this.isPlaying = false;
56+
57+
var temp, randomIndex;
58+
if (this.options.shuffle) {
59+
for (var i = this.texts.length; i--; ) {
60+
randomIndex = ~~(Math.random() * i);
61+
temp = this.texts[randomIndex];
62+
this.texts[randomIndex] = this.texts[i];
63+
this.texts[i] = temp;
64+
}
65+
}
66+
3567
this.begin();
3668
}
3769

3870
PlaceHolder.prototype.begin = function() {
39-
var self = this,
40-
temp,
41-
randomIndex;
71+
var self = this;
4272
self.originalPlaceholder = self.el.getAttribute('placeholder');
43-
if (self.options.shuffle) {
44-
for (var i = self.texts.length; i--; ) {
45-
randomIndex = ~~(Math.random() * i);
46-
temp = self.texts[randomIndex];
47-
self.texts[randomIndex] = self.texts[i];
48-
self.texts[i] = temp;
73+
74+
if (self.options.onFocusAction || self.options.onBlurAction) {
75+
// Store to unbind later
76+
self.listeners = {
77+
focus: self.onFocus.bind(self),
78+
blur: self.onBlur.bind(self)
79+
};
80+
self.el.addEventListener('focus', self.listeners.focus);
81+
self.el.addEventListener('blur', self.listeners.blur);
82+
}
83+
if (self.options.autoStart) {
84+
self.processText(0);
85+
}
86+
};
87+
88+
PlaceHolder.prototype.onFocus = function() {
89+
if (this.options.onFocusAction === Actions.START) {
90+
if (this.isInProgress()) {
91+
return;
4992
}
93+
this.processText(0);
94+
} else if (this.options.onFocusAction === Actions.STOP) {
95+
this.cleanUp();
5096
}
97+
};
5198

52-
if (self.options.startOnFocus) {
53-
self.el.addEventListener('focus', function() {
54-
self.processText(0);
55-
});
56-
self.el.addEventListener('blur', function() {
57-
self.cleanUp();
58-
});
59-
} else {
60-
self.processText(0);
99+
PlaceHolder.prototype.onBlur = function() {
100+
if (this.options.onBlurAction === Actions.STOP) {
101+
this.cleanUp();
102+
} else if (this.options.onBlurAction === Actions.START) {
103+
if (this.isInProgress()) {
104+
return;
105+
}
106+
this.processText(0);
61107
}
62108
};
63109

@@ -73,6 +119,11 @@
73119
this.el.setAttribute('placeholder', this.originalPlaceholder);
74120
}
75121
this.timeouts.length = 0;
122+
this.isPlaying = false;
123+
};
124+
125+
PlaceHolder.prototype.isInProgress = function() {
126+
return this.isPlaying;
76127
};
77128

78129
PlaceHolder.prototype.typeString = function(str, callback) {
@@ -82,6 +133,7 @@
82133
if (!str) {
83134
return false;
84135
}
136+
85137
function setTimeoutCallback(index) {
86138
// Add cursor `|` after current substring unless we are showing last
87139
// character of the string.
@@ -92,6 +144,7 @@
92144
? ''
93145
: self.options.cursor)
94146
);
147+
// Call the completion callback when last character is being printed
95148
if (index === str.length - 1) {
96149
callback();
97150
}
@@ -106,7 +159,14 @@
106159
var self = this,
107160
timeout;
108161

162+
this.isPlaying = true;
163+
109164
self.typeString(self.texts[index], function() {
165+
// Empty the timeouts array
166+
self.timeouts.length = 0;
167+
if (!self.options.loop && !self.texts[index + 1]) {
168+
self.isPlaying = false;
169+
}
110170
timeout = setTimeout(function() {
111171
self.processText(
112172
self.options.loop ? (index + 1) % self.texts.length : index + 1
@@ -120,9 +180,31 @@
120180
if (!isPlaceHolderSupported) {
121181
return;
122182
}
123-
new PlaceHolder(params.el, params.sentences, params.options);
183+
var instance = new PlaceHolder(params.el, params.sentences, params.options);
184+
return {
185+
start: function() {
186+
if (instance.isInProgress()) {
187+
return;
188+
}
189+
instance.processText(0);
190+
},
191+
stop: function() {
192+
instance.cleanUp();
193+
},
194+
destroy: function() {
195+
instance.cleanUp();
196+
for (var eventName in instance.listeners) {
197+
instance.el.removeEventListener(
198+
eventName,
199+
instance.listeners[eventName]
200+
);
201+
}
202+
}
203+
};
124204
};
125205

206+
superplaceholder.Actions = Actions;
207+
126208
// open to the world.
127209
// commonjs
128210
if (typeof exports === 'object') {

dist/superplaceholder.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "superplaceholder",
33
"title": "superplaceholder.js",
44
"description": "Super charge your input placeholders",
5-
"version": "0.1.2",
5+
"version": "1.0.0",
66
"homepage": "http://kushagragour.in/lab/superplaceholderjs/",
77
"author": {
88
"name": "Kushagra Gour",

0 commit comments

Comments
 (0)