Skip to content

Commit

Permalink
cleaned up a mess
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Rinck committed Sep 15, 2013
1 parent 090ed82 commit dd7e310
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 64 deletions.
31 changes: 10 additions & 21 deletions build/angular-onbeforeunload.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
angular.module('angular-onbeforeunload', []);
angular.module("angular-onbeforeunload").directive("onbeforeunload", ["$window", function ($window) {
angular.module("angular-onbeforeunload").directive("onbeforeunload", ["$window", "$filter", function ($window, $filter) {
"use strict";
var forms = [];
var unloadtext, forms = [];

function handleOnbeforeUnload() {
var i, form, s = "test", isDirty = false;
var i, form, isDirty = false;

for (i = 0; i < forms.length; i++) {
form = forms[i];
Expand All @@ -16,7 +16,7 @@ angular.module("angular-onbeforeunload").directive("onbeforeunload", ["$window",
}

if (isDirty) {
return s;
return unloadtext;
} else {
return undefined;
}
Expand All @@ -34,21 +34,10 @@ angular.module("angular-onbeforeunload").directive("onbeforeunload", ["$window",

$window.onbeforeunload = handleOnbeforeUnload;

// $element.bind("tap", function (event) {
// var confirmMessage = $attrs.confirmMessage;
// if (confirmMessage) {
// if ($window.confirm(localizationService.translate(confirmMessage))) {
// $scope.$apply($attrs.tap);
// }
// } else {
// $scope.$apply($attrs.tap);
// }
// event.stopPropagation();
// event.preventDefault();
// });
try {
unloadtext = $filter("translate")("onbeforeunload");
} catch (err) {
unloadtext = ""
}
};

}

])
;
}]);
44 changes: 1 addition & 43 deletions src/onbeforeunload.js
Original file line number Diff line number Diff line change
@@ -1,43 +1 @@
angular.module('angular-onbeforeunload', []);
angular.module("angular-onbeforeunload").directive("onbeforeunload", ["$window", "$filter", function ($window, $filter) {
"use strict";
var unloadtext, forms = [];

function handleOnbeforeUnload() {
var i, form, isDirty = false;

for (i = 0; i < forms.length; i++) {
form = forms[i];

if (form.scope[form.name].$dirty) {
isDirty = true;
break;
}
}

if (isDirty) {
return unloadtext;
} else {
return undefined;
}
}

return function ($scope, $element, $attrs) {
if ($element[0].localName !== 'form') {
throw new Error("onbeforeunload directive must only be set on a angularjs form!");
}

forms.push({
"name": $element[0].name,
"scope": $scope
});

$window.onbeforeunload = handleOnbeforeUnload;

try {
unloadtext = $filter("translate")("onbeforeunload");
} catch (err) {
unloadtext = ""
}
};
}]);
angular.module('angular-onbeforeunload', []);

0 comments on commit dd7e310

Please sign in to comment.