|
| 1 | +$(function() { |
| 2 | + var translations; |
| 3 | + translations = function() { |
| 4 | + var toggleTab, updateLocaleButtonsStatus; |
| 5 | + updateLocaleButtonsStatus = function($dom) { |
| 6 | + var $localeList; |
| 7 | + $localeList = $dom.find('.add-locale ul li:not(.hidden)'); |
| 8 | + if ($localeList.length === 0) { |
| 9 | + return $dom.find('.add-locale').hide(); |
| 10 | + } else { |
| 11 | + return $dom.find('.add-locale').show(); |
| 12 | + } |
| 13 | + }; |
| 14 | + toggleTab = function($tab, active) { |
| 15 | + var $addButton; |
| 16 | + $addButton = $tab.parents('ul').find('.add-locale li:has(a[href="' + $tab.attr('href') + '"])'); |
| 17 | + if (active) { |
| 18 | + $tab.addClass('hidden').show().removeClass('hidden'); |
| 19 | + return $addButton.hide().addClass('hidden'); |
| 20 | + } else { |
| 21 | + $tab.addClass('hidden').hide().addClass('hidden'); |
| 22 | + return $addButton.show().removeClass('hidden'); |
| 23 | + } |
| 24 | + }; |
| 25 | + return $(".activeadmin-translations > ul").each(function() { |
| 26 | + var $addLocaleButton, $contents, $dom, $form, $removeButton, $tabs, availableLocales, showAction; |
| 27 | + $dom = $(this); |
| 28 | + showAction = $dom.hasClass('locale-selector'); |
| 29 | + if (!$dom.data("ready")) { |
| 30 | + $dom.data("ready", true); |
| 31 | + $tabs = $("li > a", this); |
| 32 | + $contents = showAction ? $(this).siblings("div.field-translation") : $(this).siblings("fieldset"); |
| 33 | + $tabs.click(function(e) { |
| 34 | + var $tab; |
| 35 | + $tab = $(this); |
| 36 | + $tabs.not($tab).removeClass("active"); |
| 37 | + $tab.addClass("active"); |
| 38 | + $contents.hide(); |
| 39 | + $contents.filter($tab.attr("href")).show(); |
| 40 | + return e.preventDefault(); |
| 41 | + }); |
| 42 | + $tabs.eq(0).click(); |
| 43 | + if (showAction) { |
| 44 | + return; |
| 45 | + } |
| 46 | + availableLocales = []; |
| 47 | + $tabs.not('.default').each(function() { |
| 48 | + return availableLocales.push($('<li></li>').append($(this).clone().removeClass('active'))); |
| 49 | + }); |
| 50 | + $addLocaleButton = $('<li class="add-locale"><a href="#">+</a></li>'); |
| 51 | + $addLocaleButton.append($('<ul></ul>').append(availableLocales)); |
| 52 | + $addLocaleButton.find('ul a').click(function(e) { |
| 53 | + var $tab, href; |
| 54 | + href = $(this).attr('href'); |
| 55 | + $tab = $tabs.filter('[href="' + href + '"]'); |
| 56 | + toggleTab($tab, true); |
| 57 | + $tab.click(); |
| 58 | + updateLocaleButtonsStatus($dom); |
| 59 | + return e.preventDefault(); |
| 60 | + }); |
| 61 | + $removeButton = $('<span class="remove">x</span>').click(function(e) { |
| 62 | + var $tab; |
| 63 | + e.stopImmediatePropagation(); |
| 64 | + e.preventDefault(); |
| 65 | + $tab = $(this).parent(); |
| 66 | + toggleTab($tab, false); |
| 67 | + if ($tab.hasClass('active')) { |
| 68 | + $tabs.not('.hidden').eq(0).click(); |
| 69 | + } |
| 70 | + return updateLocaleButtonsStatus($dom); |
| 71 | + }); |
| 72 | + $tabs.not('.default').append($removeButton); |
| 73 | + $dom.append($addLocaleButton); |
| 74 | + $tabs.each(function() { |
| 75 | + var $content, $tab, containsErrors, hide; |
| 76 | + $tab = $(this); |
| 77 | + $content = $contents.filter($tab.attr("href")); |
| 78 | + containsErrors = $content.find(".input.error").length > 0; |
| 79 | + $tab.toggleClass("error", containsErrors); |
| 80 | + hide = true; |
| 81 | + if ($tab.hasClass('error') || $tab.hasClass('default')) { |
| 82 | + hide = false; |
| 83 | + } else { |
| 84 | + $content.find('[name]').not('[type="hidden"]').each(function() { |
| 85 | + if ($(this).val()) { |
| 86 | + hide = false; |
| 87 | + return false; |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | + if (hide) { |
| 92 | + return toggleTab($tab, false); |
| 93 | + } else { |
| 94 | + return toggleTab($tab, true); |
| 95 | + } |
| 96 | + }); |
| 97 | + $form = $dom.parents('form'); |
| 98 | + if (!$form.data('ready')) { |
| 99 | + $form.data('ready'); |
| 100 | + $form.submit(function() { |
| 101 | + return $('.activeadmin-translations > ul').each(function() { |
| 102 | + var $fieldsets; |
| 103 | + $fieldsets = $(this).siblings('fieldset'); |
| 104 | + return $("li:not(.add-locale) > a", this).each(function() { |
| 105 | + var $currentFieldset, $destroy, $translationId; |
| 106 | + if ($(this).hasClass('hidden')) { |
| 107 | + $currentFieldset = $("fieldset" + ($(this).attr('href'))); |
| 108 | + $translationId = $('input[id$=_id]', $currentFieldset); |
| 109 | + if ($translationId.val()) { |
| 110 | + $destroy = $('<input/>').attr({ |
| 111 | + type: 'hidden', |
| 112 | + name: $translationId.attr('name').replace('[id]', '[_destroy]'), |
| 113 | + id: $translationId.attr('id').replace('_id', '_destroy'), |
| 114 | + value: '1' |
| 115 | + }); |
| 116 | + return $destroy.appendTo($currentFieldset); |
| 117 | + } else { |
| 118 | + return $fieldsets.filter($(this).attr('href')).remove(); |
| 119 | + } |
| 120 | + } |
| 121 | + }); |
| 122 | + }); |
| 123 | + }); |
| 124 | + } |
| 125 | + updateLocaleButtonsStatus($dom); |
| 126 | + return $tabs.filter('.default').click(); |
| 127 | + } |
| 128 | + }); |
| 129 | + }; |
| 130 | + $("a").bind("click", function() { |
| 131 | + return setTimeout(function() { |
| 132 | + return translations(); |
| 133 | + }, 50); |
| 134 | + }); |
| 135 | + $('a.ui-translation-trigger').click(function(e) { |
| 136 | + var $locale, $td; |
| 137 | + $locale = $(this).data('locale'); |
| 138 | + $td = $(this).closest('td'); |
| 139 | + $('.field-translation', $td).hide(); |
| 140 | + $(".locale-" + $locale, $td).show(); |
| 141 | + $(this).parent().children('a.ui-translation-trigger').removeClass('active'); |
| 142 | + $(this).addClass('active'); |
| 143 | + return e.preventDefault(); |
| 144 | + }); |
| 145 | + return translations(); |
| 146 | +}); |
0 commit comments