@@ -33,18 +33,7 @@ openNote.controller("noteController", function( $scope, $rootScope, $routeParams
33
33
return {
34
34
text : "Clear" ,
35
35
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 ( ) ;
48
37
} ,
49
38
helpText : $rootScope . helpContent . clearButton
50
39
} ;
@@ -63,6 +52,8 @@ openNote.controller("noteController", function( $scope, $rootScope, $routeParams
63
52
CKEDITOR . replace ( "note" , config ) ;
64
53
$rootScope . buttons = [ ] ;
65
54
55
+ attachWindowUnload ( ) ;
56
+
66
57
//Add new buttons
67
58
$rootScope . buttons . push ( saveButton ( ) ) ;
68
59
$rootScope . buttons . push ( clearButton ( ) ) ;
@@ -100,7 +91,6 @@ openNote.controller("noteController", function( $scope, $rootScope, $routeParams
100
91
* Save a note
101
92
*/
102
93
$scope . save = function ( ) {
103
-
104
94
$scope . note . note = CKEDITOR . instances [ "note" ] . getData ( ) ;
105
95
106
96
//Insert only logic
@@ -109,6 +99,7 @@ openNote.controller("noteController", function( $scope, $rootScope, $routeParams
109
99
110
100
$ ( ".notePartial" ) . fadeOut ( config . fadeSpeedShort ( ) ) ;
111
101
$scope . note . $save ( ) . then ( function ( ) {
102
+ detachWindowUnload ( ) ;
112
103
$location . url ( "/note/" + $scope . note . id )
113
104
alertify . success ( "Note Saved" ) ; //all done. close the notify dialog
114
105
} ) ;
@@ -127,17 +118,52 @@ openNote.controller("noteController", function( $scope, $rootScope, $routeParams
127
118
var folderID = $scope . note . folderID ; //need to keep track of this because we are about to delete it
128
119
$ ( ".notePartial" ) . fadeOut ( config . fadeSpeedShort ( ) ) ;
129
120
$scope . note . $remove ( { id : $scope . note . id } ) . then ( function ( ) {
121
+ detachWindowUnload ( ) ;
130
122
alertify . success ( "Note Deleted" , 5 ) ; //all done. close the notify dialog
131
123
$location . url ( "/folder/" + folderID ) ;
132
124
} ) ;
133
125
}
134
126
) ;
135
127
}
136
128
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
+
137
147
/**
138
148
* Mark html as trusted
139
149
*/
140
150
$scope . trustHTML = function ( html ) {
141
151
return $sce . trustAsHtml ( html ) ;
142
152
}
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
+ }
143
169
} ) ;
0 commit comments