var changeOrder = function (elem) { var input_id, input_order; input_id = elem.find('fieldset:first [name$=id]:first').val(); input_order = elem.find('fieldset:first [name$=field_order]:first').val(); $.ajax({ url: FormilyRoutes.input_update_path(input_id), data: {field_order: input_order}, type: 'PUT' }); }; $(document).ready(function(){ $(document).on('click', 'a[data-formily], button[data-formily]', function(e){ var self, btnType, element, container, formilyModal; self = $(this); btnType = self.data('formily'); element = self.parents('.ff-input-container:first'); container = element.parents('li:first'); formilyModal = $('#formily_modal'); window.InputModel.clear(); window.InputModel.getFromFieldset(element.find('fieldset:first')); switch(btnType){ case 'add_input': window.InputModel.clear(); window.InputModel.type = 'Formily::TextInput'; window.FormilyEditor.dialogType = 'input'; window.FormilyEditor.actionType = 'add'; window.InputModel.field_order = $('ul#formily_sortable_inputs>li').length + 1; formilyModal.dialog('option', 'title', 'Add New Input'); formilyModal.dialog('open'); break; case 'edit_input': window.FormilyEditor.dialogType = 'input'; window.FormilyEditor.actionType = 'edit'; window.FormilyEditor.element = element; formilyModal.dialog('option', 'title', 'Edit Input'); formilyModal.dialog('open'); break; case 'add_group_input': window.InputModel.clear(); window.FormilyEditor.dialogType = 'group'; window.FormilyEditor.actionType = 'add'; window.InputModel.type = self.data('group'); window.InputModel.group_input_id = self.parents('li:first').find('.ff-input-container fieldset:first input[name$=id]').val(); window.InputModel.field_order = self.parents('li:first').find('ul.ff-inputs-group-list>li').length + 1; window.FormilyEditor.element = self.parent().find('.ff-inputs-group-list'); formilyModal.dialog('option', 'title', 'Add New Group Input'); formilyModal.dialog('open'); break; case 'edit_group_input': window.FormilyEditor.dialogType = 'group'; window.FormilyEditor.actionType = 'edit'; window.FormilyEditor.element = element; formilyModal.dialog('open'); break; case 'up': var el1 = container; var el2, update_all; el2 = el1.prev(); if(el2.length == 0) { el2 = el1.parent().children('li:last'); el2.after(el1); //TODO: need update all fields update_all = true; } else { el1.after(el2); update_all = false; } if(el1.parents('li').length > 0) { window.FormilyEditor.inputsGroupUpdate(el1.parents('.ff-inputs-group-list')); } else { window.FormilyEditor.inputsUpdate(); } if(update_all) { el1.parent().find('li').each(function(_, elem){ changeOrder($(elem).find('.ff-input-container:first')); }); }else{ changeOrder(el1); changeOrder(el2); } $(document).trigger('formily_form_updated'); break; case 'down': var el1 = container; var el2 ; el2 = el1.next(); if(el2.length == 0) { el2 = el1.parent().children('li:first'); el2.before(el1); update_all = true; } else { el1.before(el2); update_all = false; } if(el1.parents('li').length > 0) { window.FormilyEditor.inputsGroupUpdate(el1.parents('.ff-inputs-group-list')); } else { window.FormilyEditor.inputsUpdate(); } if(update_all) { el1.parent().find('li').each(function(_, elem){ changeOrder($(elem).find('.ff-input-container:first')); }); }else{ changeOrder(el1); changeOrder(el2); } $(document).trigger('formily_form_updated'); break; case 'delete': // Delete input container.hide(300, function(){ if(confirm('Delete input ?')){ var elem = container.parents('ul.ff-inputs-group-list'); var parent = container.parents('ul:first'); container.remove(); if(elem.length > 0) { window.FormilyEditor.inputsGroupUpdate(elem); } else { window.FormilyEditor.inputsUpdate(); } if(window.InputModel.id && window.InputModel.id > 0) { $.ajax({ url: FormilyRoutes.input_delete_path(InputModel.id), type: 'DELETE', success: function(){ parent.children('li').each(function(_, elem){ changeOrder($(elem).find('.ff-input-container:first')); }); $(document).trigger('formily_form_updated'); } }); } } else { container.show(0); } }); break; default: console.warn('Wrong formily data attr'); } return false; }); });