Sha256: 2bc1f3ea024a14a8869bd22c20f40f68b8cf1869466ff89dd86744a8f3f437e3

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

///- - - - - - - - - - - - - - - - - - - -
/// FORM ITEM
///- - - - - - - - - - - - - - - - - - - -

import { _FormItemEditor } from './form_item_editor'

class FormItemRepeater {
	
	constructor()
	{
		this.target = '.form-item--repeater-section'
	}

	isSet()
	{
		if ( $( this.target ).length > 0 ) { return true }
		else { return false }
	}

	setEvents()
	{
		$(document).on('click', this.target + '--add-new', addNewItem )
		
		$(document).on('click', '.form-item--remove-item-with-js', function( event )
		{
			// Stop default behaviour
			event.preventDefault()
			$( this ).parent( this.target ).remove()
			_FormItemEditor.resize()
		})

		$(document).on('click', '.form-item--delete-repeater-item', function( event )
		{
			// Stop default behaviour
			event.preventDefault()

			$.ajax({
				url: $( this ).attr('href'),
				data: { id: $( this ).data('id'), isAjax: true },
				method: "DELETE"
			}).done( ()=>{
				$( this ).parent('li').remove()
				_FormItemEditor.resize()
			})
		})
	}
}

export let _FormItemRepeater = new FormItemRepeater()


///- - - - - - - - - - - - - - - - - - - -
/// COMPONENT HELPER FUNCTIONS
///- - - - - - - - - - - - - - - - - - - -

function addNewItem( event ) 
{
	// Stop default behaviour
	event.preventDefault()
	// Get the child to clone
	let id = $( event.target ).data( 'id' )
	let $list = $('#form-item--repeater-setting-' + id )
	let url = $( event.target ).data( 'url' )
	$.post( url, { repeater_setting_id: id }, function( data )
	{
		let parts = data.split('<!-- SPLIT -->')
		let newRepeater = parts[1]
		$list.append( newRepeater )
		var editor_id = $list.find('textarea').last('textarea').attr('id')
		tinyMCE.EditorManager.execCommand('mceAddEditor',true, editor_id);
		_FormItemEditor.resize()
	})
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
binda-0.0.7 app/assets/javascripts/binda/components/form_item_repeater.js
binda-0.0.6 app/assets/javascripts/binda/components/form_item_repeater.js
binda-0.0.5 app/assets/javascripts/binda/components/form_item_repeater.js