modal.js 12.5 KB
$(document).ready(function() {
			window.bs = {

			};
			window.bs.modal = function(config) {
				var renderHeadrer = function(c) {
					var content = '<div class="modal-header">';
					content += '<h4 class="modal-title">' + this.title + '</h4>';
					var hd = $(c).append(content);
					$(this.header).each(function(index, x) {
								var cmp = new window.bs.xtypes[x.xtype](x);
								cmp.render(hd.children(':last'));
							});
				};
				var renderBody = function(c) {
					if (!this.body || this.body.length <= 0) {
						return false;
					}
					var content = '<div class="modal-body">';
					content += '</div>';
					var bd = $(c).append(content);
					$(this.body).each(function(index, x) {
								var cmp = new window.bs.xtypes[x.xtype](x);
								cmp.render(bd.children(':last'));
							});
				};
				var renderFooter = function(c) {
					if (!this.footer || this.footer.length <= 0) {
						return false;
					}
					var content = '<div class="modal-footer">';
					content += '</div>';
					var ft = $(c).append(content);
					$(this.footer).each(function(index, x) {
								var cmp = new window.bs.xtypes[x.xtype](x);
								cmp.render(ft.children(':last'));
							});
				};
				this.id = '';
				this.title = '';
				this.render = function(c) {
					var content = this.getContent.call(this);
					var ins = $(c).append(content).find(':last');
					renderHeadrer.call(this, ins);
					renderBody.call(this, ins);
					renderFooter.call(this, ins);
					var scope = this;
					/*
					 * $('#' + this.id).on('hidden.bs.modal', function(m) {
					 * scope.close.call(scope, m); });
					 */
				};
				this.getContent = function() {
					var content = '<div class="modal fade" id="' + this.id + '" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">';
					content += '<div class="modal-dialog" role="dialog">';
					content += '<div class="modal-content">';
					content += '</div>';
					content += '</div>';
					content += '</div>';
					return content;
				};
				this.closable = true;
				this.closeAction = 'hide';
				this.show = function() {
					if ($('#' + this.id).length <= 0) {
						this.render(document.body);
					}
					$('#' + this.id).modal({
								keyboard : false
							});
				};
				this.close = function(m) {
					this[this.closeAction].call(this, m);
				};
				this.destroy = function(m) {
					var me = this;
					$('#' + me.id).on('hidden.bs.modal', function(e) {
								$('#' + me.id).remove();
							});
					me.hide(m);
				};
				this.hide = function(m) {
					$('#' + this.id).modal('hide')
				};
				$.extend(true, this, config);
			};
			window.bs.MessageBox = {
				confirm : function(title, callback) {
					var m = new window.bs.modal({
								id : 'confirmModal',
								title : title,
								closeAction : 'destroy',
								footer : [{
											xtype : 'button',
											value : '是',
											cls : 'btn btn-primary',
											events : {
												click : function() {
													callback.call(this, m, true);
												}
											}
										}, {
											xtype : 'button',
											value : '否',
											cls : 'btn btn-default',
											events : {
												click : function() {
													callback.call(this, m, false);
												}
											}
										}]
							});
					m.show();
				},
				alert : function(title, message, callback) {
					var m = new window.bs.modal({
								id : 'alertModal',
								title : title,
								closeAction : 'destroy',
								body : [{
											xtype : 'container',
											html : '<p>' + message + '</p>'
										}],
								footer : [{
											xtype : 'button',
											value : '确定',
											cls : 'btn btn-primary',
											events : {
												click : function() {
													m.close.call(m);
													if (callback) {
														callback.call(this, $('#alertModal'), true);
													}
												}
											}
										}]
							});
					m.show();
				}
			};
			window.bs.xtypes = {
				components : function(config) {
					if (config.defaults) {
						if (config.items) {
							$(config.items).each(function(index, ele) {
										$.extend(ele, config.defaults);
									});
						}
					}
					this.render = function(c) {
					};
					this.bindEvents = function(ele) {
						for (var prop in this.events) {
							ele.bind(prop, this.events[prop]);
						}
					};
					$.extend(true, this, config);
				},
				abs_input : function(config) {
					$.extend(this, new window.bs.xtypes.components(config));
					this.render = function(c) {
						var content = this.getContent.call(this);
						var ins = $(c).append(content).children('input:last');
						if (this.events) {
							this.bindEvents.call(this, ins);
						}
					};
					this.getContent = function() {
						var content = '';
						content += '<input type="' + this.type + '"';
						content += this.cls ? (' class="' + this.cls + '"') : '';
						content += this.name ? (' name="' + this.name + '"') : '';
						content += this.id ? (' id="' + this.id + '"') : '';
						content += this.value ? (' value="' + this.value + '"') : '';
						content += this.checked == true ? ' checked="checked"' : '';
						content += this.disabled == true ? ' disabled="disabled"' : '';
						content += this.readonly == true ? ' readonly="readonly"' : '';
						content += ' />';
						return content;
					};
				},
				ipt_hidden : function(config) {
					$.extend(true, this, new window.bs.xtypes.abs_input(config), config);
					this.type = 'hidden';
				},
				ipt_text : function(config) {
					$.extend(true, this, new window.bs.xtypes.abs_input(config), config);
					this.type = 'text';
				},
				ipt_password : function(config) {
					$.extend(true, this, new window.bs.xtypes.abs_input(config), config);
					this.type = 'password';
				},
				ipt_radio : function(config) {
					$.extend(true, this, new window.bs.xtypes.abs_input(config), config);
					this.type = 'radio';
				},
				ipt_chkbox : function(config) {
					$.extend(true, this, new window.bs.xtypes.abs_input(config), config);
					this.type = 'checkbox';
				},
				radiobox : function(config) {
					$.extend(true, this, new window.bs.xtypes.ipt_radio(config), config);
					this.render = function(c) {
						var ins = $(c).append(this.getContent.call(this));
						config.cls = null;
						var tc = new window.bs.xtypes.ipt_radio(config);
						tc.render(ins.find(':last'));
						ins = ins.find('label:last');
						ins.append(this.label);
					};
					this.getContent = function() {
						var content = '<label';
						content += this.cls ? (' class="' + this.cls + '"') : '';
						content += '>'
						content += '</label>';
						return content;
					};
				},
				chkbox : function(config) {
					$.extend(true, this, new window.bs.xtypes.ipt_chkbox(config), config);
					this.render = function(c) {
						var ins = $(c).append(this.getContent.call(this));
						config.cls = null;
						var tc = new window.bs.xtypes.ipt_chkbox(config);
						tc.render(ins.find(':last'));
						ins = ins.find('label:last');
						ins.append(this.label);
					};
					this.getContent = function() {
						var content = '<label';
						content += this.cls ? (' class="' + this.cls + '"') : '';
						content += '>'
						content += '</label>';
						return content;
					};
				},
				form : function(config) {
					this.method = 'post';
					$.extend(true, this, new window.bs.xtypes.components(config), config);
					this.render = function(c) {
						var ins = $(c).append(this.getContent());
						$(this.items).each(function(index, x) {
									var cmp = new window.bs.xtypes[x.xtype](x);
									cmp.render(ins.children(':last'));
								});
					};
					this.getContent = function() {
						var content = '<form';
						content += this.id ? (' id="' + this.id + '"') : '';
						content += this.cls ? (' class="' + this.cls + '"') : '';
						content += this.action ? (' action="' + this.action + '"') : '';
						content += ' method="' + this.method + '"';
						content += '>';
						content += '</form>';
						return content;
					};
				},
				checkboxgroup : function(config) {
					$.extend(true, this, new window.bs.xtypes.components(config), config);
					this.render = function(c) {
						var ins = $(c).append(this.getContent.call(this)).find('div:last');
						if (this.items) {
							$(this.items).each(function(index, ele) {
										var rc = new chkbox(ele);
										rc.render(ins);
									});
						}
					};
					this.getContent = function() {
						var content = '<div class="form-group">';
						content += '<label class="control-label col-sm-2">' + this.label + '</label>';
						content += '<div class="col-sm-10">';
						if (this.cls && this.cls != 'checkbox-inline') {
							content += '<div class="checkbox"></div>';
						}
						content += '</div>';
						content += '</div>';
						return content;
					};
				},
				radiogroup : function(config) {
					$.extend(true, this, new window.bs.xtypes.components(config), config);
					this.render = function(c) {
						var ins = $(c).append(this.getContent.call(this)).find('div:last');
						if (this.items) {
							$(this.items).each(function(index, ele) {
										var rc = new window.bs.xtypes.radiobox(ele);
										rc.render(ins);
									});
						}
					};
					this.getContent = function() {
						var content = '<div class="form-group">';
						content += '<label class="control-label col-sm-2">' + this.label + '</label>';
						content += '<div class="col-sm-10">';
						if (this.cls && this.cls != 'radio-inline') {
							content += '<div class="radio"></div>';
						}
						content += '</div>';
						content += '</div>';
						return content;
					};
				},
				passwordfield : function(config) {
					$.extend(true, this, new window.bs.xtypes.textfield(config), config);
					this.render = function(c) {
						var ins = $(c).append(this.getContent.call(this));
						var tc = window.bs.xtypes.ipt_password(config);
						tc.render(ins.find(':last'));
					};
				},
				textfield : function(config) {
					$.extend(true, this, config);
					this.render = function(c) {
						var ins = $(c).append(this.getContent.call(this));
						var tc = window.bs.xtypes.ipt_text(config);
						tc.render(ins.find(':last'));
					};
					this.getContent = function() {
						var content = '<div class="form-group">';
						content += '<label class="control-label col-sm-2">' + this.label + '</label>';
						content += '<div class="col-sm-10">';
						content += '</div>';
						content += '</div>';
						return content;
					};
				},
				button : function(config) {
					$.extend(true, this, new window.bs.xtypes.abs_input(config), config);
					this.type = 'button';
				},
				select : function(config) {
					$.extend(true, this, new window.bs.xtypes.components(config), config);
					this.render = function(c) {
						var content = this.getContent.call(this);
						var ins = $(c).append(content).find('select:last');
						if (this.events) {
							this.bindEvents.call(this, ins);
						}
					};
					this.getContent = function() {
						var content = '<div class="form-group">';
						content += '<label class="control-label col-sm-2">' + this.label + '</label>';
						content += '<select';
						content += this.cls ? (' class="' + this.cls + '"') : '';
						content += this.id ? (' id="' + this.id + '"') : '';
						content += this.disabled ? (' disabled="disabled"') : '';
						content += '>';
						content += '</div>';
						if (!this.store) {
							$(this.options).each(function(index, ele) {
										content += '<option';
										content += ele.value ? (' value="' + ele.value + '"') : '';
										content += '>';
										content += ele.text;
										content += '</option>';
									});
						} else {
							var me = this;
							if (me.store.beforeLoad.call(me, me.store) != false) {
								$(me.store.url, me.store.params ? me.store.params : {}, function(data) {
											if (data.success) {
												me.store.onSuccess.call(me.store, data);
											} else {
												me.store.onFalure.call(me.store, data);
											}
											me.store.afterLoad.call(me.store, data.data);
										}, 'json');
							}
						}
						content += '</select>';
						return content;
					};
				},
				container : function(config) {
					$.extend(true, this, new window.bs.xtypes.components(config), config);
					this.render = function(c) {
						var content = this.getContent.call(this);
						var ins = $(c).append(content).children('input:last');
						if (this.events) {
							this.bindEvents.call(this, ins);
						}
					};
					this.getContent = function() {
						var content = this.html;
						return content;
					};
				},
				store : function(config) {
					$.extend(true, this, config);
				}
			};
		});