redux-slides.js (4669B)
1 /*global redux_change, redux, jQuery */ 2 3 (function( $ ) { 4 'use strict'; 5 6 redux.field_objects = redux.field_objects || {}; 7 redux.field_objects.slides = redux.field_objects.slides || {}; 8 9 redux.field_objects.slides.init = function( selector ) { 10 selector = $.redux.getSelector( selector, 'slides' ); 11 12 $( selector ).each( 13 function() { 14 var el = $( this ); 15 var parent = el; 16 17 redux.field_objects.media.init( el ); 18 19 if ( ! el.hasClass( 'redux-field-container' ) ) { 20 parent = el.parents( '.redux-field-container:first' ); 21 } 22 23 if ( parent.is( ':hidden' ) ) { 24 return; 25 } 26 27 if ( parent.hasClass( 'redux-container-slides' ) ) { 28 parent.addClass( 'redux-field-init' ); 29 } 30 31 if ( parent.hasClass( 'redux-field-init' ) ) { 32 parent.removeClass( 'redux-field-init' ); 33 } else { 34 return; 35 } 36 37 el.find( '.redux-slides-remove' ).on( 38 'click', 39 function() { 40 var slideCount; 41 var contentNewTitle; 42 43 redux_change( $( this ) ); 44 45 $( this ).parent().siblings().find( 'input[type="text"]' ).val( '' ); 46 $( this ).parent().siblings().find( 'textarea' ).val( '' ); 47 $( this ).parent().siblings().find( 'input[type="hidden"]' ).val( '' ); 48 49 slideCount = $( this ).parents( '.redux-container-slides:first' ).find( '.redux-slides-accordion-group' ).length; 50 51 if ( slideCount > 1 ) { 52 $( this ).parents( '.redux-slides-accordion-group:first' ).slideUp( 53 'medium', 54 function() { 55 $( this ).remove(); 56 } 57 ); 58 } else { 59 contentNewTitle = $( this ).parent( '.redux-slides-accordion' ).data( 'new-content-title' ); 60 61 $( this ).parents( '.redux-slides-accordion-group:first' ).find( '.remove-image' ).trigger( 'click' ); 62 $( this ).parents( '.redux-container-slides:first' ).find( '.redux-slides-accordion-group:last' ).find( '.redux-slides-header' ).text( contentNewTitle ); 63 } 64 } 65 ); 66 67 el.find( '.redux-slides-add' ).off( 'click' ).on( 68 'click', 69 function() { 70 var contentNewTitle; 71 72 var newSlide = $( this ).prev().find( '.redux-slides-accordion-group:last' ).clone( true ); 73 var slideCount = $( newSlide ).find( '.slide-title' ).attr( 'name' ).match( /[0-9]+(?!.*[0-9])/ ); 74 var slideCount1 = slideCount * 1 + 1; 75 76 $( newSlide ).find( 'input[type="text"], input[type="hidden"], textarea' ).each( 77 function() { 78 $( this ).attr( 'name', $( this ).attr( 'name' ).replace( /[0-9]+(?!.*[0-9])/, slideCount1 ) ).attr( 'id', $( this ).attr( 'id' ).replace( /[0-9]+(?!.*[0-9])/, slideCount1 ) ); 79 80 $( this ).val( '' ); 81 82 if ( $( this ).hasClass( 'slide-sort' ) ) { 83 $( this ).val( slideCount1 ); 84 } 85 } 86 ); 87 88 contentNewTitle = $( this ).prev().data( 'new-content-title' ); 89 90 $( newSlide ).find( '.screenshot' ).removeAttr( 'style' ); 91 $( newSlide ).find( '.screenshot' ).addClass( 'hide' ); 92 $( newSlide ).find( '.screenshot a' ).attr( 'href', '' ); 93 $( newSlide ).find( '.remove-image' ).addClass( 'hide' ); 94 $( newSlide ).find( '.redux-slides-image' ).attr( 'src', '' ).removeAttr( 'id' ); 95 $( newSlide ).find( 'h3' ).text( '' ).append( '<span class="redux-slides-header">' + contentNewTitle + '</span><span class="ui-accordion-header-icon ui-icon ui-icon-plus"></span>' ); 96 $( this ).prev().append( newSlide ); 97 } 98 ); 99 100 el.find( '.slide-title' ).on( 101 'keyup', 102 function( event ) { 103 var newTitle = event.target.value; 104 $( this ).parents().eq( 3 ).find( '.redux-slides-header' ).text( newTitle ); 105 } 106 ); 107 108 el.find( '.redux-slides-accordion' ).accordion( 109 { 110 header: '> div > fieldset > h3', 111 collapsible: true, 112 active: false, 113 heightStyle: 'content', 114 icons: { 115 'header': 'ui-icon-plus', 116 'activeHeader': 'ui-icon-minus' 117 } 118 } 119 ).sortable( 120 { 121 axis: 'y', 122 handle: 'h3', 123 connectWith: '.redux-slides-accordion', 124 start: function( e, ui ) { 125 e = null; 126 ui.placeholder.height( ui.item.height() ); 127 ui.placeholder.width( ui.item.width() ); 128 }, 129 placeholder: 'ui-state-highlight', 130 stop: function( event, ui ) { 131 var inputs; 132 133 event = null; 134 135 // IE doesn't register the blur when sorting 136 // so trigger focusout handlers to remove .ui-state-focus. 137 ui.item.children( 'h3' ).triggerHandler( 'focusout' ); 138 inputs = $( 'input.slide-sort' ); 139 inputs.each( 140 function( idx ) { 141 $( this ).val( idx ); 142 } 143 ); 144 } 145 } 146 ); 147 } 148 ); 149 }; 150 })( jQuery );