!function(a,n){angular.module("widgetkit").service("mediaPicker",["$location","$q","Application",function(r,t,e){var d=new RegExp("^"+e.baseUrl());return{select:function(e){e=angular.extend({title:"Pick media",multiple:!1,button:{text:"Select"}},e);var i=t.defer();return e.editor?(e=angular.extend({frame:"post"},e),wp.media.editor.open(e.editor,e)):frame=wp.media(e).on("select",function(){var t=frame.state().get("selection").map(function(t){var e,i,a=t.toJSON(),n=(e=a.url,(i=document.createElement("a")).href=e,i);return n.host==r.host()&&(a.url=n.pathname.replace(d,"").replace(/^\//,"")),a});i.resolve(e.multiple?t:t[0])}).open(),i.promise}}}]),a(function(){a("body").on("click",".widgetkit-editor",function(t){t.preventDefault();var e=a(this),i=e.data("source")?a("#"+e.data("source")):a("textarea#content");n.widgetkit.env.editor(i)}).on("click",".widgetkit-widget button",function(t){t.preventDefault();var e=a(this).nextAll("input"),i=a(this).closest("form").find(".widget-control-save");n.widgetkit.env.init("widget",JSON.parse(e.val()),function(t){e.val(JSON.stringify(t)),i.trigger("click")})}).find("[data-app]").addClass("wrap"),a(".widgetkit-editor[data-source]").each(function(){var t=a(this);a("#"+t.data("source")).addClass("uk-margin-small-top").before(t)}),a(document).on("widget-updated",function(t,e){var i;e.is('[id*="_text-"]')&&(i=e.find("textarea")).before('<a href="#" class="button add_media widgetkit-editor" title="Add Widget" data-source="'+i.attr("id")+'"><span></span> Widgetkit</a>').addClass("uk-margin-small-top")})})}(jQuery,window);