Skip to content

Commit d69c65b

Browse files
author
Null McNull
committed
FIX: Location change
1 parent 9499add commit d69c65b

File tree

10 files changed

+247
-126
lines changed

10 files changed

+247
-126
lines changed

package/angular-block-ui/README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
angular-block-ui
2+
============
3+
A simple AngularJS module that allows you to block user interaction on AJAX requests. Blocking is done automatically for each http request and/or manually via an injectable service.
4+
5+
#### Dependencies
6+
Besides AngularJS (~1.2.4), none.
7+
8+
#### Usage
9+
By default the module will block the user interface on each pending request made from the browser. This behavior can be modified in the configuration.
10+
11+
It's also possible to do the blocking manually. The blockUI module exposes a service by the same name. Access to the service is gained by injecting it into your controller or directive:
12+
13+
angular.module('myApp').controller('MyController', function($scope, blockUI) {
14+
// A function called from user interface, which performs an async operation.
15+
$scope.onSave = function(item) {
16+
17+
// Block the user interface
18+
blockUI.start();
19+
20+
// Perform the async operation
21+
item.$save(function() {
22+
23+
// Unblock the user interface
24+
blockUI.stop();
25+
26+
});
27+
};
28+
});
29+
30+
BlockUI service methods
31+
=======================
32+
33+
#### start
34+
The start method will start the user interface block. Because multiple user interface elements can request a user interface block at the same time, the service keeps track of the number of start calls. Each call to start() will increase the count and every call to stop() will decrease the value. Whenever the count reaches 0 the block will end.
35+
36+
*Note: By default the block is immediately active after calling this method, but to prevent trashing the user interface everytime a button is pressed, the block is visible after a short delay. This behaviour can be modified in the configuration.*
37+
38+
**Arguments:**
39+
40+
* **message** (string)
41+
Indicates the message to be shown in the overlay. If none is provided, the default message from the configuration is used.
42+
43+
#### stop
44+
This will decrease the block count. The block will end if the count is 0.
45+
46+
#### reset
47+
The reset will force a unblock by setting the block count to 0.
48+
49+
#### message
50+
Allows the message shown in the overlay to be updated while to block is active.
51+
52+
BlockUI overlay template
53+
========================
54+
55+
The html and styling of the builtin template is kept barebone. It consist of two divs (overlay and message):
56+
57+
<div ng-show="blockCount > 0" class="block-ui-overlay" ng-class="{ 'block-ui-visible': blocking }"></div>
58+
<div ng-show="blocking" class="block-ui-message">{{ message }}</div>
59+
60+
A custom template can be specified in the module configuration.
61+
62+
BlockUI module configuration
63+
============================
64+
65+
The configuration of the BlockUI module can be modified via the **blockUIConfigProvider** during the config phase of your Angular application:
66+
67+
angular.module('myApp').config(function(blockUIConfigProvider) {
68+
69+
// Change the default overlay message
70+
blockUIConfigProvider.message('Please stop clicking!');
71+
72+
// Change the default delay to 100ms before the blocking is visible
73+
blockUIConfigProvider.delay(100);
74+
75+
});
76+
77+
### Methods
78+
79+
#### message
80+
Changes the default message to be used when no message has been provided to the *start* method of the service. Default value is *'Loading ...'*.
81+
82+
// Change the default overlay message
83+
blockUIConfigProvider.message('Please wait');
84+
85+
#### delay
86+
Specifies the amount in milliseconds before the block is visible to the user. By delaying a visible block your application will appear more responsive. The default value is *250*.
87+
88+
// Change the default delay to 100ms before the blocking is visible ...
89+
blockUIConfigProvider.delay(100);
90+
91+
// ... or completely remove the delay
92+
blockUIConfigProvider.delay(1);
93+
94+
#### template
95+
Specifies a custom template to use as the overlay.
96+
97+
// Provide a custom template to use
98+
blockUIConfigProvider.template('<div class="block-ui-overlay">{{ message }}</div>');
99+
100+
#### templateUrl
101+
Specifies a url to retrieve the template from. *The current release only works with pre-cached templates, which means that this url should be known in the $templateCache service of Angular. If you're using the grunt with html2js or angular-templates, which I highly recommend, you're already set.*
102+
103+
// Provide the custom template via a url
104+
blockUIConfigProvider.templateUrl('my-templates/block-ui-overlay.html');
105+
106+
#### autoBlock
107+
By default the BlockUI module will start a block whenever the Angular *$http* service has an pending request. If you don't want this behaviour and want to do all the blocking manually you can change this value to *false*.
108+
109+
// Disable automatically blocking of the user interface
110+
blockUIConfigProvider.autoBlock(false);
111+
112+
#### resetOnException
113+
By default the BlockUI module will reset the block count and hide the overlay whenever an exception has occured. You can set this value to *false* if you don't want this behaviour.
114+
115+
// Disable clearing block whenever an exception has occured
116+
blockUIConfigProvider.resetOnException(false);

package/angular-block-ui/angular-block-ui.css

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,25 @@
1515
-ms-filter:"alpha(opacity=50)";
1616
cursor: wait;
1717
}
18-
.block-ui-message {
19-
font-size: 20px;
20-
font-weight: bold;
21-
z-index: 10002;
18+
.block-ui-message-container {
2219
position: fixed;
20+
top: 50%;
21+
left: 0;
22+
right: 0;
23+
height: 0;
2324
text-align: center;
24-
width: 30%;
25-
top: 40%;
26-
left: 30%;
27-
padding: 20px;
25+
z-index: 10002;
26+
}
27+
.block-ui-message {
28+
cursor: wait;
29+
display: inline-block;
30+
text-align: left;
2831
background-color: #333;
32+
color: #f5f5f5;
33+
padding: 20px;
2934
-webkit-border-radius: 4px;
3035
-moz-border-radius: 4px;
3136
border-radius: 4px;
32-
color: #f5f5f5;
37+
font-size: 20px;
38+
font-weight: bold;
3339
}

package/angular-block-ui/angular-block-ui.js

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,20 @@
3737
_config.resetOnException = enabled;
3838
};
3939

40-
this.$get = ['$templateCache',
41-
function($templateCache) {
42-
43-
if (!_config.template) {
44-
_config.template = $templateCache.get(_config.templateUrl);
45-
}
46-
47-
return _config;
48-
}
49-
];
50-
40+
this.$get = function() {
41+
return _config;
42+
};
43+
5144
}); // blockUIConfig
5245

5346
$provide.decorator('$exceptionHandler', ['$delegate', '$injector',
5447
function($delegate, $injector) {
55-
return function(exception, cause) {
48+
var blockUI;
5649

50+
return function(exception, cause) {
5751
if (_config.resetOnException) {
58-
var blockUI = $injector.get('blockUI');
59-
60-
if (blockUI) {
61-
blockUI.reset();
62-
}
52+
blockUI = blockUI || $injector.get('blockUI');
53+
blockUI.reset();
6354
}
6455

6556
$delegate(exception, cause);
@@ -115,43 +106,44 @@
115106
$httpProvider.interceptors.push('blockUIHttpInterceptor');
116107
});
117108

109+
angular.module('blockUI').directive('blockUi', function(blockUI, blockUIConfig) {
118110

119-
angular.module('blockUI').factory('blockUI', function(blockUIConfig, $document, $rootScope, $timeout, $compile) {
120-
121-
var $overlay, $scope, startPromise, stopPromise;
122-
123-
initScope();
124-
initOverlay();
111+
return {
112+
restrict: 'A',
113+
templateUrl: blockUIConfig.template ? undefined : blockUIConfig.templateUrl,
114+
template: blockUIConfig.template,
115+
link: function($scope, $element, $attrs) {
116+
$scope.state = blockUI.state();
125117

126-
function initScope() {
127-
$scope = $rootScope.$new();
128-
$scope.blockCount = 0;
118+
var i = 0; // Skip the initial location change
129119

130-
$scope.$on('$locationChangeStart', function(event, newUrl, oldUrl) {
131-
if ($scope.blockCount > 0) {
132-
event.preventDefault();
133-
}
134-
});
135-
}
120+
$scope.$on('$locationChangeStart', function(event) {
121+
if (i++ && $scope.state.blockCount > 0) {
122+
event.preventDefault();
123+
}
124+
});
125+
}
126+
};
136127

137-
function initOverlay() {
138-
var $body = $document.find('body');
139-
var $overlay = angular.element(blockUIConfig.template);
128+
});
140129

141-
$compile($overlay)($scope);
130+
angular.module('blockUI').factory('blockUI', function(blockUIConfig, $timeout) {
142131

143-
$body.append($overlay);
144-
}
132+
var state = {
133+
blockCount: 0,
134+
message: blockUIConfig.message,
135+
blocking: false
136+
}, startPromise, stopPromise;
145137

146138
function start(message) {
147-
$scope.message = message || blockUIConfig.message;
139+
state.message = message || blockUIConfig.message;
148140

149-
$scope.blockCount++;
141+
state.blockCount++;
150142

151143
if (!startPromise) {
152144
startPromise = $timeout(function() {
153145
startPromise = null;
154-
$scope.blocking = true;
146+
state.blocking = true;
155147
}, blockUIConfig.delay);
156148
}
157149
}
@@ -164,41 +156,46 @@
164156
}
165157

166158
function stop() {
167-
$scope.blockCount = Math.max(0, --$scope.blockCount);
159+
state.blockCount = Math.max(0, --state.blockCount);
168160

169-
if ($scope.blockCount === 0) {
161+
if (state.blockCount === 0) {
170162
cancelStartTimeout();
171-
$scope.blocking = false;
163+
state.blocking = false;
172164
}
173165
}
174166

175167
function message(message) {
176-
$scope.message = message;
168+
state.message = message;
177169
}
178170

179171
function reset() {
180172
cancelStartTimeout();
181-
$scope.blockCount = 0;
182-
$scope.blocking = false;
173+
state.blockCount = 0;
174+
state.blocking = false;
183175
}
184176

185177
return {
178+
state: function() { return state; },
186179
start: start,
187180
stop: stop,
188181
message: message,
189182
reset: reset
190183
};
191184
});
192-
193-
// By forcing the injection of the blockUI service we ensure that it's initialized at application start.
194-
angular.module('blockUI').run(function(blockUI) {});
185+
186+
angular.module('blockUI').run(function($document) {
187+
$document.find('body').append('<div block-ui></div>');
188+
});
195189

196190
})(angular);
197191

198192
angular.module('templates-angularBlockUI', ['angular-block-ui/angular-block-ui.tmpl.html']);
199193

200194
angular.module("angular-block-ui/angular-block-ui.tmpl.html", []).run(["$templateCache", function($templateCache) {
201195
$templateCache.put("angular-block-ui/angular-block-ui.tmpl.html",
202-
"<div ng-show=\"blockCount > 0\" class=\"block-ui-overlay\" ng-class=\"{ 'block-ui-visible': blocking }\"></div>\n" +
203-
"<div ng-show=\"blocking\" class=\"block-ui-message\">{{ message }}</div>");
196+
"<div ng-show=\"state.blockCount > 0\" class=\"block-ui-overlay\" ng-class=\"{ 'block-ui-visible': state.blocking }\"></div>\n" +
197+
"<div ng-show=\"state.blocking\" class=\"block-ui-message-container\">\n" +
198+
" <div class=\"block-ui-message\">{{ state.message }}</div> \n" +
199+
"</div>\n" +
200+
"");
204201
}]);

package/angular-block-ui/angular-block-ui.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/angular-block-ui/angular-block-ui.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/angular-block-ui/angular-block-ui.css

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,25 @@
1515
-ms-filter:"alpha(opacity=50)";
1616
cursor: wait;
1717
}
18-
.block-ui-message {
19-
font-size: 20px;
20-
font-weight: bold;
21-
z-index: 10002;
18+
.block-ui-message-container {
2219
position: fixed;
20+
top: 50%;
21+
left: 0;
22+
right: 0;
23+
height: 0;
2324
text-align: center;
24-
width: 30%;
25-
top: 40%;
26-
left: 30%;
27-
padding: 20px;
25+
z-index: 10002;
26+
}
27+
.block-ui-message {
28+
cursor: wait;
29+
display: inline-block;
30+
text-align: left;
2831
background-color: #333;
32+
color: #f5f5f5;
33+
padding: 20px;
2934
-webkit-border-radius: 4px;
3035
-moz-border-radius: 4px;
3136
border-radius: 4px;
32-
color: #f5f5f5;
37+
font-size: 20px;
38+
font-weight: bold;
3339
}

0 commit comments

Comments
 (0)