Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Project.modal = function(params, callback){
- var self = this;
- var container;
- var options = {
- close: true,
- buttons: [{
- type: '',
- label: 'Закрыть',
- }, {
- type: 'primary',
- label: 'Сохранить',
- value: true
- }],
- header: null,
- headerCloseButton: true,
- body: '',
- modal: {
- show: true
- },
- className: '',
- destroyOnClose: true
- }
- this.init = function(){
- $.extend(options, params);
- this.id = genRandId();
- container = $('<div class="modal fade '+options.className+'" id="'+this.id+'"/>');
- if(options.header){
- var header = $('<div class="modal-header"><h3>'+options.header+'</h3></div>').appendTo(container);
- if(options.headerCloseButton){
- $('<button class="close" data-dismiss="modal">×</button>').click(function(){
- self.close(false);
- }).prependTo(header);
- }
- }
- container.append($('<div class="modal-body"/>').html(options.body));
- var footer = $('<div class="modal-footer"/>').appendTo(container);
- for (var i=0; i < options.buttons.length; i++) {
- (function(){
- var button = options.buttons[i];
- if(button instanceof Object){
- $('<a href="#" class="btn'+(button.type ? ' btn-'+button.type : '')+'">'+button.label+'</a>').click(function(e){
- if(typeof button.f == 'function'){
- button.f.call(this, e, self, container)
- } else {
- self.close(button.value);
- }
- }).appendTo(footer);
- } else if(button == 'close'){
- $('<a href="#" class="btn">Отмена</a>').click(function(e){
- self.close(false);
- }).appendTo(footer);
- } else if(button == 'save'){
- $('<a href="#" class="btn btn-primary">Сохранить</a>').click(function(e){
- self.close(true);
- }).appendTo(footer);
- }
- })()
- };
- $('body').append(container);
- container.modal(options.modal);
- }
- this.close = function(result){
- container.modal('hide');
- if(typeof callback == 'function'){
- callback(result, container);
- }
- if(options.destroyOnClose){
- container.remove();
- delete container, options;
- }
- }
- this.open = function(result){
- container.modal('show');
- }
- this.init();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement