angular.module("widgetkit").controller("customCtrl",["$scope","$timeout","UIkit","mediaInfo","mediaPicker","Fields","Application","Translator",function(e,t,i,n,o,a,d,l){e.content.data._fields||(e.content.data._fields=[]);var s,c=this,m=e.content.data._fields;e.content.data.items&&e.content.data.items.length||(e.content.data.items=[{media:""}]),s=e.content.data.items,e.item=s[0],e.extrafields=m,c.corefields=d.config.types.custom.fields,c.fields=a.fields(),c.previewItem=function(e){var t=e.options&&e.options.media&&e.options.media.poster;return n(t||e.media).image},c.addItem=function(t){e.item=t||{media:""},s.push(e.item)},c.importItems=function(){o.select({multiple:!0}).then(function(t){!t.length||1!=s.length||e.item.title&&e.item.media&&e.item.content||(s.length=0),angular.forEach(t,function(e){e.title=String(e.title).replace(/(-|_)/g," ").replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g,function(e){return e.toUpperCase()}),c.addItem({title:e.title,media:e.url,width:e.width,height:e.height})})})},c.editItem=function(t){e.item=t},c.deleteItem=function(t){s.splice(s.indexOf(t),1),e.item=s[0]},c.addField=function(e){return e=e||{type:"text",name:"field-x",label:"Field X"},c.corefields[e.name]&&(e.type=c.corefields[e.name].type,e.label=c.corefields[e.name].label),c.hasField(e.name)?void alert('Field name "'+e.name+'" is already in use.'):(s.forEach(function(t){t[e.name]||(t[e.name]="")}),void m.push(angular.copy(e)))},c.deleteField=function(e){confirm(l.trans("Are you sure you want to delete this field?"))&&(s.forEach(function(t){t[e.name]&&delete t[e.name]}),m.splice(m.indexOf(e),1))},c.hasField=function(e){if(["title","media","link"].indexOf(e)>-1)return!0;for(var t=0;t<m.length;t++)if(m[t].name==e)return!0;return!1},c.toggleEditFields=function(){c.editfields=!c.editfields,c.editfields||setTimeout(function(){window.dispatchEvent(new Event("resize"))},150),c.custom={field:{}},c.addCustomField=!1},c.getFieldOptions=function(e){var t={},i=c.corefields[e.name];return i&&i.options&&(t=angular.extend(t,i.options)),JSON.stringify(t)},e.$watch("content",function(t){var i=s.indexOf(e.item);s=t.data.items,e.item=s[i]}),i.$doc.trigger("ready.uk.dom"),i.$doc.on("change.uk.sortable",function(e,i,n){n&&void 0!==n&&(n=angular.element(n),t(function(){"js-content-items"==i.element[0].id&&s.splice(n.index(),0,s.splice(s.indexOf(n.scope().item),1)[0]),"js-fields-items"==i.element[0].id&&m.splice(n.index(),0,m.splice(m.indexOf(n.scope().field),1)[0])}))})}]).run(["$rootScope","mediaInfo",function(e,t){e.$on("wk.preview.content",function(e,i){if("custom"==i.type&&i.data.items.length){var n=i.data.items[0],o=n.options&&n.options.media&&n.options.media.poster;e.preview=t(o||n.media).image.replace(/preview(-.+\.svg)$/g,"content$1")}})}]);