admin-settings.js
111 lines
| 1 | (function (FOOPluginBaseAdminSettings, $, undefined) { |
| 2 | |
| 3 | FOOPluginBaseAdminSettings.media_uploader = false; |
| 4 | |
| 5 | FOOPluginBaseAdminSettings.setupImageUploader = function() { |
| 6 | $('.image-upload-button').on('click', function(e) { |
| 7 | var $button = $(this), |
| 8 | linkedToId = $button.data('link'), |
| 9 | $linkedToInput = $('#' + linkedToId); |
| 10 | |
| 11 | e.preventDefault(); |
| 12 | |
| 13 | //if the media frame already exists, reopen it. |
| 14 | if ( FOOPluginBaseAdminSettings.media_uploader !== false ) { |
| 15 | // Open frame |
| 16 | FOOPluginBaseAdminSettings.media_uploader.open(); |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | // Create the media frame. |
| 21 | FOOPluginBaseAdminSettings.media_uploader = wp.media.frames.file_frame = wp.media({ |
| 22 | title: $button.data( 'uploader-title' ), |
| 23 | button: { |
| 24 | text: $button.data( 'uploader-button-text' ) |
| 25 | }, |
| 26 | multiple: false // Set to true to allow multiple files to be selected |
| 27 | }); |
| 28 | |
| 29 | // When an image is selected, run a callback. |
| 30 | FOOPluginBaseAdminSettings.media_uploader |
| 31 | .on( 'select', function() { |
| 32 | var attachments = FOOPluginBaseAdminSettings.media_uploader.state().get('selection').toJSON(); |
| 33 | |
| 34 | $.each(attachments, function(i, item) { |
| 35 | var attachment = { |
| 36 | id : item.id, |
| 37 | width: item.sizes.thumbnail.width, |
| 38 | height: item.sizes.thumbnail.height, |
| 39 | src: item.sizes.thumbnail.url |
| 40 | }; |
| 41 | |
| 42 | //do something with the attachment |
| 43 | $linkedToInput.val( attachment.src ); |
| 44 | }); |
| 45 | }) |
| 46 | .on( 'open', function() { |
| 47 | if ($linkedToInput.val().length > 0) { |
| 48 | var selection = FOOPluginBaseAdminSettings.media_uploader.state().get('selection'); |
| 49 | selection.set(); //clear any previous selections |
| 50 | var url = $linkedToInput.val(), |
| 51 | attachment = wp.media.attachment(); |
| 52 | attachment.fetch(); |
| 53 | selection.add( attachment ? [ attachment ] : [] ); |
| 54 | } |
| 55 | }); |
| 56 | |
| 57 | // Finally, open the modal |
| 58 | FOOPluginBaseAdminSettings.media_uploader.open(); |
| 59 | }); |
| 60 | }; |
| 61 | |
| 62 | FOOPluginBaseAdminSettings.setupTabs = function() { |
| 63 | |
| 64 | $(".foo-nav-tabs a.nav-tab").on('click', function(e) { |
| 65 | e.preventDefault(); |
| 66 | |
| 67 | $this = $(this); |
| 68 | |
| 69 | $this.parents(".nav-tab-wrapper:first").find(".nav-tab-active").removeClass("nav-tab-active"); |
| 70 | $this.addClass("nav-tab-active"); |
| 71 | |
| 72 | $(".nav-container:visible").hide(); |
| 73 | |
| 74 | var hash = $this.attr("href"); |
| 75 | |
| 76 | $(hash+'_tab').show(); |
| 77 | |
| 78 | //fix the referer so if changes are saved, we come back to the same tab |
| 79 | var referer = $("input[name=_wp_http_referer]").val(); |
| 80 | if (referer.indexOf("#") >= 0) { |
| 81 | referer = referer.substr(0, referer.indexOf("#")); |
| 82 | } |
| 83 | referer += hash; |
| 84 | |
| 85 | window.location.hash = hash; |
| 86 | |
| 87 | $("input[name=_wp_http_referer]").val(referer); |
| 88 | }); |
| 89 | |
| 90 | if (window.location.hash) { |
| 91 | $('a.nav-tab[href="' + window.location.hash + '"]').click(); |
| 92 | } |
| 93 | |
| 94 | }; //End of setupTabs |
| 95 | |
| 96 | |
| 97 | FOOPluginBaseAdminSettings.ready = function () { |
| 98 | FOOPluginBaseAdminSettings.setupTabs(); |
| 99 | FOOPluginBaseAdminSettings.setupImageUploader(); |
| 100 | }; |
| 101 | }(window.FOOPluginBaseAdminSettings = window.FOOPluginBaseAdminSettings || {}, jQuery)); |
| 102 | |
| 103 | jQuery(function () { |
| 104 | FOOPluginBaseAdminSettings.ready(); |
| 105 | |
| 106 | }); |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 |