Created
April 10, 2013 11:45
-
-
Save h-lame/5353932 to your computer and use it in GitHub Desktop.
Cope with the fact that what we clone: a) has chosen selects in it that don't like being cloned - we remove their chosen artifacts and re-chosen()-ify them
b) has hidden chosen selects in it that don't re-render properly if they were hidden - we do the same thing that we do on initial load and wait for all of them to declare they are ready befor…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$(function() { | |
var toggle_topics_and_policies = function() { | |
var elem = $(this); | |
var target = elem.parent().data('itemType'); | |
elem.parents('.well').find('.featured-item-type-pane').hide(); | |
elem.parents('.well').find('.featured-item-type-pane[data-item-type='+target+']').show(); | |
}; | |
var wait_for_chosen_to_be_ready_before_toggle_initial_topic_vs_policy_selector = function(container) { | |
// know how many lists to expect | |
var counter = container.find('.featured-item-type-pane .chzn-select-no-deselect').length; | |
container.find('.featured-item-type-pane .chzn-select-no-deselect').on('liszt:ready', function() { | |
// count how many are ready | |
counter--; | |
if (counter < 1) { | |
// once all lists are ready, toggle to hide the unchosen ones | |
// this avoids hidden ones being tiny and wee | |
$(this).parents('.featured-topics-and-policies-items').find('.featured-item-type input[type=radio][checked]').each(toggle_topics_and_policies); | |
} | |
}) | |
}; | |
wait_for_chosen_to_toggle_initial_topic_vs_policy_selector($('.featured-topics-and-policies-items')); | |
// use a "live" selector so we pick up any and all new radio buttons | |
$(document).on('change', ".featured-topics-and-policies-items .featured-item-type input[type=radio]", toggle_topics_and_policies); | |
// hide initial, cloned ones will remain hidden | |
$(".featured-topics-and-policies-items label[for$=ordering]").hide(); | |
// the following is heavily copied from duplicate_fields.js - but we | |
// have very slightly different needs (we want the clone to go onto | |
// the same container (list) as the clonee, we need to trigger some | |
// stuff so the chosen's get re-done correctly | |
var duplicateFeaturedItems = function(e) { | |
e.preventDefault(); | |
var $button = $(e.target), | |
$set = $(".featured-topics-and-policies-items"), | |
$fields = $set.find('.sort_item').last(), | |
$newFields = $fields.clone(); | |
$newFields.find('input[type=text], textarea').val(''); | |
$newFields.find('label,input,textarea,select').each(function(i, el){ | |
var $el = $(el), | |
currentName = $el.attr('name'), | |
currentId = $el.attr('id'), | |
currentFor = $el.attr('for'), | |
index = false; | |
if(currentName && currentName.match(/\[([0-9]+)\]/)){ | |
index = parseInt(currentName.match(/\[([0-9]+)\]/)[1], 10); | |
$el.attr('name', currentName.replace('['+ index +']', '['+ (index+1) +']')); | |
} | |
if(currentId && currentId.match(/_([0-9]+)_/)){ | |
if(index === false){ | |
index = parseInt(currentId.match(/_([0-9]+)_/)[1], 10); | |
} | |
$el.attr('id', currentId.replace('_'+ index +'_', '_'+ (index+1) +'_')); | |
} | |
if(currentFor && currentFor.match(/_([0-9]+)_/)){ | |
if(index === false){ | |
index = parseInt(currentFor.match(/_([0-9]+)_/)[1], 10); | |
} | |
$el.attr('for', currentFor.replace('_'+ index +'_', '_'+ (index+1) +'_')); | |
} | |
}); | |
$fields.parent().append($newFields); | |
$newFields.find('.featured-item-type-pane').show(); | |
// remove old chosen artifacts... | |
$newFields.find('div.chzn-container').remove(); | |
// ...then re-chosen-ify them | |
wait_for_chosen_to_toggle_initial_topic_vs_policy_selector($newFields); | |
$newFields.find('.chzn-done').removeClass('chzn-done').show().chosen(); | |
}; | |
$button = $('<a href="#">Add another</a>'); | |
$(".featured-topics-and-policies-items").append($button); | |
$button.on('click', duplicateFeaturedItems); | |
}) | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment