Skip to content

Commit e380a80

Browse files
committed
https://github.com/FoxUSA/OpenNote/issues/57
1 parent 1c9097a commit e380a80

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

OpenNote/openNote/controllers/noteController.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,7 @@ openNote.controller("noteController", function( $scope, $rootScope, $routeParams
3333
return {
3434
text: "Clear",
3535
action: function(){
36-
alertify.confirm("Are you sure you want to clear your changes?",
37-
function(confirm) {
38-
if(!confirm)
39-
return;
40-
41-
$(".notePartial").fadeOut(config.fadeSpeedShort(),function(){
42-
$scope.$apply(function(){
43-
$location.url("/folder/"+$scope.note.folderID);
44-
});
45-
});
46-
}
47-
);
36+
$scope.clear();
4837
},
4938
helpText: $rootScope.helpContent.clearButton
5039
};
@@ -63,6 +52,8 @@ openNote.controller("noteController", function( $scope, $rootScope, $routeParams
6352
CKEDITOR.replace("note", config);
6453
$rootScope.buttons=[];
6554

55+
attachWindowUnload();
56+
6657
//Add new buttons
6758
$rootScope.buttons.push(saveButton());
6859
$rootScope.buttons.push(clearButton());
@@ -100,7 +91,6 @@ openNote.controller("noteController", function( $scope, $rootScope, $routeParams
10091
* Save a note
10192
*/
10293
$scope.save = function(){
103-
10494
$scope.note.note = CKEDITOR.instances["note"].getData();
10595

10696
//Insert only logic
@@ -109,6 +99,7 @@ openNote.controller("noteController", function( $scope, $rootScope, $routeParams
10999

110100
$(".notePartial").fadeOut(config.fadeSpeedShort());
111101
$scope.note.$save().then(function(){
102+
detachWindowUnload();
112103
$location.url("/note/"+$scope.note.id)
113104
alertify.success("Note Saved"); //all done. close the notify dialog
114105
});
@@ -127,17 +118,52 @@ openNote.controller("noteController", function( $scope, $rootScope, $routeParams
127118
var folderID = $scope.note.folderID;//need to keep track of this because we are about to delete it
128119
$(".notePartial").fadeOut(config.fadeSpeedShort());
129120
$scope.note.$remove({id: $scope.note.id}).then(function(){
121+
detachWindowUnload();
130122
alertify.success("Note Deleted",5); //all done. close the notify dialog
131123
$location.url("/folder/"+folderID);
132124
});
133125
}
134126
);
135127
}
136128

129+
/**
130+
* Reset changes
131+
*/
132+
$scope.clear = function(){
133+
alertify.confirm("Are you sure you want to clear your changes?",
134+
function(confirm) {
135+
if(!confirm)
136+
return;
137+
138+
$(".notePartial").fadeOut(config.fadeSpeedShort(),function(){
139+
$scope.$apply(function(){
140+
detachWindowUnload();
141+
$location.url("/folder/"+$scope.note.folderID);
142+
});
143+
});
144+
});
145+
};
146+
137147
/**
138148
* Mark html as trusted
139149
*/
140150
$scope.trustHTML = function(html) {
141151
return $sce.trustAsHtml(html);
142152
}
153+
154+
/**
155+
* Attach window on-load listener
156+
*/
157+
var attachWindowUnload = function(){
158+
window.onbeforeunload = function() {
159+
return "Are you sure you want to navigate away?";//Keep the page from closing
160+
};
161+
}
162+
163+
/**
164+
* Remove window on-load listener
165+
*/
166+
var detachWindowUnload = function(){
167+
window.onbeforeunload = null;
168+
}
143169
});

0 commit comments

Comments
 (0)