Section.js 4.39 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
/**
 * Section Model
 *
 * @since 3.16.0
 * @version 4.20.0
 */
define( [ 'Collections/Lessons', 'Models/_Relationships' ], function( Lessons, Relationships ) {

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

		relationships: {
			parent: {
				model: 'course',
				type: 'model',
			},
			children: {
				lessons: {
					class: 'Lessons',
					model: 'lesson',
					type: 'collection',
				},
			}
		},

		/**
		 * New section defaults
		 *
		 * @since 3.16.0
		 *
		 * @return {Object}
		 */
		defaults: function() {
			return {
				id: _.uniqueId( 'temp_' ),
				lessons: [],
				order: this.collection ? this.collection.length + 1 : 1,
				parent_course: window.llms_builder.course.id,
				title: LLMS.l10n.translate( 'New Section' ),
				type: 'section',

				_expanded: false,
				_selected: false,
			};
		},

		/**
		 * Initialize
		 *
		 * @since 3.16.0
		 *
		 * @return {void}
		 */
		initialize: function() {

			this.startTracking();
			this.init_relationships();

		},

		/**
		 * Add a lesson to the section
		 *
		 * @since 3.16.0
		 * @since 3.16.11 Unknown.
		 *
		 * @param {Object} data    Hash of lesson data (creates new lesson)
		 *                         or existing lesson as a Backbone.Model.
		 * @param {Object} options Hash of options.
		 * @return {Object} Backbone.Model of the new/updated lesson.
		 */
		add_lesson: function( data, options ) {

			data    = data || {};
			options = options || {};

			if ( data instanceof Backbone.Model ) {
				data.set( 'parent_section', this.get( 'id' ) );
				data.set_parent( this );
			} else {
				data.parent_section = this.get( 'id' );
			}

			return this.get( 'lessons' ).add( data, options );

		},

		/**
		 * Retrieve the translated post type name for the model's type
		 *
		 * @since 3.16.12
		 *
		 * @param {Boolean} plural If true, returns the plural, otherwise returns singular.
		 * @return {String}
		 */
		get_l10n_type: function( plural ) {

			if ( plural ) {
				return LLMS.l10n.translate( 'sections' );
			}

			return LLMS.l10n.translate( 'section' );
		},

		/**
		 * Get next section in the collection
		 *
		 * @since 3.16.11
		 *
		 * @param {boolean} circular If true handles the collection in a circle.
		 *                           If current is the last section, returns the first section.
		 * @return {Object}|false
		 */
		get_next: function( circular ) {
			return this._get_sibling( 'next', circular );
		},

		/**
		 * Retrieve a reference to the parent course of the section
		 *
		 * @since 4.14.0
		 *
		 * @return {Object}
		 */
		get_course: function() {

			// When working with an unsaved draft course the parent isn't properly set on the creation of a section.
			if ( ! this.get_parent() ) {
				this.set_parent( window.llms_builder.CourseModel );
			}

			return this.get_parent();

		},

		/**
		 * Get prev section in the collection
		 *
		 * @since 3.16.11
		 *
		 * @param {Boolean} circular If true handles the collection in a circle.
		 *                           If current is the first section, returns the last section.
		 * @return {Object}|false
		 */
		get_prev: function( circular ) {
			return this._get_sibling( 'prev', circular );
		},

		/**
		 * Get a sibling section
		 *
		 * @since 3.16.11
		 * @since 4.20.0 Fix case when the last section was returned when looking for the prev of the first section and not `circular`.
		 *
		 * @param {String}  direction Siblings direction [next|prev].
		 * @param {Boolean} circular  If true handles the collection in a circle.
		 *                            If current is the last section, returns the first section.
		 *                            If current is the first section, returns the last section.
		 * @return {Object}|false
		 */
		_get_sibling: function( direction, circular ) {

			circular = ( 'undefined' === circular ) ? true : circular;

			var max   = this.collection.size() - 1,
				index = this.collection.indexOf( this ),
				sibling_index;

			if ( 'next' === direction ) {
				sibling_index = index + 1;
			} else if ( 'prev' === direction ) {
				sibling_index = index - 1;
			}

			// Don't retrieve greater than max or less than min.
			if ( sibling_index <= max || sibling_index >= 0 ) {

				return this.collection.at( sibling_index );

			} else if ( circular ) {

				if ( 'next' === direction ) {
					return this.collection.first();
				} else if ( 'prev' === direction ) {
					return this.collection.last();
				}

			}

			return false;

		},

	}, Relationships ) );

} );