Blame view

WebRoot/static/ace/js/ace/ace.ajax-content.js 11.3 KB
ad5081d3   孙向锦   初始化项目
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
  /**
   <b>Load content via Ajax </b>. For more information please refer to documentation #basics/ajax
  */
  
  (function($ , undefined) {
  	var ajax_loaded_scripts = {}
  
  	function AceAjax(contentArea, settings) {
  		var $contentArea = $(contentArea);
  		var self = this;
  		$contentArea.attr('data-ajax-content', 'true');
  		
  		//get a list of 'data-*' attributes that override 'defaults' and 'settings'
  		var attrib_values = ace.helper.getAttrSettings(contentArea, $.fn.ace_ajax.defaults);
  		this.settings = $.extend({}, $.fn.ace_ajax.defaults, settings, attrib_values);
  
  
  		var working = false;
  		var $overlay = $();//empty set
  
  		this.force_reload = false;//set jQuery ajax's cache option to 'false' to reload content
  		this.loadUrl = function(hash, cache) {
  			var url = false;
  			hash = hash.replace(/^(\#\!)?\#/, '');
  			
  			this.force_reload = (cache === false)
  			
  			if(typeof this.settings.content_url === 'function') url = this.settings.content_url(hash);
  			if(typeof url === 'string') this.getUrl(url, hash, false);
  		}
  		
  		this.loadAddr = function(url, hash, cache) {
  			this.force_reload = (cache === false);
  			this.getUrl(url, hash, false);
  		}
  		
  		this.getUrl = function(url, hash, manual_trigger) {
  			if(working) {
  				return;
  			}
  		
  			var event
  			$contentArea.trigger(event = $.Event('ajaxloadstart'), {url: url, hash: hash})
  			if (event.isDefaultPrevented()) return;
  			
  			self.startLoading();
  
  			$.ajax({
  				'url': url,
  				'cache': !this.force_reload
  			})
  			.error(function() {
  				$contentArea.trigger('ajaxloaderror', {url: url, hash: hash});
  				
  				self.stopLoading(true);
  			})
  			.done(function(result) {
  				$contentArea.trigger('ajaxloaddone', {url: url, hash: hash});
  				
  				var link_element = null, link_text = '';;
  				if(typeof self.settings.update_active === 'function') {
  					link_element = self.settings.update_active.call(null, hash, url);
  				}
  				else if(self.settings.update_active === true && hash) {
  					link_element = $('a[data-url="'+hash+'"]');
  					if(link_element.length > 0) {
  						var nav = link_element.closest('.nav');
  						if(nav.length > 0) {
  							nav.find('.active').each(function(){
  								var $class = 'active';
  								if( $(this).hasClass('hover') || self.settings.close_active ) $class += ' open';
  								
  								$(this).removeClass($class);							
  								if(self.settings.close_active) {
  									$(this).find(' > .submenu').css('display', '');
  								}
  							})
  							
  							var active_li = link_element.closest('li').addClass('active').parents('.nav li').addClass('active open');
  							nav.closest('.sidebar[data-sidebar-scroll=true]').each(function() {
  								var $this = $(this);
  								$this.ace_sidebar_scroll('reset');
  								if(manual_trigger) $this.ace_sidebar_scroll('scroll_to_active');//first time only
  							})
  						}
  					}
  				}
  
  				/////////
  				if(typeof self.settings.update_breadcrumbs === 'function') {
  					link_text = self.settings.update_breadcrumbs.call(null, hash, url, link_element);
  				}
  				else if(self.settings.update_breadcrumbs === true && link_element != null && link_element.length > 0) {
  					link_text = updateBreadcrumbs(link_element);
  				}
  				/////////
  
  				//convert "title" and "link" tags to "div" tags for later processing
  				result = String(result)
  					.replace(/<(title|link)([\s\>])/gi,'<div class="hidden ajax-append-$1"$2')
  					.replace(/<\/(title|link)\>/gi,'</div>')
  			
  				
  				$overlay.addClass('content-loaded').detach();
  				$contentArea.empty().html(result);
  				
  				$(self.settings.loading_overlay || $contentArea).append($overlay);
  	
  				//remove previous stylesheets inserted via ajax
  				setTimeout(function() {
  					$('head').find('link.ace-ajax-stylesheet').remove();
  
  					var main_selectors = ['link.ace-main-stylesheet', 'link#main-ace-style', 'link[href*="/ace.min.css"]', 'link[href*="/ace.css"]']
  					var ace_style = [];
  					for(var m = 0; m < main_selectors.length; m++) {
  						ace_style = $('head').find(main_selectors[m]).first();
  						if(ace_style.length > 0) break;
  					}
  					
  					$contentArea.find('.ajax-append-link').each(function(e) {
  						var $link = $(this);
  						if ( $link.attr('href') ) {
  							var new_link = jQuery('<link />', {type : 'text/css', rel: 'stylesheet', 'class': 'ace-ajax-stylesheet'})
  							if( ace_style.length > 0 ) new_link.insertBefore(ace_style);
  							else new_link.appendTo('head');
  							new_link.attr('href', $link.attr('href'));//we set "href" after insertion, for IE to work
  						}
  						$link.remove();
  					})
  				}, 10);
  
  				//////////////////////
  
  				if(typeof self.settings.update_title === 'function') {
  					self.settings.update_title.call(null, hash, url, link_text);
  				}
  				else if(self.settings.update_title === true) {
  					updateTitle(link_text);
  				}
  				
  
  				if( !manual_trigger ) {
  					$('html,body').animate({scrollTop: 0}, 250);
  				}
  
  				//////////////////////
  				$contentArea.trigger('ajaxloadcomplete', {url: url, hash: hash});
  				//////////////////////
  				
  				self.stopLoading();
  			})
  		}
  		
  		
  		///////////////////////
  		var fixPos = false;
  		var loadTimer = null;
  		this.startLoading = function() {
  			if(working) return;
  			working = true;
  			
  			if(!this.settings.loading_overlay && $contentArea.css('position') == 'static') {
  				$contentArea.css('position', 'relative');//for correct icon positioning
  				fixPos = true;
  			}
  				
  			$overlay.remove();
  			$overlay = $('<div class="ajax-loading-overlay"><i class="ajax-loading-icon '+(this.settings.loading_icon || '')+'"></i> '+this.settings.loading_text+'</div>')
  
  			if(this.settings.loading_overlay == 'body') $('body').append($overlay.addClass('ajax-overlay-body'));
  			else if(this.settings.loading_overlay) $(this.settings.loading_overlay).append($overlay);
  			else $contentArea.append($overlay);
  
  			
  			if(this.settings.max_load_wait !== false) 
  			 loadTimer = setTimeout(function() {
  				loadTimer = null;
  				if(!working) return;
  				
  				var event
  				$contentArea.trigger(event = $.Event('ajaxloadlong'))
  				if (event.isDefaultPrevented()) return;
  				
  				self.stopLoading(true);
  			 }, this.settings.max_load_wait * 1000);
  		}
  		
  		this.stopLoading = function(stopNow) {
  			if(stopNow === true) {
  				working = false;
  				
  				$overlay.remove();
  				if(fixPos) {
  					$contentArea.css('position', '');//restore previous 'position' value
  					fixPos = false;
  				}
  				
  				if(loadTimer != null) {
  					clearTimeout(loadTimer);
  					loadTimer = null;
  				}
  			}
  			else {
  				$overlay.addClass('almost-loaded');
  				
  				$contentArea.one('ajaxscriptsloaded.inner_call', function() {
  					self.stopLoading(true);
  					/**
  					if(window.Pace && Pace.running == true) {
  						Pace.off('done');
  						Pace.once('done', function() { self.stopLoading(true) })
  					}
  					else self.stopLoading(true);
  					*/
  				})
  			}
  		}
  		
  		this.working = function() {
  			return working;
  		}
  		///////////////////////
  		
  		
  		
  		function updateBreadcrumbs(link_element) {
  			var link_text = '';
  		 
  			//update breadcrumbs
  			var breadcrumbs = $('.breadcrumb');
  			if(breadcrumbs.length > 0 && breadcrumbs.is(':visible')) {
  				breadcrumbs.find('> li:not(:first-child)').remove();
  
  				var i = 0;		
  				link_element.parents('.nav li').each(function() {
  					var link = $(this).find('> a');
  					
  					var link_clone = link.clone();
  					link_clone.find('i,.fa,.glyphicon,.ace-icon,.menu-icon,.badge,.label').remove();
  					var text = link_clone.text();
  					link_clone.remove();
  					
  					var href = link.attr('href');
  
  					if(i == 0) {
  						var li = $('<li class="active"></li>').appendTo(breadcrumbs);
  						li.text(text);
  						link_text = text;
  					}
  					else {
  						var li = $('<li><a /></li>').insertAfter(breadcrumbs.find('> li:first-child'));
  						li.find('a').attr('href', href).text(text);
  					}
  					i++;
  				})
  			}
  			
  			return link_text;
  		 }
  		 
  		 function updateTitle(link_text) {
  			var $title = $contentArea.find('.ajax-append-title');
  			if($title.length > 0) {
  				document.title = $title.text();
  				$title.remove();
  			}
  			else if(link_text.length > 0) {
  				var extra = $.trim(String(document.title).replace(/^(.*)[\-]/, ''));//for example like " - Ace Admin"
  				if(extra) extra = ' - ' + extra;
  				link_text = $.trim(link_text) + extra;
  			}
  		 }
  		 
  		 
  		 this.loadScripts = function(scripts, callback) {
  			$.ajaxPrefilter('script', function(opts) {opts.cache = true});
  			setTimeout(function() {
  				//let's keep a list of loaded scripts so that we don't load them more than once!
  				
  				function finishLoading() {
  					if(typeof callback === 'function') callback();
  					$('.btn-group[data-toggle="buttons"] > .btn').button();
  					
  					$contentArea.trigger('ajaxscriptsloaded');
  				}
  				
  				//var deferreds = [];
  				var deferred_count = 0;//deferreds count
  				var resolved = 0;
  				for(var i = 0; i < scripts.length; i++) if(scripts[i]) {
  					(function() {
  						var script_name = "js-"+scripts[i].replace(/[^\w\d\-]/g, '-').replace(/\-\-/g, '-');
  						if( ajax_loaded_scripts[script_name] !== true )	deferred_count++;
  					})()
  				}
  				
  
  				function nextScript(index) {
  					index += 1;
  					if(index < scripts.length) loadScript(index);
  					else {
  						finishLoading();
  					}
  				}
  				
  				function loadScript(index) {
  					index = index || 0;
  					if(!scripts[index]) {//could be null sometimes
  						return nextScript(index);
  					}
  				
  					var script_name = "js-"+scripts[index].replace(/[^\w\d\-]/g, '-').replace(/\-\-/g, '-');
  					//only load scripts that are not loaded yet!
  					if( ajax_loaded_scripts[script_name] !== true ) {
  						$.getScript(scripts[index])
  						.done(function() {
  							ajax_loaded_scripts[script_name] = true;
  						})
  						//.fail(function() {
  						//})
  						.complete(function() {
  							resolved++;
  							if(resolved >= deferred_count && working) {
  								finishLoading();
  							}
  							else {
  								nextScript(index);
  							}
  						})
  					}
  					else {//script previoisly loaded
  						nextScript(index);
  					}
  				}
  				
  				
  				if (deferred_count > 0) {
  					loadScript();
  				}
  				else {
  					finishLoading();
  				}
  
  			}, 10)
  		}
  		
  		
  		
  		/////////////////
  		$(window)
  		.off('hashchange.ace_ajax')
  		.on('hashchange.ace_ajax', function(e, manual_trigger) {
  			var hash = $.trim(window.location.hash);
  			if(!hash || hash.length == 0) return;
  			
  			self.loadUrl(hash);
  		}).trigger('hashchange.ace_ajax', [true]);
  		
  		var hash = $.trim(window.location.hash);
  		if(!hash && this.settings.default_url) window.location.hash = this.settings.default_url;
  
  	}//AceAjax
  
  
  
  	$.fn.aceAjax = $.fn.ace_ajax = function (option, value, value2, value3) {
  		var method_call;
  
  		var $set = this.each(function () {
  			var $this = $(this);
  			var data = $this.data('ace_ajax');
  			var options = typeof option === 'object' && option;
  
  			if (!data) $this.data('ace_ajax', (data = new AceAjax(this, options)));
  			if (typeof option === 'string' && typeof data[option] === 'function') {
  				if(value3 != undefined) method_call = data[option](value, value2, value3);
  				else if(value2 != undefined) method_call = data[option](value, value2);
  				else method_call = data[option](value);
  			}
  		});
  
  		return (method_call === undefined) ? $set : method_call;
  	}
  	
  	
  	
  	$.fn.aceAjax.defaults = $.fn.ace_ajax.defaults = {
  		content_url: false,
  		default_url: false,
  		loading_icon: 'fa fa-spin fa-spinner fa-2x orange',
  		loading_text: '',
  		loading_overlay: null,
  		update_breadcrumbs: true,
  		update_title: true,
  		update_active: true,
  		close_active: false,
  		max_load_wait: false
       }
  
  })(window.jQuery);