Last active
January 20, 2016 18:18
-
-
Save citylims/7d9ad1ebd1ec6038953a to your computer and use it in GitHub Desktop.
ghostwriter angular directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
/** | |
* @desc directive for ghostwriter | |
* @example <ghost-writer></ghost-writer> | |
*/ | |
angular | |
.module('starter') | |
.directive('ghostWriter', ghostWriter); | |
function ghostWriter() { | |
var directive = { | |
restrict: 'E', | |
templateUrl: '/templates/ghostwriter.html', | |
scope: true, | |
controller: ghostWriterCtrl, | |
bindToController: true, | |
link: link | |
}; | |
return directive; | |
function link(scope, element, attrs) { | |
scope.openForm(); | |
}; | |
} | |
ghostWriterCtrl.$inject = ['$scope', '$http', '$timeout', '$interval']; | |
function ghostWriterCtrl($scope, $http, $timeout, $interval) { | |
init(); | |
function init() { | |
getSuggestions(); | |
console.log(apiUrl) | |
} | |
function getSuggestions() { | |
$http.get(apiUrl + '/suggestions').success(function(data) { | |
writeMessage(data); | |
}); | |
} | |
var shuffle = function(o) { | |
for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); | |
return o; | |
} | |
function writeMessage(placeholders) { | |
var placeholders = shuffle(placeholders); | |
var content = placeholders[0]; | |
var i = 0; | |
var el = angular.element(document.getElementById('textBoxMessage')); | |
var cursor = angular.element(document.getElementById('cursor')); | |
$scope.cursor = "|"; | |
$scope.message = ""; | |
if (el.hasClass('fade')) { | |
el.removeClass('fade'); | |
} | |
if (cursor.hasClass('fade')) { | |
cursor.removeClass('fade'); | |
} | |
var timer = $interval(function() { | |
if (i < content.length) { | |
$scope.message += content[i] | |
} else { | |
$interval.cancel(timer); | |
$timeout(function() { | |
refresh(el, cursor, placeholders) | |
}, 1500); | |
} | |
i++ | |
}, 130) | |
function refresh (el, cursor, placeholders) { | |
el.addClass('fade'); | |
cursor.addClass('fade'); | |
$timeout(function() { | |
writeMessage(placeholders); | |
}, 3000) | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment