| 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 |
// WordPress v4.9 uses Backbone v1.3.3, which does not support the preinitialize() hook. |
| 17 |
// Therefore, we are adding a patch to enable support for the preinitialize() hook. |
| 18 |
(function() { |
| 19 |
var OriginalView = Backbone.View; |
| 20 |
Backbone.View = function(options) { |
| 21 |
if (typeof this.preinitialize === 'function') { |
| 22 |
this.preinitialize.apply(this, arguments); |
| 23 |
} |
| 24 |
return OriginalView.apply(this, arguments); |
| 25 |
}; |
| 26 |
Backbone.View.prototype = OriginalView.prototype; |
| 27 |
Backbone.View.extend = OriginalView.extend; |
| 28 |
})(); |
| 29 |
|
| 30 |
(function($, wp) { |
| 31 |
'use strict'; |
| 32 |
var modal = {}; |
| 33 |
modal.views = {}; |
| 34 |
|
| 35 |
/** |
| 36 |
* Main modal View |
| 37 |
*/ |
| 38 |
modal.views.modal = Backbone.View.extend({ |
| 39 |
tagName: 'div', |
| 40 |
template: wp.template('wpo-modal'), |
| 41 |
/** |
| 42 |
* Extend default values |
| 43 |
*/ |
| 44 |
preinitialize: function() { |
| 45 |
this.events = _.extend(this.events || {}, { |
| 46 |
'click .wpo-modal--close': 'close' |
| 47 |
}); |
| 48 |
this.className = this.className ? 'wpo-modal--container ' + this.className : 'wpo-modal--container '; |
| 49 |
}, |
| 50 |
render: function() { |
| 51 |
this.$el.append(this.template()); |
| 52 |
this.trigger('rendered'); |
| 53 |
}, |
| 54 |
initialize: function() { |
| 55 |
this.trigger('initialize'); |
| 56 |
this.render(); |
| 57 |
this.$content = this.$el.find('.wpo-modal--content'); |
| 58 |
// Append the content area with the content provided by the child object |
| 59 |
if ('function' === typeof this.content) { |
| 60 |
this.$content.append(this.content()); |
| 61 |
} |
| 62 |
}, |
| 63 |
close: function() { |
| 64 |
$('body').removeClass('wpo-modal-is-opened'); |
| 65 |
this.remove(); |
| 66 |
} |
| 67 |
}); |
| 68 |
|
| 69 |
/** |
| 70 |
* Public method to create and open the modal |
| 71 |
*/ |
| 72 |
modal.open = function(options) { |
| 73 |
var view_options = _.extend(options || {}, {}); |
| 74 |
var modalView = modal.views.modal.extend(view_options); |
| 75 |
var m = new modalView(); |
| 76 |
m.$el.appendTo('body'); |
| 77 |
m.$('.wpo-modal').focus(); |
| 78 |
$('body').addClass('wpo-modal-is-opened'); |
| 79 |
return m; |
| 80 |
} |
| 81 |
|
| 82 |
wp_optimize.modal = modal; |
| 83 |
})(jQuery, window.wp); |