Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Release v0.1.17
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Oct 2, 2016
1 parent 399666d commit 04bb7a7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"name": "angular-promise-buttons",
"description": "Fabulous helper to help you with your daily form coding.",
"version": "0.1.16",
"version": "0.1.17",
"authors": [
"Johannes Millan <johannes.millan@gmail.com>"
],
Expand Down
54 changes: 41 additions & 13 deletions dist/angular-promise-buttons.js
@@ -1,9 +1,13 @@
angular.module('angularPromiseButtons', []);

angular.module('angularPromiseButtons')
.directive('promiseBtn', ['angularPromiseButtons', '$compile', '$parse', function(angularPromiseButtons, $compile, $parse) {
.directive('promiseBtn', ['angularPromiseButtons', '$parse', '$timeout', function(angularPromiseButtons, $parse, $timeout) {
'use strict';

var CLICK_EVENT = 'click';
var CLICK_ATTR = 'ngClick';
var SUBMIT_EVENT = 'submit';
var SUBMIT_ATTR = 'ngSubmit';

return {
restrict: 'EA',
Expand All @@ -16,6 +20,9 @@ angular.module('angularPromiseButtons')
var providerCfg = angularPromiseButtons.config;
var cfg = providerCfg;
var promiseWatcher;
var timeout;
var timeoutDone;
var promiseDone;


function handleLoading(btnEl) {
Expand All @@ -28,28 +35,43 @@ angular.module('angularPromiseButtons')
}

function handleLoadingFinished(btnEl) {
if (cfg.btnLoadingClass) {
btnEl.removeClass(cfg.btnLoadingClass);
}
if (cfg.disableBtn) {
btnEl.removeAttr('disabled');
if ((!cfg.minDuration || timeoutDone) && promiseDone) {
if (cfg.btnLoadingClass) {
btnEl.removeClass(cfg.btnLoadingClass);
}
if (cfg.disableBtn) {
btnEl.removeAttr('disabled');
}
}
}

function initPromiseWatcher(watchExpressionForPromise, btnEl) {
// watch promise to resolve or fail
scope.$watch(watchExpressionForPromise, function(mVal) {
timeoutDone = false;
promiseDone = false;

// create timeout if option is set
if (cfg.minDuration) {
timeout = $timeout(function() {
timeoutDone = true;
handleLoadingFinished(btnEl);
}, cfg.minDuration);
}

// for regular promises
if (mVal && mVal.then) {
handleLoading(btnEl);
mVal.finally(function() {
promiseDone = true;
handleLoadingFinished(btnEl);
});
}
// for $resource
else if (mVal && mVal.$promise) {
handleLoading(btnEl);
mVal.$promise.finally(function() {
promiseDone = true;
handleLoadingFinished(btnEl);
});
}
Expand All @@ -67,19 +89,19 @@ angular.module('angularPromiseButtons')
}

function appendSpinnerTpl(btnEl) {
btnEl.append($compile(cfg.spinnerTpl)(scope));
btnEl.append(cfg.spinnerTpl);
}

function addHandlersForCurrentBtnOnly(btnEl) {
// handle current button only options via click
if (cfg.addClassToCurrentBtnOnly) {
btnEl.on(cfg.CLICK_EVENT, function() {
btnEl.on(CLICK_EVENT, function() {
btnEl.addClass(cfg.btnLoadingClass);
});
}

if (cfg.disableCurrentBtnOnly) {
btnEl.on(cfg.CLICK_EVENT, function() {
btnEl.on(CLICK_EVENT, function() {
btnEl.attr('disabled', 'disabled');
});
}
Expand Down Expand Up @@ -135,19 +157,19 @@ angular.module('angularPromiseButtons')
// check if there is any value given via attrs.promiseBtn
if (!attrs.promiseBtn) {
// handle ngClick function directly returning a promise
if (attrs.hasOwnProperty(cfg.CLICK_ATTR)) {
if (attrs.hasOwnProperty(CLICK_ATTR)) {
appendSpinnerTpl(el);
addHandlersForCurrentBtnOnly(el);
initHandlingOfViewFunctionsReturningAPromise(cfg.CLICK_EVENT, cfg.CLICK_ATTR, el);
initHandlingOfViewFunctionsReturningAPromise(CLICK_EVENT, CLICK_ATTR, el);
}
// handle ngSubmit function directly returning a promise
else if (attrs.hasOwnProperty(cfg.SUBMIT_ATTR)) {
else if (attrs.hasOwnProperty(SUBMIT_ATTR)) {
// get child submits for form elements
var btnElements = getSubmitBtnChildren(el);

appendSpinnerTpl(btnElements);
addHandlersForCurrentBtnOnly(btnElements);
initHandlingOfViewFunctionsReturningAPromise(cfg.SUBMIT_EVENT, cfg.SUBMIT_ATTR, btnElements);
initHandlingOfViewFunctionsReturningAPromise(SUBMIT_EVENT, SUBMIT_ATTR, btnElements);
}
}
// handle promises passed via scope.promiseBtn
Expand All @@ -167,6 +189,11 @@ angular.module('angularPromiseButtons')
cfg = angular.extend({}, providerCfg, newVal);
}
}, true);

// cleanup
scope.$on('$destroy', function() {
$timeout.cancel(timeout);
});
}
};
}]);
Expand All @@ -186,6 +213,7 @@ angular.module('angularPromiseButtons')
btnLoadingClass: 'is-loading',
addClassToCurrentBtnOnly: false,
disableCurrentBtnOnly: false,
minDuration: false,
CLICK_EVENT: 'click',
CLICK_ATTR: 'ngClick',
SUBMIT_EVENT: 'submit',
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-promise-buttons.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "angular-promise-buttons",
"version": "0.1.16",
"version": "0.1.17",
"repository": {
"type": "git",
"url": "https://github.com/johannesjo/angular-promise-buttons.git"
Expand Down

0 comments on commit 04bb7a7

Please sign in to comment.