"use strict";function _typeof(obj){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&"function"==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj})(obj)}var ASSET_EXTENSIONS=["gif","jpe?g","png"],CONTEXT_MAX_WIDTHS={l:1280,m:768,s:480,xl:1920,xs:320},SELF_CLOSING_TAGS=["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"],RENAME_ASSETS=!1,SRC_DIR="/images",TAB_CHAR="  ",hasExtension=(Array.prototype.indexOf||(Array.prototype.indexOf=function(searchElement,fromIndex){null==this&&alert('Error: Array.prototype.indexOf()\n"this" is null or undefined.');var k,o=Object(this),len=o.length>>>0;if(0!=len){fromIndex=0|fromIndex;if(!(len<=fromIndex))for(k=Math.max(0<=fromIndex?fromIndex:len-Math.abs(fromIndex),0);k<len;k++)if(k in o&&o[k]===searchElement)return k}return-1}),Object.keys||(Object.keys=function(){var hasOwnProperty=Object.prototype.hasOwnProperty,hasDontEnumBug=!{toString:null}.propertyIsEnumerable("toString"),dontEnums=["toString","toLocaleString","valueOf","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","constructor"],dontEnumsLength=dontEnums.length;return function(obj){"function"==typeof obj||"object"===_typeof(obj)&&null!==obj||alert("Error: Object.keys()\nCalled on a non-object.");var prop,result=[];for(prop in obj)hasOwnProperty.call(obj,prop)&&result.push(prop);if(hasDontEnumBug)for(var i=0;i<dontEnumsLength;i++)hasOwnProperty.call(obj,dontEnums[i])&&result.push(dontEnums[i]);return result}}()),String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")}),function(str){var types=1<arguments.length&&void 0!==arguments[1]?arguments[1]:ASSET_EXTENSIONS;if(!types.length)return!1;for(var regExp="\\.(",i=0,len=types.length;i<len;i++)regExp=regExp+types[i].trim()+(i<len-1?"|":")");return new RegExp(regExp,"i").test(str)}),renameAssets=function(_){scanLayers(function(layer){for(var args=layer.name.split(","),i=0,len=args.length;i<len;i++){var asset=toAsset(args[i]),_name="";asset.size&&(_name+="".concat(asset.size," ")),_name+=_.docName,asset.context&&(_name+="-".concat(asset.context)),asset.def&&(_name+="-".concat(asset.def)),_name+=asset.ext,asset.qual&&(_name+=asset.qual),args[i]=_name}app.activeDocument.artLayers.add().remove(),layer.name=args.join(", ")},_.layers);for(var i=0,len=_.contexts.length;i<len;i++)for(var assets=_.assets[_.contexts[i]],_i=0,_len=assets.length;_i<_len;_i++)assets[_i].name=_.docName},scanLayers=function scanLayers(fn,layers){for(var i=0,len=layers.length;i<len;i++)hasExtension(layers[i].name)&&fn(layers[i]),"LayerSet"===layers[i].typename&&scanLayers(fn,layers[i].layers)},showDialog=function(fn,_){var dialog=new Window("dialog","Generate Responsive Image"),generalPanel=(dialog.margins=16,dialog.add("panel",void 0,"General Information")),fileNameGroup=(generalPanel.alignment="fill",generalPanel.margins=16,generalPanel.spacing=12,generalPanel.add("group")),fileNameInput=(fileNameGroup.add("statictext",void 0,"File Name"),fileNameGroup.alignment="right",fileNameGroup.spacing=0,fileNameGroup.add("edittext",void 0,_.docName)),fileNameGroup=(fileNameInput.characters=16,fileNameInput.helpTip="Add the image's file name",generalPanel.add("group")),srcDirInput=(fileNameGroup.add("statictext",void 0,"Src Directory"),fileNameGroup.alignment="right",fileNameGroup.spacing=0,fileNameGroup.add("edittext",void 0,SRC_DIR)),fileNameGroup=(srcDirInput.characters=16,srcDirInput.helpTip="Add the image's src directory",generalPanel.add("group")),altTextInput=(fileNameGroup.add("statictext",void 0,"Alt Text"),fileNameGroup.alignment="right",fileNameGroup.spacing=0,fileNameGroup.add("edittext")),contextInputs=(altTextInput.active=!0,altTextInput.characters=16,altTextInput.helpTip="Add the image's alt text",{});if(1<_.contexts.length){var contextualPanel=dialog.add("panel",void 0,"Contextual Information");contextualPanel.alignment="fill",contextualPanel.margins=16,contextualPanel.spacing=12;for(var i=0,len=_.contexts.length;i<len-1;i++){var contextGroup=contextualPanel.add("group"),contextGroup=(contextGroup.add("statictext",void 0,"".concat(_.contexts[i].toUpperCase(),":")),contextGroup.alignment="right",contextGroup.spacing=0,contextGroup.add("edittext",void 0,"".concat(CONTEXT_MAX_WIDTHS[_.contexts[i]],"px")));contextGroup.characters=16,contextGroup.helpTip="Add the context's max-width",contextInputs[_.contexts[i]]=contextGroup}}var renameAssetsCheckbox=dialog.add("checkbox",void 0,"Rename Image Assets"),generalPanel=(renameAssetsCheckbox.alignment="fill",renameAssetsCheckbox.helpTip="Rename image assets as Photoshop document's name",renameAssetsCheckbox.value=RENAME_ASSETS,dialog.add("group"));generalPanel.alignment="right",generalPanel.spacing=0,generalPanel.add("button",void 0,"Cancel").onClick=function(){dialog.close()},generalPanel.add("button",void 0,"Save").onClick=function(){if(dialog.close(),altTextInput.text&&(_.dialog.altText=altTextInput.text.trim()),fileNameInput.text&&(_.dialog.fileName=fileNameInput.text.trim()),srcDirInput.text&&(_.dialog.srcDir=srcDirInput.text.trim()),1<_.contexts.length){_.dialog.maxWidths={};for(var i=0,len=_.contexts.length;i<len-1;i++)contextInputs[_.contexts[i]].text?_.dialog.maxWidths[_.contexts[i]]=toNumber(contextInputs[_.contexts[i]].text):_.dialog.maxWidths[_.contexts[i]]=CONTEXT_MAX_WIDTHS[_.contexts[i]]}_.dialog.renameAssets=renameAssetsCheckbox.value,fn(_)},dialog.show()},sortAssets=function(arr){arr.sort(function(a,b){return toNumber(a.def)<toNumber(b.def)?-1:1});for(var indexes=[],uniqueArr=[],i=0,len=arr.length;i<len;i++){var def=toNumber(arr[i].def);-1===indexes.indexOf(def)?(indexes.push(def),uniqueArr.push(arr[i])):uniqueArr[indexes.indexOf(def)]=arr[i]}return uniqueArr},sortContexts=function(arr){var sortOrder={xs:0,s:1,m:2,l:3,xl:4};return arr.sort(function(a,b){return sortOrder[a]-sortOrder[b]})},toAsset=function(str){var regExps={context:/(m(ed(ium)?)?|(e?x(tra)?-*)?(s(m(al)?l)?|l((ar)?ge)?))$/i,def:/@?[1-9](\.\d+)?x?$/i,ext:/\.(gif|jpe?g|png)$/i,qual:/(([1-9]\d?|100)%|10|[1-9])$/,size:/^\d{1,5}((\.\d{1,6})?%|([cm]m|in|px)?\s*?x\s*?\d{1,5}([cm]m|in|px)?)/i},asset={},str=str.trim();return regExps.size.test(str)&&(asset.size=str.match(regExps.size)[0],str=str.replace(regExps.size,"").trim()),regExps.qual.test(str)&&(asset.qual=str.match(regExps.qual)[0],str=str.replace(regExps.qual,"").trim()),asset.ext=str.match(regExps.ext)[0],str=str.replace(regExps.ext,"").trim(),regExps.def.test(str)&&(asset.def=str.match(regExps.def)[0],str=str.replace(regExps.def,"").replace(/[\-_]+$/,"").trim()),regExps.context.test(str)&&(asset.context=str.match(regExps.context)[0],str=str.replace(regExps.context,"").replace(/[\-_]+$/,"").trim()),asset.name=str.trim(),asset},toContext=function(){var str=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";switch(!0){case/^l((ar)?ge)?$/i.test(str):return"l";case/^m(ed(ium)?)?$/i.test(str):return"m";case/^s(m(al)?l)?$/i.test(str):return"s";case/^e?x(tra)?-*l((ar)?ge)?$/i.test(str):return"xl";default:return"xs"}},toIndent=function(tabs){for(var indent="",i=0;i<tabs;i++)indent+=TAB_CHAR;return indent},toNumber=function(){var str=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";return/\d/.test(str)?parseFloat(str.match(/\d+(\.\d+)?/)[0]):1},toWebSafeSrc=function(asset,path){var isSrcSetRef=2<arguments.length&&void 0!==arguments[2]&&arguments[2],src=(src=(src="")+path.replace(/\s/g,"%20")+"/")+asset.name.replace(/\s/g,"%20");return asset.context&&(src+="-".concat(asset.context)),asset.def&&(src+="-".concat(asset.def)),src+=asset.ext,isSrcSetRef&&(src+=" ".concat(toNumber(asset.def),"x")),src},writeAssets=function(fn){var _={assets:{},contexts:[],dialog:{},docName:app.activeDocument.name.replace(/\.[a-z]{3,4}$/i,""),layers:app.activeDocument.layers,path:app.activeDocument.path};scanLayers(function(layer){for(var args=layer.name.split(","),i=0,len=args.length;i<len;i++){var asset=toAsset(args[i]),context=toContext(asset.context);_.assets[context]?_.assets[context].push(asset):_.assets[context]=[asset]}},_.layers),_.contexts=sortContexts(Object.keys(_.assets));for(var i=0,len=_.contexts.length;i<len;i++)_.assets[_.contexts[i]]=sortAssets(_.assets[_.contexts[i]]);fn(_)},writeAttr=function(file,key,value){var tabs=3<arguments.length&&void 0!==arguments[3]?arguments[3]:0;if(file.write("".concat(toIndent(tabs)).concat(key.trim(),'="')),"string"==typeof value)file.write("".concat(value.trim(),'"\r\n'));else{file.write("\r\n");for(var i=0,len=value.length;i<len;i++)file.write("".concat(toIndent(tabs+1)).concat(value[i].trim())),i<len-1&&file.write(","),file.write("\r\n");file.write("".concat(toIndent(tabs),'"\r\n'))}},writeTag=function(fn,file,tag){var tabs=3<arguments.length&&void 0!==arguments[3]?arguments[3]:0,tag=tag.trim();file.write("".concat(toIndent(tabs),"<").concat(tag)),-1===SELF_CLOSING_TAGS.indexOf(tag)&&file.write(">"),file.write("\r\n"),fn(tabs+1),file.write(toIndent(tabs)),-1===SELF_CLOSING_TAGS.indexOf(tag)?file.write("</".concat(tag)):file.write("/"),file.write(">\r\n")},writeHTML=function(_){_.dialog.renameAssets&&renameAssets(_);function writeImgTag(file){writeTag(function(tabs){_.dialog.altText&&writeAttr(file,"alt",_.dialog.altText,tabs);for(var assets=_.assets[_.contexts[0]],src=toWebSafeSrc(assets[0],_.dialog.srcDir),srcset=[],i=1,len=assets.length;i<len;i++)srcset.push(toWebSafeSrc(assets[i],_.dialog.srcDir,!0));writeAttr(file,"src",src,tabs),writeAttr(file,"srcset",srcset,tabs)},file,"img",1<arguments.length&&void 0!==arguments[1]?arguments[1]:0)}var dir=Folder("".concat(_.path,"/").concat(_.docName,"-assets")),file=(dir.exists||dir.create(),File("".concat(_.path,"/").concat(_.docName,"-assets/").concat(_.dialog.fileName,".html")));file.exists&&file.remove(),file.encoding="utf-8",file.open("w");1<_.contexts.length?writeTag(function(tabs){for(var i=_.contexts.length-1;1<=i;i--)!function(){for(var assets=_.assets[_.contexts[i]],minWidth=_.dialog.maxWidths[_.contexts[i-1]]+1,srcset=[],_i2=0,len=assets.length;_i2<len;_i2++)srcset.push(toWebSafeSrc(assets[_i2],_.dialog.srcDir,!0));writeTag(function(tabs){writeAttr(file,"media","(min-width: ".concat(minWidth/16,"em)"),tabs),writeAttr(file,"srcset",srcset,tabs)},file,"source",tabs)}();writeImgTag(file,tabs)},file,"picture"):writeImgTag(file),file.close()};writeAssets(function(_){0<Object.keys(_.assets).length&&showDialog(function(_){writeHTML(_)},_)});