|
1 | | -/*! superplaceholder.js - v0.1.2 - 2018-03-28 |
| 1 | +/*! superplaceholder.js - v1.0.0 - 2019-01-04 |
2 | 2 | * 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 */ |
4 | 4 |
|
5 | 5 | (function() { |
6 | 6 | var test = document.createElement('input'); |
|
15 | 15 | return obj; |
16 | 16 | } |
17 | 17 |
|
| 18 | + var Actions = Object.freeze({ |
| 19 | + START: 'start', |
| 20 | + STOP: 'stop', |
| 21 | + NOTHING: false |
| 22 | + }); |
| 23 | + |
18 | 24 | var defaults = { |
19 | 25 | letterDelay: 100, //milliseconds |
20 | 26 | sentenceDelay: 1000, //milliseconds |
21 | 27 | loop: false, |
22 | 28 | startOnFocus: true, |
23 | 29 | shuffle: false, |
24 | 30 | showCursor: true, |
25 | | - cursor: '|' |
| 31 | + cursor: '|', |
| 32 | + autoStart: false, |
| 33 | + onFocusAction: Actions.START, |
| 34 | + onBlurAction: Actions.STOP |
26 | 35 | }; |
27 | 36 |
|
28 | 37 | // Constructor: PlaceHolder |
|
31 | 40 | this.texts = texts; |
32 | 41 | options = options || {}; |
33 | 42 | 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 | + } |
34 | 54 | 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 | + |
35 | 67 | this.begin(); |
36 | 68 | } |
37 | 69 |
|
38 | 70 | PlaceHolder.prototype.begin = function() { |
39 | | - var self = this, |
40 | | - temp, |
41 | | - randomIndex; |
| 71 | + var self = this; |
42 | 72 | 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; |
49 | 92 | } |
| 93 | + this.processText(0); |
| 94 | + } else if (this.options.onFocusAction === Actions.STOP) { |
| 95 | + this.cleanUp(); |
50 | 96 | } |
| 97 | + }; |
51 | 98 |
|
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); |
61 | 107 | } |
62 | 108 | }; |
63 | 109 |
|
|
73 | 119 | this.el.setAttribute('placeholder', this.originalPlaceholder); |
74 | 120 | } |
75 | 121 | this.timeouts.length = 0; |
| 122 | + this.isPlaying = false; |
| 123 | + }; |
| 124 | + |
| 125 | + PlaceHolder.prototype.isInProgress = function() { |
| 126 | + return this.isPlaying; |
76 | 127 | }; |
77 | 128 |
|
78 | 129 | PlaceHolder.prototype.typeString = function(str, callback) { |
|
82 | 133 | if (!str) { |
83 | 134 | return false; |
84 | 135 | } |
| 136 | + |
85 | 137 | function setTimeoutCallback(index) { |
86 | 138 | // Add cursor `|` after current substring unless we are showing last |
87 | 139 | // character of the string. |
|
92 | 144 | ? '' |
93 | 145 | : self.options.cursor) |
94 | 146 | ); |
| 147 | + // Call the completion callback when last character is being printed |
95 | 148 | if (index === str.length - 1) { |
96 | 149 | callback(); |
97 | 150 | } |
|
106 | 159 | var self = this, |
107 | 160 | timeout; |
108 | 161 |
|
| 162 | + this.isPlaying = true; |
| 163 | + |
109 | 164 | 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 | + } |
110 | 170 | timeout = setTimeout(function() { |
111 | 171 | self.processText( |
112 | 172 | self.options.loop ? (index + 1) % self.texts.length : index + 1 |
|
120 | 180 | if (!isPlaceHolderSupported) { |
121 | 181 | return; |
122 | 182 | } |
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 | + }; |
124 | 204 | }; |
125 | 205 |
|
| 206 | + superplaceholder.Actions = Actions; |
| 207 | + |
126 | 208 | // open to the world. |
127 | 209 | // commonjs |
128 | 210 | if (typeof exports === 'object') { |
|
0 commit comments