llms-metabox-instructors.js 1.29 KB
Newer Older
cyrille's avatar
cyrille committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/**
 * Instructors Metabox
 *
 * @since    3.13.0
 * @version  3.13.0
 */
( function( $ ) {

	window.llms = window.llms || {};

	window.llms.metabox_instructors = function() {

		/**
		 * Initialize
		 *
		 * @return  void
		 * @since   3.13.0
		 * @version 3.13.0
		 */
		this.init = function() {

			// before saving, update the wp core hidden field for post_author
			// so that the first instructor is always set as the post author
			$( '._llms_instructors_data.repeater' ).on( 'llms-repeater-before-save', function( e, params ) {
				var author_id = params.$el.find( '.llms-repeater-rows .llms-repeater-row' ).first().find( 'select[name^="_llms_id"]' ).val();
				$( '#post_author' ).val( author_id );
			} );

			$( '._llms_instructors_data.repeater' ).on( 'llms-new-repeater-row', function( e, params ) {

				var $instructor = params.$row.find( 'select[name^="_llms_id"]' ),
					$target     = params.$row.find( '.llms-repeater-title' );

				$instructor.on( 'select2:select', function( e ) {
					if ( ! e.params ) {
						$target.html( $instructor.find( 'option[selected="selected"]' ).html() );
					} else {
						$target.text( e.params.data.text );
					}
				} ).trigger( 'select2:select' );

			} );

		};

		// go
		this.init();

	};

	var a = new window.llms.metabox_instructors();

} )( jQuery );