Skip to content

Commit

Permalink
Merge remote-tracking branch 'github/delete-long-text'
Browse files Browse the repository at this point in the history
  • Loading branch information
san650 committed Mar 7, 2017
2 parents e962228 + 3a1cff8 commit 0647f34
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
68 changes: 53 additions & 15 deletions addon/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const START_TO_WRITE_DELAY = 300;
const WRITE_SPEED = 9; // leters per second

const SUPPORTED_TRIGGER_EVENTS = ['keyup', 'keydown', 'focus', 'blur'];

const LONG_TEXT_LENGTH = 30;

const WRITABLE_INPUT_TYPES = ['text', 'email', 'tel', 'password', 'url', 'number'];

function sleep(milliseconds) {
Expand Down Expand Up @@ -72,7 +75,6 @@ function _destroyPointer(container) {

function typing(element, text, container) {
const $input = $(element, container);

if (!_canWrite($input)) {
return;
}
Expand All @@ -82,6 +84,7 @@ function typing(element, text, container) {
return _deleteTextFromInput($input).then(function() {
return sleep(START_TO_WRITE_DELAY);
}).then(function() {

return new RSVP.Promise(function(resolve) {
let index = 0;
let typingTimer = window.setInterval(function() {
Expand All @@ -97,7 +100,6 @@ function typing(element, text, container) {

window.clearInterval(typingTimer);
resolve();

}, 1000 / WRITE_SPEED);
});
});
Expand All @@ -109,22 +111,45 @@ function _canWrite($input) {
return isValidElement && isWritable;
}

function _canSelect($input) {
return WRITABLE_INPUT_TYPES.includes($input[0].type) || $input.is('textarea');
}

function _deleteTextFromInput($input) {
return sleep(START_TO_DELETE_DELAY).then(function() {
return new RSVP.Promise(function(resolve) {
let deleteTimer = window.setInterval(function() {
let currentText = $input.val();
return new RSVP.Promise((resolve) => {
sleep(START_TO_DELETE_DELAY).then(() => {
let isLongText = $input.val().length >= LONG_TEXT_LENGTH;
let promise = isLongText ? _deleteLongText($input) : _deleteShortText($input);
promise.then(() => {
resolve();
});
});
});
}

if (currentText.length !== 0) {
let textAfterDelete = currentText.slice(0, -1);
$input.val(textAfterDelete);
return;
}
function _deleteShortText($input) {
return new RSVP.Promise((resolve) => {
let deleteTimer = window.setInterval(function() {
let currentText = $input.val();

window.clearInterval(deleteTimer);
resolve();
if (currentText.length !== 0) {
$input.val(currentText.slice(0, -1));
return;
}

}, 1000 / DELETE_SPEED);
window.clearInterval(deleteTimer);
resolve();
}, 1000 / DELETE_SPEED);
});
}

function _deleteLongText($input) {
return new RSVP.Promise((resolve) => {
selectText($input).then(() => {
sleep(300).then(() => {
$input.val('');
resolve();
});
});
});
}
Expand Down Expand Up @@ -183,6 +208,19 @@ function triggerEvent(selector, container, eventName, eventOptions) {
return _triggerFocusBlurEvent(selector, eventName);
}

function selectText($input) {
return new RSVP.Promise((resolve, reject) => {
if ($input && _canSelect($input)) {
sleep(300).then(() => {
$input.select();
resolve();
});
} else {
reject('Input is undefined or not selectable.');
}
});
}

function movePointer(target, container, easing = "swing") {
let result;
let $target = $(target.selector, target.container);
Expand Down Expand Up @@ -331,7 +369,6 @@ function removeBlur() {
$('#ember-testing-container').removeClass('no-filter');
}


export default {
pointer,
finish,
Expand All @@ -342,5 +379,6 @@ export default {
afterClick,
typing,
triggerEvent,
selectText,
show
};
5 changes: 2 additions & 3 deletions addon/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class BasePlayer {
}

flushTasks() {
// global wait
var tasks = this.pendingTasks;
let tasks = this.pendingTasks;

this.pendingTasks = [];

Expand Down Expand Up @@ -142,7 +141,7 @@ class Player extends BasePlayer {
}

// Singleton instance of Player
var current = null;
let current = null;

export function player() {
if (!current) {
Expand Down

0 comments on commit 0647f34

Please sign in to comment.