"use strict";angular.module("widgetkit").controller("customCtrl",["$scope","$timeout","UIkit","mediaInfo","mediaPicker","Fields","Application","Translator",function(i,e,t,n,a,o,d,l){i.content.data._fields||(i.content.data._fields=[]);var s,c=this,r=i.content.data._fields;i.content.data.items&&i.content.data.items.length||(i.content.data.items=[{media:""}]),i.content.data.hasOwnProperty("parse_shortcodes")||(i.content.data.parse_shortcodes=1),s=i.content.data.items,i.item=s[0],i.extrafields=r,c.corefields=d.config.types.custom.fields,c.fields=o.fields(),i.tinyMCE=window.tinyMCE||!1,c.previewItem=function(e){var t=e.options&&e.options.media&&e.options.media.poster;return n(t||e.media).image},c.addItem=function(e){i.item=e||{media:""},s.push(i.item)},c.importItems=function(){a.select({multiple:!0}).then(function(e){!e.length||1!=s.length||i.item.title&&i.item.media&&i.item.content||(s.length=0),angular.forEach(e,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(e){i.item=e},c.deleteItem=function(e){s.splice(s.indexOf(e),1),i.item=s[0]},c.addField=function(t){t=t||{type:"text",name:"field-x",label:"Field X"},c.corefields[t.name]&&(t.type=c.corefields[t.name].type,t.label=c.corefields[t.name].label),c.hasField(t.name)?alert('Field name "'+t.name+'" is already in use.'):(s.forEach(function(e){e[t.name]||(e[t.name]="")}),r.push(angular.copy(t)))},c.deleteField=function(t){confirm(l.trans("Are you sure you want to delete this field?"))&&(s.forEach(function(e){e[t.name]&&delete e[t.name]}),r.splice(r.indexOf(t),1))},c.hasField=function(e){if(-1<["title","media","link"].indexOf(e))return!0;for(var t=0;t<r.length;t++)if(r[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,t){var i={},n=c.corefields[e.name];return n&&n.options&&(i=angular.extend(i,n.options)),JSON.stringify(i)},i.$watch("content",function(e){var t=s.indexOf(i.item);s=e.data.items,i.item=s[t]}),t.util.on(document,"moved",function(e,t,i){var n=angular.element(i);"js-content-items"==t.$el.id&&s.splice(n.index(),0,s.splice(s.indexOf(n.scope().item),1)[0]),"js-fields-items"==t.$el.id&&r.splice(n.index(),0,r.splice(r.indexOf(n.scope().field),1)[0])}),angular.isArray(i.widget.fields)&&i.widget.fields.forEach(function(e){e&&e.name&&!c.hasField(e.name)&&c.addField(e)})}]).run(["$rootScope","mediaInfo",function(e,a){e.$on("wk.preview.content",function(e,t){if("custom"==t.type&&t.data.items.length){var i=t.data.items[0],n=i.options&&i.options.media&&i.options.media.poster;e.preview=a(n||i.media).image.replace(/preview(-.+\.svg)$/g,"content$1")}})}]);