| 1 |
/* |
| 2 |
How to use the modal |
| 3 |
|
| 4 |
wp_optimize.modal.open({ |
| 5 |
className: 'a-class', // A class name, added to the main modal container |
| 6 |
events: {}, // An object containing the events added to the modal. See Backbonejs View events syntax. |
| 7 |
content: function() { |
| 8 |
return ''; // the Content method returns html or jQuery objects which will be added to the content area of the modal |
| 9 |
}, |
| 10 |
... // Other methods used by the custom events |
| 11 |
}) |
| 12 |
*/ |
| 13 |
|
| 14 |
var wp_optimize = window.wp_optimize || {}; |
| 15 |
|
| 16 |
(function($, wp) { |
| 17 |
'use strict'; |
| 18 |
var modal = {}; |
| 19 |
modal.views = {}; |
| 20 |
|
| 21 |
/** |
| 22 |
* Main modal View |
| 23 |
*/ |
| 24 |
modal.views.modal = Backbone.View.extend({ |
| 25 |
tagName: 'div', |
| 26 |
template: wp.template('wpo-modal'), |
| 27 |
/** |
| 28 |
* Extend default values |
| 29 |
*/ |
| 30 |
preinitialize: function() { |
| 31 |
this.events = _.extend(this.events || {}, { |
| 32 |
'click .wpo-modal--close': 'close' |
| 33 |
}); |
| 34 |
this.className = this.className ? 'wpo-modal--container ' + this.className : 'wpo-modal--container '; |
| 35 |
}, |
| 36 |
render: function() { |
| 37 |
this.$el.append(this.template()); |
| 38 |
this.trigger('rendered'); |
| 39 |
}, |
| 40 |
initialize: function() { |
| 41 |
this.trigger('initialize'); |
| 42 |
this.render(); |
| 43 |
this.$content = this.$el.find('.wpo-modal--content'); |
| 44 |
// Append the content area with the content provided by the child object |
| 45 |
if ('function' === typeof this.content) { |
| 46 |
this.$content.append(this.content()); |
| 47 |
} |
| 48 |
}, |
| 49 |
close: function() { |
| 50 |
$('body').removeClass('wpo-modal-is-opened'); |
| 51 |
this.remove(); |
| 52 |
} |
| 53 |
}); |
| 54 |
|
| 55 |
/** |
| 56 |
* Public method to create and open the modal |
| 57 |
*/ |
| 58 |
modal.open = function(options) { |
| 59 |
var view_options = _.extend(options || {}, {}); |
| 60 |
var modalView = modal.views.modal.extend(view_options); |
| 61 |
var m = new modalView(); |
| 62 |
m.$el.appendTo('body'); |
| 63 |
m.$('.wpo-modal').focus(); |
| 64 |
$('body').addClass('wpo-modal-is-opened'); |
| 65 |
return m; |
| 66 |
} |
| 67 |
|
| 68 |
wp_optimize.modal = modal; |
| 69 |
})(jQuery, window.wp); |