2016-01-13 15:04:10 +00:00
|
|
|
/*global jQuery, Backbone, _ */
|
|
|
|
( function( $, Backbone, _ ) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce Backbone Modal plugin
|
|
|
|
*
|
|
|
|
* @param {object} options
|
|
|
|
*/
|
|
|
|
$.fn.WCBackboneModal = function( options ) {
|
|
|
|
return this.each( function() {
|
2017-11-15 05:39:10 +00:00
|
|
|
( new $.WCBackboneModal( $( this ), options ) );
|
2016-01-13 15:04:10 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the Backbone Modal
|
|
|
|
*
|
|
|
|
* @param {object} element [description]
|
|
|
|
* @param {object} options [description]
|
|
|
|
*/
|
|
|
|
$.WCBackboneModal = function( element, options ) {
|
|
|
|
// Set settings
|
|
|
|
var settings = $.extend( {}, $.WCBackboneModal.defaultOptions, options );
|
|
|
|
|
|
|
|
if ( settings.template ) {
|
2017-11-15 05:39:10 +00:00
|
|
|
new $.WCBackboneModal.View({
|
|
|
|
target: settings.template,
|
|
|
|
string: settings.variable
|
|
|
|
});
|
2016-01-13 15:04:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set default options
|
|
|
|
*
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
$.WCBackboneModal.defaultOptions = {
|
|
|
|
template: '',
|
|
|
|
variable: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the Backbone Modal
|
|
|
|
*
|
|
|
|
* @return {null}
|
|
|
|
*/
|
|
|
|
$.WCBackboneModal.View = Backbone.View.extend({
|
|
|
|
tagName: 'div',
|
|
|
|
id: 'wc-backbone-modal-dialog',
|
|
|
|
_target: undefined,
|
|
|
|
_string: undefined,
|
|
|
|
events: {
|
|
|
|
'click .modal-close': 'closeButton',
|
2016-05-11 15:19:19 +00:00
|
|
|
'click #btn-ok' : 'addButton',
|
2016-01-13 15:04:10 +00:00
|
|
|
'touchstart #btn-ok': 'addButton',
|
2016-05-11 15:19:19 +00:00
|
|
|
'keydown' : 'keyboardActions'
|
|
|
|
},
|
|
|
|
resizeContent: function() {
|
|
|
|
var $content = $( '.wc-backbone-modal-content' ).find( 'article' );
|
|
|
|
var max_h = $( window ).height() * 0.75;
|
|
|
|
|
|
|
|
$content.css({
|
|
|
|
'max-height': max_h + 'px'
|
|
|
|
});
|
2016-01-13 15:04:10 +00:00
|
|
|
},
|
|
|
|
initialize: function( data ) {
|
2016-05-11 15:19:19 +00:00
|
|
|
var view = this;
|
2016-01-13 15:04:10 +00:00
|
|
|
this._target = data.target;
|
|
|
|
this._string = data.string;
|
|
|
|
_.bindAll( this, 'render' );
|
|
|
|
this.render();
|
2016-05-11 15:19:19 +00:00
|
|
|
|
|
|
|
$( window ).resize(function() {
|
|
|
|
view.resizeContent();
|
|
|
|
});
|
2016-01-13 15:04:10 +00:00
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
var template = wp.template( this._target );
|
|
|
|
|
2017-08-03 11:32:15 +00:00
|
|
|
this.$el.append(
|
2016-01-13 15:04:10 +00:00
|
|
|
template( this._string )
|
|
|
|
);
|
|
|
|
|
|
|
|
$( document.body ).css({
|
|
|
|
'overflow': 'hidden'
|
|
|
|
}).append( this.$el );
|
|
|
|
|
2016-05-11 15:19:19 +00:00
|
|
|
this.resizeContent();
|
2017-08-03 11:32:15 +00:00
|
|
|
this.$( '.wc-backbone-modal-content' ).attr( 'tabindex' , '0' ).focus();
|
|
|
|
|
2016-08-05 12:15:38 +00:00
|
|
|
$( document.body ).trigger( 'init_tooltips' );
|
2016-01-13 15:04:10 +00:00
|
|
|
|
|
|
|
$( document.body ).trigger( 'wc_backbone_modal_loaded', this._target );
|
|
|
|
},
|
|
|
|
closeButton: function( e ) {
|
|
|
|
e.preventDefault();
|
|
|
|
$( document.body ).trigger( 'wc_backbone_modal_before_remove', this._target );
|
|
|
|
this.undelegateEvents();
|
|
|
|
$( document ).off( 'focusin' );
|
|
|
|
$( document.body ).css({
|
|
|
|
'overflow': 'auto'
|
|
|
|
});
|
|
|
|
this.remove();
|
|
|
|
$( document.body ).trigger( 'wc_backbone_modal_removed', this._target );
|
|
|
|
},
|
|
|
|
addButton: function( e ) {
|
|
|
|
$( document.body ).trigger( 'wc_backbone_modal_response', [ this._target, this.getFormData() ] );
|
|
|
|
this.closeButton( e );
|
|
|
|
},
|
|
|
|
getFormData: function() {
|
|
|
|
var data = {};
|
|
|
|
|
|
|
|
$( document.body ).trigger( 'wc_backbone_modal_before_update', this._target );
|
|
|
|
|
|
|
|
$.each( $( 'form', this.$el ).serializeArray(), function( index, item ) {
|
2016-12-06 11:53:27 +00:00
|
|
|
if ( item.name.indexOf( '[]' ) !== -1 ) {
|
2016-11-21 12:15:17 +00:00
|
|
|
item.name = item.name.replace( '[]', '' );
|
2016-01-13 15:04:10 +00:00
|
|
|
data[ item.name ] = $.makeArray( data[ item.name ] );
|
|
|
|
data[ item.name ].push( item.value );
|
|
|
|
} else {
|
|
|
|
data[ item.name ] = item.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
keyboardActions: function( e ) {
|
|
|
|
var button = e.keyCode || e.which;
|
|
|
|
|
|
|
|
// Enter key
|
2020-05-05 03:49:29 +00:00
|
|
|
if (
|
|
|
|
13 === button &&
|
|
|
|
! ( e.target.tagName && ( e.target.tagName.toLowerCase() === 'input' || e.target.tagName.toLowerCase() === 'textarea' ) )
|
|
|
|
) {
|
2016-01-13 15:04:10 +00:00
|
|
|
this.addButton( e );
|
|
|
|
}
|
|
|
|
|
|
|
|
// ESC key
|
|
|
|
if ( 27 === button ) {
|
|
|
|
this.closeButton( e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}( jQuery, Backbone, _ ));
|