PageSourceSearch

https://gid.cherinfo.ru/assets/7d04157e/modal2.js

js cherinfo.ru collected 2026-10-02 06:04:50 UTC 7,333 bytes, 282 lines download raw bytes

1function dkmodal( options )
2{
3	var self = this;
4	this.id = '';
5	this.div = undefined;
6	this.title = '';
7	this.body = undefined;
8	this.footer = undefined;
9	this.buttons = [];
10	this.onClose = function() {};
11	this.options = {
12		width: '600px'	
13	}
14	
15	$.extend(this.options, options);
16	
17	this.init = function() {
18		var id = Math.round(Math.random()*100000);
19		do {
20			this.id = 'modal'+id;
21			id++;
22		}
23		while($('#'+this.id).length>0);
24		
25		$('body').append('<div id="'+this.id+'"></div>');
26		this.div = $('#'+this.id);
27		this.div.addClass('modal')
28				.addClass('fade')
29				.attr('role','dialog')
30				.attr('aria-labelledby','myLargeModalLabel')
31				.attr('aria-hidden',true)
32				.html('<div class="modal-dialog"' + (this.options.width ?'style="width:'+this.options.width+';"': '') + '><div class="modal-content"><div class="modal-header">'+
33			       		'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title">'+
34			       		'</h4></div><div class="modal-body"></div><div class="modal-footer"></div></div></div>');
35		
36		this.div.on('hidden.bs.modal', function (e) {
37			self.onClose();
38			self.div.remove();
39			delete self;
40		});
41		this.title  = $('.modal-title', this.div);
42		this.body   = $('.modal-body' , this.div);
43		this.footer = $('.modal-footer', this.div);		
44	}
45	
46	this.init();
47	
48	this.close = function() {
49		this.div.modal('hide');
50	};
51	
52	this.open = function() {
53		console.log( $('#'+this.id).attr('id') );
54		this.div.modal('show');
55	};
56	
57
58	function buttons(buttons)
59	{
60		this.items = [];
61		
62		this.add = function(elem) {
63			var button = {
64				type: 'submit',
65				caption: '',
66				hidden: false,
67				url: '',
68				className:'',
69				action: function(){},
70				beforeSave: function(){return true;},
71				afterSave: function(h){},
72				afterValidate: function(h){return true;},
73				target: undefined,
74				loading: false,
75				id:'',
76				left: false,
77				disabled:false
78			};
79			
80			$.extend(button, elem);
81			
82			switch (button.type) {
83				case 'dismiss':
84					self.footer.append('<button '+(button.id ? 'data-button-id="'+button.id+'"' : '')+'type="button" class="btn btn-default" data-dismiss="modal"'+(button.disabled?' disabled="disabled"':'')+'>'+button.caption+'</button>');
85				break;
86			
87				case 'function':
88					var b = new $('<button>');
89					$(b).attr('type','button').addClass('btn');
90					if (button.className) {
91						$(b).addClass(button.className);
92					}
93					else {
94						$(b).addClass('btn-primary');
95					}
96					if (button.left) {
97						$(b).addClass('pull-left');
98					}
99					$(b).text(button.caption);
100					if (button.id) {
101						$(b).attr('data-button-id', button.id);
102					}
103					if (button.hidden!=undefined && button.hidden) {
104						$(b).css('display','none');
105					}
106					if (button.disabled){
107						$(b).attr('disabled','disabled');
108					}
109					var action = button.action;
110					$(b).click(function(){
111						if (button.beforeSave()) {
112							action();
113							button.afterSave();
114						}
115					});
116					self.footer.append(b);
117				break;
118				
119				case 'submit':
120				default:
121					var b = new $('<button>');
122					$(b).attr('type','button').addClass('btn');
123					if (button.className) {
124						$(b).addClass(button.className);
125					}
126					else {
127						$(b).addClass('btn-primary');
128					}
129					if (button.left) {
130						$(b).addClass('pull-left');
131					}
132					$(b).text(button.caption);
133					if (button.loading!==false) {
134						$(b).attr('data-loading-text', button.loading);
135					}
136					if (button.id) {
137						$(b).attr('data-button-id', button.id);
138					}
139					if (button.hidden!=undefined && button.hidden) {
140						$(b).css('display','none');
141					}
142					if (button.disabled){
143						$(b).attr('disabled','disabled');
144					}
145					$(b).click(function(){
146						$('.errorMessage', self.body).hide();
147						$('.errorSummary', self.body).hide();
148						if (button.beforeSave()) {
149							var btn = $(this);
150							if (button.loading!==false) {
151							    btn.button('loading');
152							}
153							$.ajax({
154								data: $('form', self.body ).serialize(),
155								dataType:'json',
156								type:'POST',
157								url: button.url,
158								success: function(h) {
159									switch(h.status) {
160										//все хорошо, все сделано, больше ничего не надо, закрываем окно
161										case 'success' : button.afterSave(h);  self.close(); break;
162										//форма не прошла валидацию, она пришла в поле html
163										case 'validate': 
164											self.body.html(h.html); 
165											button.afterValidate(h);
166										break;
167										//обновим страницу
168										case 'refresh': location.reload(); break;
169										//перейдем по ссылке в href
170										case 'redirect': location.href = h.href; break;
171										//обновим страницу, код в html, что обновляем в target ответа, или в target buttona
172										case 'update': $(button.target?button.target:h.target).html(h.html); button.afterSave(h); self.close(); break;
173									}
174								}
175							}).always(function () {
176								btn.button('reset');
177						    });
178						}
179					});
180					self.footer.append(b);
181				break;
182			}
183			this.items[elem.id] = elem;
184		};
185		
186		this.get = function(id) {
187			return $('button[data-button-id="'+id+'"]');
188		}
189		
190		this.remove = function(id) {
191			var button = $('button[data-button-id="'+id+'"]');
192			if (button) {
193				delete this.items[id];
194				button.remove();
195			}
196		};
197		this.hide = function(id){
198			var button = $('button[data-button-id="'+id+'"]');
199			if (button) {
200				button.css('display', 'none');
201				this.items[id].hidden = true;
202			}
203		};
204		this.show = function(id){
205			var button = $('button[data-button-id="'+id+'"]');
206			if (button) {
207				button.css('display', 'inline-block');
208				this.items[id].hidden = false;
209			}
210		};
211		this.disable = function(id){
212			var button = $('button[data-button-id="'+id+'"]');
213			if (button) {
214				button.attr('disabled', 'disabled');
215				this.items[id].disabled = true;
216			}
217		}
218		this.enable = function(id){
219			var button = $('button[data-button-id="'+id+'"]');
220			if (button) {
221				button.attr('disabled', false);
222				this.items[id].disabled = false;
223			}
224		}
225		
226		self.footer.html('');
227		for (i in buttons) {
228			this.add(buttons[i]);
229		}
230	};
231	
232	this.form = function(options) {
233		var settings = {
234			title : '',
235			url   : '',
236			buttons    : {},
237			afterLoad  : function() {},
238			onClose : function() {}
239		};
240		
241		if (options) {
242			$.extend(settings, options);
243		}
244		
245		this.body.load(
246			settings.url, function(){
247				self.title.text(settings.title);
248				self.buttons = new buttons(settings.buttons);
249				settings.afterLoad(this);
250				self.onClose = function() { settings.onClose()};
251				self.open();
252				$('form', self.body).submit( function(){
253					return false;
254				});
255			}
256		);
257	}
258	
259	this.message = function(options){
260		var settings = {
261			title: '',
262			buttons: {},
263			afterLoad  : function() {},
264			onClose : function() {},
265			html: ''
266		};
267		
268		if (options) {
269			$.extend(settings, options);
270		}
271		
272		self.body.html(settings.html);
273		self.title.text(settings.title);
274		self.buttons = new buttons(settings.buttons);
275		settings.afterLoad();
276		self.onClose = function() { settings.onClose()};
277		self.open();
278		$('form', self.body).submit( function(){
279			return false;
280		});
281	}
282}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.