//= require rails_admin/custom/ckeditor_ajax (function($) { function nearestField($input) { return $input.closest('.form-group, .control-group'); } function buildProductLocaleTabs() { var $form = $('form').filter(function() { return $(this).find('[name="product[name]"]').length > 0; }).first(); if ($form.length === 0) { return; } var fieldMap = [ { key: 'zh-CN', label: '简体中文', nameField: nearestField($form.find('[name="product[name]"]')), detailField: nearestField($form.find('[name="product[detail]"]')) }, { key: 'en', label: 'English', nameField: nearestField($form.find('[name="product[name_en]"]')), detailField: nearestField($form.find('[name="product[detail_en]"]')) }, { key: 'ru', label: 'Русский', nameField: nearestField($form.find('[name="product[name_ru]"]')), detailField: nearestField($form.find('[name="product[detail_ru]"]')) }, { key: 'zh-TW', label: '繁體中文', nameField: nearestField($form.find('[name="product[name_tw]"]')), detailField: nearestField($form.find('[name="product[detail_tw]"]')) } ]; if ($form.find('.product-locale-tabs').length > 0) { return; } var hasAllFields = true; $.each(fieldMap, function(_, group) { if (group.nameField.length === 0 || group.detailField.length === 0) { hasAllFields = false; return false; } }); if (!hasAllFields) { return; } var $anchor = fieldMap[0].nameField; var $mount = $('
'); $anchor.before($mount); var $tabs = $('
'); var $header = $('

商品多语言内容

名称与详情按语言分别维护,未填写时接口默认回退到简体中文。

'); var $nav = $(''); var $content = $('
'); $.each(fieldMap, function(index, group) { var tabId = 'product-locale-tab-' + group.key.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase(); var activeClass = index === 0 ? 'active' : ''; var $navItem = $(''); var $link = $(''); $link.attr('href', '#' + tabId); $link.append($('').text(group.label)); $link.append($('').text(group.key)); $navItem.append($link); $nav.append($navItem); var $pane = $('
'); $pane.append(group.nameField); $pane.append(group.detailField); $content.append($pane); }); $tabs.append($header).append($nav).append($content); $mount.replaceWith($tabs); } $(document).on('ready page:load turbolinks:load', buildProductLocaleTabs); })(jQuery);