1/*global jQuery, Backbone, _ */ 2( function( $, Backbone, _ ) { 3 'use strict'; 4 5 /** 6 * WooCommerce Backbone Modal plugin 7 * 8 * @param {object} options 9 */ 10 $.fn.WCBackboneModal = function( options ) { 11 return this.each( function() { 12 ( new $.WCBackboneModal( $( this ), options ) ); 13 }); 14 }; 15 16 /** 17 * Initialize the Backbone Modal 18 * 19 * @param {object} element [description] 20 * @param {object} options [description] 21 */ 22 $.WCBackboneModal = function( element, options ) { 23 // Set settings 24 var settings = $.extend( {}, $.WCBackboneModal.defaultOptions, options ); 25 26 if ( settings.template ) { 27 new $.WCBackboneModal.View({ 28 target: settings.template, 29 string: settings.variable 30 }); 31 } 32 }; 33 34 /** 35 * Set default options 36 * 37 * @type {object} 38 */ 39 $.WCBackboneModal.defaultOptions = { 40 template: '', 41 variable: {} 42 }; 43 44 /** 45 * Create the Backbone Modal 46 * 47 * @return {null} 48 */ 49 $.WCBackboneModal.View = Backbone.View.extend({ 50 tagName: 'div', 51 id: 'wc-backbone-modal-dialog', 52 _target: undefined, 53 _string: undefined, 54 events: { 55 'click .modal-close': 'closeButton', 56 'click #btn-ok' : 'addButton', 57 'click #btn-back' : 'backButton', 58 'click #btn-next' : 'nextButton', 59 'touchstart #btn-ok': 'addButton', 60 'keydown' : 'keyboardActions', 61 'input' : 'handleInputValidation' 62 }, 63 resizeContent: function() { 64 var $content = $( '.wc-backbone-modal-content' ).find( 'article' ); 65 var max_h = $( window ).height() * 0.75; 66 67 $content.css({ 68 'max-height': max_h + 'px' 69 }); 70 }, 71 initialize: function( data ) { 72 var view = this; 73 this._target = data.target; 74 this._string = data.string; 75 _.bindAll( this, 'render' ); 76 this.render(); 77 78 $( window ).on( 'resize', function() { 79 view.resizeContent(); 80 }); 81 }, 82 render: function() { 83 var template = wp.template( this._target ); 84 85 this.$el.append( 86 template( this._string ) 87 ); 88 89 $( document.body ).css({ 90 'overflow': 'hidden' 91 }).append( this.$el ); 92 93 this.resizeContent(); 94 this.$( '.wc-backbone-modal-content' ).attr( 'tabindex' , '0' ).trigger( 'focus' ); 95 96 $( document.body ).trigger( 'init_tooltips' ); 97 98 $( document.body ).trigger( 'wc_backbone_modal_loaded', this._target ); 99 }, 100 closeButton: function( e, addButtonCalled ) { 101 e.preventDefault(); 102 $( document.body ).trigger( 'wc_backbone_modal_before_remove', [ this._target, this.getFormData(), !!addButtonCalled ] ); 103 this.undelegateEvents(); 104 $( document ).off( 'focusin' ); 105 $( document.body ).css({ 106 'overflow': 'auto' 107 }); 108 this.remove(); 109 $( document.body ).trigger( 'wc_backbone_modal_removed', this._target ); 110 }, 111 addButton: function( e ) { 112 $( document.body ).trigger( 'wc_backbone_modal_response', [ this._target, this.getFormData() ] ); 113 this.closeButton( e, true ); 114 }, 115 backButton: function( e ) { 116 $( document.body ).trigger( 'wc_backbone_modal_back_response', [ this._target, this.getFormData() ] ); 117 this.closeButton( e, false ); 118 }, 119 nextButton: function( e ) { 120 var context = this; 121 function closeModal() { 122 context.closeButton( e ); 123 } 124 $( document.body ).trigger( 'wc_backbone_modal_next_response', [ this._target, this.getFormData(), closeModal ] ); 125 }, 126 getFormData: function( updating = true ) { 127 var data = {}; 128 129 if ( updating ) { 130 $( document.body ).trigger( 'wc_backbone_modal_before_update', this._target ); 131 } 132 133 $.each( $( 'form', this.$el ).serializeArray(), function( index, item ) { 134 if ( item.name.indexOf( '[]' ) !== -1 ) { 135 item.name = item.name.replace( '[]', '' ); 136 data[ item.name ] = $.makeArray( data[ item.name ] ); 137 data[ item.name ].push( item.value ); 138 } else { 139 data[ item.name ] = item.value; 140 } 141 }); 142 143 return data; 144 }, 145 handleInputValidation: function() { 146 $( document.body ).trigger( 'wc_backbone_modal_validation', [ this._target, this.getFormData( false ) ] ); 147 }, 148 keyboardActions: function( e ) { 149 var button = e.keyCode || e.which; 150 151 // Enter key 152 if ( 13 === button ) { 153 var isFormField = e.target.tagName && 154 ( e.target.tagName.toLowerCase() === 'input' || 155 e.target.tagName.toLowerCase() === 'textarea' ); 156 157 // Let selectWoo handle Enter on an enhanced-select control instead of submitting the modal. 158 var inEnhancedSelect = $( e.target ).closest( 159 '.select2-container, .select2-selection, .select2-search__field, [role="combobox"]' 160 ).length > 0; 161 162 if ( ! isFormField && ! inEnhancedSelect ) { 163 if ( $( '#btn-ok' ).length ) { 164 this.addButton( e ); 165 } else if ( $( '#btn-next' ).length ) { 166 this.nextButton( e ); 167 } 168 } 169 } 170 171 // ESC key 172 if ( 27 === button ) { 173 this.closeButton( e ); 174 } 175 } 176 }); 177 178}( jQuery, Backbone, _ ));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.