angular.module("widgetkit").controller("customCtrl",["$scope","$timeout","UIkit","mediaInfo","mediaPicker","Fields","Application","Translator",function(e,t,i,n,a,o,d,l){e.content.data._fields||(e.content.data._fields=[]);var s,r=this,c=e.content.data._fields;e.content.data.items&&e.content.data.items.length||(e.content.data.items=[{media:""}]),e.content.data.hasOwnProperty("parse_shortcodes")||(e.content.data.parse_shortcodes=1),s=e.content.data.items,e.item=s[0],e.extrafields=c,r.corefields=d.config.types.custom.fields,r.fields=o.fields(),e.tinyMCE=window.tinyMCE||!1,r.previewItem=function(e){var t=e.options&&e.options.media&&e.options.media.poster;return n(t||e.media).image},r.addItem=function(t){e.item=t||{media:""},s.push(e.item)},r.importItems=function(){a.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()}),r.addItem({title:e.title,media:e.url,width:e.width,height:e.height})})})},r.editItem=function(t){e.item=t},r.deleteItem=function(t){s.splice(s.indexOf(t),1),e.item=s[0]},r.addField=function(e){return e=e||{type:"text",name:"field-x",label:"Field X"},r.corefields[e.name]&&(e.type=r.corefields[e.name].type,e.label=r.corefields[e.name].label),r.hasField(e.name)?void alert('Field name "'+e.name+'" is already in use.'):(s.forEach(function(t){t[e.name]||(t[e.name]="")}),void c.push(angular.copy(e)))},r.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]}),c.splice(c.indexOf(e),1))},r.hasField=function(e){if(["title","media","link"].indexOf(e)>-1)return!0;for(var t=0;t<c.length;t++)if(c[t].name==e)return!0;return!1},r.toggleEditFields=function(){r.editfields=!r.editfields,r.editfields||setTimeout(function(){window.dispatchEvent(new Event("resize"))},150),r.custom={field:{}},r.addCustomField=!1},r.getFieldOptions=function(e,t){var i={},n=r.corefields[e.name];return n&&n.options&&(i=angular.extend(i,n.options)),JSON.stringify(i)},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&&c.splice(n.index(),0,c.splice(c.indexOf(n.scope().field),1)[0])}))}),angular.isArray(e.widget.fields)&&e.widget.fields.forEach(function(e){e&&e.name&&!r.hasField(e.name)&&r.addField(e)}),i.init()}]).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],a=n.options&&n.options.media&&n.options.media.poster;e.preview=t(a||n.media).image.replace(/preview(-.+\.svg)$/g,"content$1")}})}]);