Advertisement
zav

Untitled

zav
Jun 3rd, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Project.modal = function(params, callback){
  2.         var self = this;
  3.         var container;
  4.         var options = {
  5.                 close: true,
  6.                 buttons: [{
  7.                         type: '',
  8.                         label: 'Закрыть',
  9.                 }, {
  10.             type: 'primary',
  11.             label: 'Сохранить',
  12.             value: true
  13.         }],
  14.                 header: null,
  15.                 headerCloseButton: true,
  16.                 body: '',
  17.                 modal: {
  18.                         show: true
  19.                 },
  20.                 className: '',
  21.                 destroyOnClose: true
  22.         }
  23.         this.init = function(){
  24.                 $.extend(options, params);
  25.                 this.id = genRandId();
  26.                 container = $('<div class="modal fade '+options.className+'" id="'+this.id+'"/>');
  27.                 if(options.header){
  28.                         var header = $('<div class="modal-header"><h3>'+options.header+'</h3></div>').appendTo(container);
  29.                         if(options.headerCloseButton){
  30.                                 $('<button class="close" data-dismiss="modal">×</button>').click(function(){
  31.                                         self.close(false);
  32.                                 }).prependTo(header);
  33.                         }
  34.                 }
  35.                
  36.                 container.append($('<div class="modal-body"/>').html(options.body));
  37.                
  38.                 var footer = $('<div class="modal-footer"/>').appendTo(container);
  39.                
  40.                 for (var i=0; i < options.buttons.length; i++) {
  41.                         (function(){
  42.                                 var button = options.buttons[i];
  43.                                 if(button instanceof Object){
  44.                                         $('<a href="#" class="btn'+(button.type ? ' btn-'+button.type : '')+'">'+button.label+'</a>').click(function(e){
  45.                                                 if(typeof button.f == 'function'){
  46.                                                         button.f.call(this, e, self, container)
  47.                                                 } else {
  48.                                                         self.close(button.value);
  49.                                                 }
  50.                                         }).appendTo(footer);
  51.                                 } else if(button == 'close'){
  52.                                         $('<a href="#" class="btn">Отмена</a>').click(function(e){
  53.                                                 self.close(false);
  54.                                         }).appendTo(footer);
  55.                                 } else if(button == 'save'){
  56.                                         $('<a href="#" class="btn btn-primary">Сохранить</a>').click(function(e){
  57.                                                 self.close(true);
  58.                                         }).appendTo(footer);
  59.                                 }
  60.                         })()
  61.                        
  62.                 };
  63.                
  64.                 $('body').append(container);
  65.                
  66.                 container.modal(options.modal);
  67.  
  68.         }
  69.  
  70.         this.close = function(result){
  71.                 container.modal('hide');
  72.                 if(typeof callback == 'function'){
  73.                         callback(result, container);
  74.                 }
  75.                 if(options.destroyOnClose){
  76.                         container.remove();
  77.                         delete container, options;
  78.                 }
  79.         }
  80.        
  81.         this.open = function(result){
  82.                 container.modal('show');
  83.         }
  84.        
  85.         this.init();
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement