woocommerce/assets/js/admin/order-backbone-modal.js

146 lines
3.4 KiB
JavaScript
Raw Normal View History

2014-07-15 22:56:07 +00:00
/*global jQuery, Backbone, _ */
( function( $, Backbone, _ ) {
2014-07-15 22:56:07 +00:00
'use strict';
/**
* WooCommerce Backbone Modal plugin
*
* @param {object} options
*/
$.fn.WCBackboneModal = function( options ) {
return this.each( function() {
( new $.WCBackboneModal( $( this ), options ) );
});
};
/**
* Initialize the Backbone Modal
*
* @param {object} element [description]
* @param {object} options [description]
*/
$.WCBackboneModal = function( element, options ) {
// Set settings
var settings = $.extend( {}, $.WCBackboneModal.defaultOptions, options );
2014-07-21 02:58:34 +00:00
if ( settings.template ) {
new $.WCBackboneModal.View({
target: settings.template,
string: settings.variable
2014-07-21 02:58:34 +00:00
});
}
2014-07-15 22:56:07 +00:00
};
/**
* Set default options
*
* @type {object}
*/
$.WCBackboneModal.defaultOptions = {
template: '',
variable: {}
};
/**
* Create the Backbone Modal
*
* @return {null}
*/
$.WCBackboneModal.View = Backbone.View.extend({
2014-07-15 22:56:07 +00:00
tagName: 'div',
id: 'wc-backbone-modal-dialog',
_target: undefined,
_string: undefined,
2014-07-15 22:56:07 +00:00
events: {
'click .modal-close': 'closeButton',
'click #btn-ok': 'addButton',
2015-11-04 15:41:25 +00:00
'touchstart #btn-ok': 'addButton',
'keydown': 'keyboardActions'
2014-07-15 22:56:07 +00:00
},
initialize: function( data ) {
2014-07-15 22:56:07 +00:00
this._target = data.target;
this._string = data.string;
2014-07-15 22:56:07 +00:00
_.bindAll( this, 'render' );
this.render();
},
render: function() {
var template = wp.template( this._target );
this.$el.attr( 'tabindex' , '0' ).append(
template( this._string )
);
2014-07-15 22:56:07 +00:00
2015-04-13 15:37:22 +00:00
$( document.body ).css({
2014-07-15 22:56:07 +00:00
'overflow': 'hidden'
}).append( this.$el );
2014-07-23 09:49:39 +00:00
var $content = $( '.wc-backbone-modal-content' ).find( 'article' );
var content_h = ( $content.height() < 90 ) ? 90 : $content.height();
2014-07-23 09:49:39 +00:00
var max_h = $( window ).height() - 200;
if ( max_h > 400 ) {
max_h = 400;
}
if ( content_h > max_h ) {
$content.css({
'overflow': 'auto',
height: max_h + 'px'
});
} else {
$content.css({
2014-07-23 10:51:09 +00:00
'overflow': 'visible',
height: ( content_h > 90 ) ? 'auto' : content_h + 'px'
2014-07-23 09:49:39 +00:00
});
}
2015-04-13 15:37:22 +00:00
$( document.body ).trigger( 'wc_backbone_modal_loaded', this._target );
2014-07-15 22:56:07 +00:00
},
closeButton: function( e ) {
2014-07-15 22:56:07 +00:00
e.preventDefault();
2015-04-13 15:37:22 +00:00
$( document.body ).trigger( 'wc_backbone_modal_before_remove', this._target );
2014-07-15 22:56:07 +00:00
this.undelegateEvents();
$( document ).off( 'focusin' );
2015-04-13 15:37:22 +00:00
$( document.body ).css({
2014-07-15 22:56:07 +00:00
'overflow': 'auto'
});
this.remove();
2015-04-13 15:37:22 +00:00
$( document.body ).trigger( 'wc_backbone_modal_removed', this._target );
2014-07-15 22:56:07 +00:00
},
addButton: function( e ) {
2015-04-13 15:37:22 +00:00
$( document.body ).trigger( 'wc_backbone_modal_response', [ this._target, this.getFormData() ] );
2014-07-15 22:56:07 +00:00
this.closeButton( e );
},
getFormData: function() {
2014-07-15 22:56:07 +00:00
var data = {};
$( document.body ).trigger( 'wc_backbone_modal_before_update', this._target );
2014-07-15 22:56:07 +00:00
$.each( $( 'form', this.$el ).serializeArray(), function( index, item ) {
if ( data.hasOwnProperty( item.name ) ) {
data[ item.name ] = $.makeArray( data[ item.name ] );
data[ item.name ].push( item.value );
} else {
2014-07-15 22:56:07 +00:00
data[ item.name ] = item.value;
}
});
return data;
},
keyboardActions: function( e ) {
var button = e.keyCode || e.which;
// Enter key
if ( 13 === button && ! ( e.target.tagName && ( e.target.tagName.toLowerCase() === 'input' || e.target.tagName.toLowerCase() === 'textarea' ) ) ) {
this.addButton( e );
}
// ESC key
if ( 27 === button ) {
this.closeButton( e );
}
2014-07-15 22:56:07 +00:00
}
});
2014-07-15 22:56:07 +00:00
}( jQuery, Backbone, _ ));