|
| 1 | +$(function() { |
| 2 | + |
| 3 | + var initExtentForm = function(subform) { |
| 4 | + |
| 5 | + var $that = $("[name$='[extent_type]']", subform); |
| 6 | + if ($that.val() === 'duration') { |
| 7 | + |
| 8 | + var values = {}; |
| 9 | + |
| 10 | + if ($(".extent-subform", subform).length) { |
| 11 | + values = $(".extent-subform", subform).serializeObject(); |
| 12 | + $(".extent-subform", subform).remove(); |
| 13 | + } |
| 14 | + |
| 15 | + var index = $that.parents("[data-index]:first").data("index"); |
| 16 | + |
| 17 | + var template_data = { |
| 18 | + path: AS.quickTemplate($that.parents("[data-name-path]:first").data("name-path"), {index: index}), |
| 19 | + id_path: AS.quickTemplate($that.parents("[data-id-path]:first").data("id-path"), {index: index}), |
| 20 | + index: index |
| 21 | + }; |
| 22 | + |
| 23 | + var template_name = 'template_extent_duration'; |
| 24 | + |
| 25 | + var $extent_subform = $(AS.renderTemplate(template_name, template_data)); |
| 26 | + |
| 27 | + $that.parents(".control-group:first").after($extent_subform); |
| 28 | + |
| 29 | + $extent_subform.setValuesFromObject(values); |
| 30 | + |
| 31 | + }; |
| 32 | + |
| 33 | + |
| 34 | + $("[name$='[extent_type]']", subform).change(function(event) { |
| 35 | + var type = $(this).val(); |
| 36 | + |
| 37 | + var values = {}; |
| 38 | + |
| 39 | + if ($(".extent-subform", subform).length) { |
| 40 | + values = $(".extent-subform", subform).serializeObject(); |
| 41 | + $(".extent-subform", subform).remove(); |
| 42 | + } |
| 43 | + |
| 44 | + var index = $(this).parents("[data-index]:first").data("index"); |
| 45 | + |
| 46 | + var template_data = { |
| 47 | + path: AS.quickTemplate($(this).parents("[data-name-path]:first").data("name-path"), {index: index}), |
| 48 | + id_path: AS.quickTemplate($(this).parents("[data-id-path]:first").data("id-path"), {index: index}), |
| 49 | + index: index |
| 50 | + }; |
| 51 | + |
| 52 | + var template_name = (type === 'duration') ? "template_extent_duration" : "template_extent_number"; |
| 53 | + |
| 54 | + var $extent_subform = $(AS.renderTemplate(template_name, template_data)); |
| 55 | + |
| 56 | + $(this).parents(".control-group:first").after($extent_subform); |
| 57 | + |
| 58 | + $extent_subform.setValuesFromObject(values); |
| 59 | + |
| 60 | + $(document).triggerHandler("subrecordcreated.aspace", ["extent_type", $extent_subform]); |
| 61 | + }); |
| 62 | + }; |
| 63 | + |
| 64 | + |
| 65 | + $(document).bind("subrecordcreated.aspace", function(event, object_name, subform) { |
| 66 | + if (object_name === "extent") { |
| 67 | + initExtentForm($(subform)); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | +}); |
0 commit comments