input-ajax.js
| 1 | /* |
| 2 | * Input Ajax |
| 3 | * |
| 4 | * @description: show / hide metaboxes from changing category / tempalte / etc |
| 5 | * @author: Elliot Condon |
| 6 | * @since: 3.1.4 |
| 7 | */ |
| 8 | |
| 9 | (function($){ |
| 10 | |
| 11 | |
| 12 | /* |
| 13 | * Exists |
| 14 | * |
| 15 | * @description: returns true / false |
| 16 | * @created: 1/03/2011 |
| 17 | */ |
| 18 | |
| 19 | $.fn.exists = function() |
| 20 | { |
| 21 | return $(this).length>0; |
| 22 | }; |
| 23 | |
| 24 | |
| 25 | /* |
| 26 | * Document Ready |
| 27 | * |
| 28 | * @description: adds ajax data |
| 29 | * @created: 1/03/2011 |
| 30 | */ |
| 31 | |
| 32 | $(document).ready(function(){ |
| 33 | |
| 34 | // show metaboxes for this post |
| 35 | acf.data = { |
| 36 | action : 'get_input_metabox_ids', |
| 37 | post_id : acf.post_id, |
| 38 | page_template : false, |
| 39 | page_parent : false, |
| 40 | page_type : false, |
| 41 | page : acf.post_id, |
| 42 | post : acf.post_id, |
| 43 | post_category : false, |
| 44 | post_format : false, |
| 45 | taxonomy : false |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | // add classes |
| 50 | $('#poststuff .postbox[id*="acf_"]').addClass('acf_postbox'); |
| 51 | $('#adv-settings label[for*="acf_"]').addClass('acf_hide_label'); |
| 52 | |
| 53 | // hide acf stuff |
| 54 | $('#poststuff .acf_postbox').hide(); |
| 55 | $('#adv-settings .acf_hide_label').hide(); |
| 56 | |
| 57 | // loop through acf metaboxes |
| 58 | $('#poststuff .postbox.acf_postbox').each(function(){ |
| 59 | |
| 60 | // vars |
| 61 | var options = $(this).find('.inside > .options'); |
| 62 | var show = options.attr('data-show'); |
| 63 | var layout = options.attr('data-layout'); |
| 64 | var id = $(this).attr('id').replace('acf_', ''); |
| 65 | |
| 66 | // layout |
| 67 | $(this).addClass('acf_postbox').addClass(layout); |
| 68 | |
| 69 | // show / hide |
| 70 | if(show == 'true') |
| 71 | { |
| 72 | $(this).show(); |
| 73 | $('#adv-settings .acf_hide_label[for="acf_' + id + '-hide"]').show(); |
| 74 | } |
| 75 | |
| 76 | }); |
| 77 | |
| 78 | }); |
| 79 | |
| 80 | |
| 81 | /* |
| 82 | * update_fields |
| 83 | * |
| 84 | * @description: finds the new id's for metaboxes and show's hides metaboxes |
| 85 | * @created: 1/03/2011 |
| 86 | */ |
| 87 | |
| 88 | function update_fields() |
| 89 | { |
| 90 | $.ajax({ |
| 91 | url: ajaxurl, |
| 92 | data: acf.data, |
| 93 | type: 'post', |
| 94 | dataType: 'json', |
| 95 | success: function(result){ |
| 96 | |
| 97 | // hide all metaboxes |
| 98 | $('#poststuff .acf_postbox').hide(); |
| 99 | $('#adv-settings .acf_hide_label').hide(); |
| 100 | |
| 101 | // show the new postboxes |
| 102 | $.each(result, function(k, v) { |
| 103 | $('#poststuff #acf_' + v).show(); |
| 104 | $('#adv-settings .acf_hide_label[for="acf_' + v + '-hide"]').show(); |
| 105 | }); |
| 106 | |
| 107 | // load style |
| 108 | $.ajax({ |
| 109 | url: ajaxurl, |
| 110 | data: { |
| 111 | action : 'get_input_style', |
| 112 | acf_id : result[0] |
| 113 | }, |
| 114 | type: 'post', |
| 115 | dataType: 'html', |
| 116 | success: function(result){ |
| 117 | |
| 118 | $('#acf_style').html(result); |
| 119 | |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | } |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /* |
| 129 | * update_fields (Live change events) |
| 130 | * |
| 131 | * @description: call the update_fields function on live events |
| 132 | * @created: 1/03/2011 |
| 133 | */ |
| 134 | |
| 135 | $('#page_template').live('change', function(){ |
| 136 | |
| 137 | acf.data.page_template = $(this).val(); |
| 138 | update_fields(); |
| 139 | |
| 140 | }); |
| 141 | |
| 142 | $('#parent_id').live('change', function(){ |
| 143 | |
| 144 | var page_parent = $(this).val(); |
| 145 | |
| 146 | if($(this).val() != "") |
| 147 | { |
| 148 | acf.data.page_type = 'child'; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | acf.data.page_type = 'parent'; |
| 153 | } |
| 154 | |
| 155 | update_fields(); |
| 156 | |
| 157 | }); |
| 158 | |
| 159 | $('#categorychecklist input[type="checkbox"]').live('change', function(){ |
| 160 | |
| 161 | acf.data.post_category = ['0']; |
| 162 | |
| 163 | $('#categorychecklist :checked').each(function(){ |
| 164 | acf.data.post_category.push($(this).val()) |
| 165 | }); |
| 166 | |
| 167 | //console.log(data.post_category); |
| 168 | |
| 169 | update_fields(); |
| 170 | |
| 171 | }); |
| 172 | |
| 173 | |
| 174 | $('#post-formats-select input[type="radio"]').live('change', function(){ |
| 175 | |
| 176 | acf.data.post_format = $(this).val(); |
| 177 | update_fields(); |
| 178 | |
| 179 | }); |
| 180 | |
| 181 | // taxonomy |
| 182 | $('div[id*="taxonomy-"] input[type="checkbox"]').live('change', function(){ |
| 183 | |
| 184 | acf.data.taxonomy = ['0']; |
| 185 | |
| 186 | $(this).closest('ul').find('input[type="checkbox"]:checked').each(function(){ |
| 187 | acf.data.taxonomy.push($(this).val()) |
| 188 | }); |
| 189 | |
| 190 | update_fields(); |
| 191 | |
| 192 | }); |
| 193 | |
| 194 | |
| 195 | })(jQuery); |