Editor.js 3.19 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
/**
 * Sidebar Editor View
 *
 * @since    3.16.0
 * @version  3.27.0
 */
define( [
		'Views/LessonEditor',
		'Views/Quiz',
		'Views/Assignment',
		'Views/_Subview'
	], function(
		LessonEditor,
		Quiz,
		Assignment,
		Subview
	) {

		return Backbone.View.extend( _.defaults( {

			/**
			 * Current view state
			 *
			 * @type  {String}
			 */
			state: 'lesson', // [lesson|quiz]

			/**
			 * Current Subviews
			 *
			 * @type  {Object}
			 */
			views: {
				lesson: {
					class: LessonEditor,
					instance: null,
					state: 'lesson',
				},
				assignment: {
					class: Assignment,
					instance: null,
					state: 'assignment',
				},
				quiz: {
					class: Quiz,
					instance: null,
					state: 'quiz',
				},
			},

			/**
			 * HTML element selector
			 *
			 * @type  {String}
			 */
			el: '#llms-editor',

			events: {
				'click .llms-editor-nav a[href="#llms-editor-close"]': 'close_editor',
				'click .llms-editor-nav a:not([href="#llms-editor-close"])': 'switch_tab',
			},

			/**
			 * Wrapper Tag name
			 *
			 * @type  {String}
			 */
			tagName: 'div',

			/**
			 * Get the underscore template
			 *
			 * @type  {[type]}
			 */
			template: wp.template( 'llms-editor-template' ),

			/**
			 * Initialization callback func (renders the element on screen)
			 *
			 * @return   void
			 * @since    3.16.0
			 * @version  3.16.0
			 */
			initialize: function( data ) {

				this.SidebarView = data.SidebarView;
				if ( data.tab ) {
					this.state = data.tab;
				}

			},

			/**
			 * Compiles the template and renders the view
			 *
			 * @return   self (for chaining)
			 * @since    3.16.0
			 * @version  3.16.0
			 */
			render: function( view_data ) {

				view_data = view_data || {};

				this.$el.html( this.template( this ) );

				this.render_subviews( _.extend( view_data, {
					lesson: this.model,
				} ) );

				return this;

			},

			/**
			 * Click event for close sidebar editor button
			 * Sends event to main SidebarView to trigger editor closing events
			 *
			 * @param    obj   event  js event obj
			 * @return   void
			 * @since    3.16.0
			 * @version  3.27.0
			 */
			close_editor: function( event ) {

				event.preventDefault();
				Backbone.pubSub.trigger( 'sidebar-editor-close' );
				window.location.hash = '';

			},

			/**
			 * Click event for switching tabs in the editor navigation
			 *
			 * @param    object  event  js event object
			 * @return   void
			 * @since    3.16.0
			 * @version  3.27.0
			 */
			switch_tab: function( event ) {

				event.preventDefault();

				var $btn = $( event.target ),
				view     = $btn.attr( 'data-view' ),
				$tab     = this.$el.find( $btn.attr( 'href' ) );

				this.set_state( view ).render();
				this.set_hash( view );

				// Backbone.pubSub.trigger( 'editor-tab-activated', $btn.attr( 'href' ).substring( 1 ) );
			},

			/**
			 * Adds a hash for deep linking to a specific lesson tab
			 *
			 * @param  string  subtab subtab [quiz|assignment]
			 * @return void
			 * @since   3.27.0
			 * @version 3.27.0
			 */
			set_hash: function( subtab ) {

				var hash = 'lesson:' + this.model.get( 'id' );

				if ( 'lesson' !== subtab ) {
					hash += ':' + subtab;
				}

				window.location.hash = hash;

			},

		}, Subview ) );

} );