Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * ADOBE CONFIDENTIAL
  3.  *
  4.  * Copyright 2014 Adobe Systems Incorporated
  5.  * All Rights Reserved.
  6.  *
  7.  * NOTICE:  All information contained herein is, and remains
  8.  * the property of Adobe Systems Incorporated and its suppliers,
  9.  * if any.  The intellectual and technical concepts contained
  10.  * herein are proprietary to Adobe Systems Incorporated and its
  11.  * suppliers and may be covered by U.S. and Foreign Patents,
  12.  * patents in process, and are protected by trade secret or copyright law.
  13.  * Dissemination of this information or reproduction of this material
  14.  * is strictly forbidden unless prior written permission is obtained
  15.  * from Adobe Systems Incorporated.
  16.  */
  17. (function($, ns, channel, Class, window) {
  18.     "use strict";
  19.     var rel = ".aem-assets-image-edit-canvas";
  20.  
  21.     var storedMapData = "";
  22.     var previousTransformData = {};
  23.     var Editor = new Class({
  24.         extend: CUI.ImageEditor
  25.     });
  26.  
  27.     ns.ImageEditor = function() {
  28.         var self = this;
  29.         channel.on("inline-edit-start", function(e) {
  30.             var editable = $(e.editable);
  31.             storedMapData = e.mapData;
  32.             previousTransformData = transformImageMapForCUI(storedMapData);
  33.             self.startImageEditor(editable);
  34.         });
  35.     };
  36.  
  37.     ns.ImageEditor.prototype.startImageEditor = function(editable) {
  38.         var options = $.extend({}, ns.ImageEditor.options);
  39.         // Send the previous transform results such as image map etc to the CUI image editor
  40.         options.result = previousTransformData;
  41.         var a =
  42.             new Editor({
  43.                 element: editable,
  44.                 parent: "body",
  45.                 mode: "fullscreen",
  46.                 theme: "dark"
  47.             });
  48.         a.start(options);
  49.  
  50.         // customize destroyUI to not delete $ui
  51.         a.destroyUI =
  52.             function() {
  53.                 var dimensions = this.$ui.copy.data("imageeditor-dimensions");
  54.                 // this.$ui.remove();
  55.                 this.toggleAnimation(false);
  56.                 this.$element.removeClass("imageeditor-editable");
  57.                 this.$element.css(dimensions);
  58.             };
  59.     };
  60.  
  61.  
  62.     /**
  63.      * CUI ImageEditor configuration options
  64.      */
  65.     ns.ImageEditor.options = {
  66.         "ui": {
  67.             "fullscreen": {
  68.                 "toolbar": [
  69.                     [
  70.                         "crop#launchwithratio",
  71.                         "rotate#left",
  72.                         "rotate#right",
  73.                         "flip#vertical",
  74.                         "flip#horizontal",
  75.                         "map#launch"
  76.                     ],
  77.                     [
  78.                         "history#undo",
  79.                         "history#redo",
  80.                         "control#close",
  81.                         "control#finish"
  82.                     ]
  83.                 ],
  84.                 "replacementToolbars": {
  85.                     "crop": [
  86.                         [
  87.                             "crop#identifier"
  88.                         ],
  89.                         [
  90.                             "crop#unlaunch",
  91.                             "crop#confirm"
  92.                         ]
  93.                     ],
  94.                     "map": [
  95.                         [
  96.                             "map#rectangle",
  97.                             "map#circle"
  98.                         ],
  99.                         [
  100.                             "map#unlaunch",
  101.                             "map#confirm"
  102.                         ]
  103.                     ]
  104.                 }
  105.             }
  106.         }
  107.     };
  108.  
  109.     function transformImageMapForCUI(mapData) {
  110.         var result = [];
  111.         var tmp;
  112.         tmp = {
  113.             regexp: /\[(\w+)\(([0-9,]+)\)"([^"]*)"\|"([^"]*)"\|"([^"]*)"\]/g
  114.         };
  115.  
  116.         tmp.transformation = {
  117.             transformation: "map",
  118.             areas: []
  119.         };
  120.  
  121.         var inverseScaleFactorX = 1.0 / getScaleFactorX();
  122.         var inverseScaleFactorY = 1.0 / getScaleFactorY();
  123.  
  124.         while ((tmp.match = tmp.regexp.exec(mapData)) !== null) {
  125.             tmp.area = {};
  126.  
  127.             tmp.area.shape = tmp.match[1];
  128.             tmp.area.href = tmp.match[3];
  129.             tmp.area.target = tmp.match[4];
  130.             tmp.area.alt = tmp.match[5];
  131.             tmp.coords = $.map(tmp.match[2].split(","), function(e) {
  132.                 return parseInt(e, 10);
  133.             });
  134.             switch (tmp.area.shape) {
  135.                 case "rect":
  136.                     tmp.area.selection = {
  137.                         left: tmp.coords[0] * inverseScaleFactorX,
  138.                         top: tmp.coords[1] * inverseScaleFactorY,
  139.                         width: (tmp.coords[2] - tmp.coords[0]) * inverseScaleFactorX,
  140.                         height: (tmp.coords[3] - tmp.coords[1]) * inverseScaleFactorY
  141.                     };
  142.                     break;
  143.                 case "circle":
  144.                     tmp.area.selection = {
  145.                         left: (tmp.coords[0] - tmp.coords[2]) * inverseScaleFactorX,
  146.                         top: (tmp.coords[1] - tmp.coords[2]) * inverseScaleFactorY,
  147.                         width: 2 * tmp.coords[2] * inverseScaleFactorX,
  148.                         height: 2 * tmp.coords[2] * inverseScaleFactorY
  149.                     };
  150.                     break;
  151.                 default:
  152.                     continue;
  153.             }
  154.             tmp.transformation.areas.push(tmp.area);
  155.         }
  156.         result.push(tmp.transformation);
  157.         return result;
  158.     }
  159.  
  160.     function back() {
  161.         location.href = $(".aem-assets-imageeditor-back").attr("href");
  162.     }
  163.  
  164.     channel.on("editing-cancelled", function() {
  165.         back();
  166.     });
  167.  
  168.     channel.on("editing-finished", function(e, properties) {
  169.         var result = {
  170.             imageCrop: null,
  171.             imageRotate: null,
  172.             imageFlipHorizontal: null,
  173.             imageFlipVertical: null,
  174.             imageMap: null
  175.         };
  176.         var tmp;
  177.         var i;
  178.         var transformation;
  179.         var scaleFactorX = getScaleFactorX();
  180.         var scaleFactorY = getScaleFactorY();
  181.  
  182.         for (i = 0; i < properties.result.length; i++) {
  183.             transformation = properties.result[i];
  184.  
  185.             switch (transformation.transformation) {
  186.                 case "crop":
  187.                     result.imageCrop = [
  188.                         Math.round(transformation.left * scaleFactorX),
  189.                         Math.round(transformation.top * scaleFactorX),
  190.                         Math.round((transformation.left + transformation.width) * scaleFactorX),
  191.                         Math.round((transformation.top + transformation.height) * scaleFactorX)
  192.                     ].join(",");
  193.  
  194.                     break;
  195.  
  196.                 case "rotate":
  197.                     tmp = parseInt(transformation.angle, 10);
  198.                     if (!isNaN(tmp)) {
  199.                         tmp = tmp % 360;
  200.                         tmp = tmp + 360;
  201.                         tmp = tmp % 360;
  202.  
  203.                         result.imageRotate = tmp.toString();
  204.                     }
  205.  
  206.                     break;
  207.  
  208.                 case "flip":
  209.                     // CUI image editor swaps the flip actions when the image is rotated.
  210.                     var flipAxes = (result.imageRotate % 180) !== 0;
  211.                     if (!flipAxes) {
  212.                         result.imageFlipHorizontal = (transformation.horizontal) ? "fliphorizontal" : "";
  213.                         result.imageFlipVertical = (transformation.vertical) ? "flipvertical" : "";
  214.                     } else {
  215.                         result.imageFlipVertical = (transformation.horizontal) ? "flipvertical" : "";
  216.                         result.imageFlipHorizontal = (transformation.vertical) ? "fliphorizontal" : "";
  217.                     }
  218.  
  219.                     break;
  220.  
  221.                 case "map":
  222.                     tmp = {
  223.                         count: 0,
  224.                         area: null,
  225.                         areaStrings: []
  226.                     };
  227.                     for (tmp.count = 0; tmp.count < transformation.areas.length; tmp.count++) {
  228.                         tmp.area = transformation.areas[tmp.count];
  229.  
  230.                         switch (tmp.area.shape) {
  231.                             case "circle":
  232.                                 tmp.radius = Math.round(tmp.area.selection.width / 2.0 * scaleFactorX);
  233.                                 tmp.coords = [
  234.                                     Math.round(tmp.area.selection.left * scaleFactorX + tmp.radius),
  235.                                     Math.round(tmp.area.selection.top * scaleFactorY + tmp.radius),
  236.                                     tmp.radius
  237.                                 ];
  238.                                 break;
  239.                             case "rect":
  240.                                 tmp.coords = [
  241.                                     Math.round(tmp.area.selection.left * scaleFactorX),
  242.                                     Math.round(tmp.area.selection.top * scaleFactorY),
  243.                                     Math.round((tmp.area.selection.left + tmp.area.selection.width) * scaleFactorX),
  244.                                     Math.round((tmp.area.selection.top + tmp.area.selection.height) * scaleFactorY)
  245.                                 ];
  246.                                 break;
  247.                             default:
  248.                                 continue;
  249.                         }
  250.  
  251.                         // Escaping double quotes, not escaping square brackets and vertical bars.
  252.                         tmp.areaStrings.push(
  253.                             "[" +
  254.                                 tmp.area.shape +
  255.                                 "(" + tmp.coords.join(",") + ")" +
  256.                                 "\"" + tmp.area.href.replace(/\"/g, "%22") + // eslint-disable-line no-useless-escape
  257.                                 "\"|" + // eslint-disable-line no-useless-escape
  258.                                 "\"" + tmp.area.target + // eslint-disable-line no-useless-escape
  259.                                 "\"|" + // eslint-disable-line no-useless-escape
  260.                                 "\"" + tmp.area.alt.replace(/\"/g, "\&quot;") + // eslint-disable-line no-useless-escape
  261.                                 "\"" +// eslint-disable-line no-useless-escape
  262.                                 "]");
  263.                     }
  264.  
  265.                     result.imageMap = tmp.areaStrings.join("");
  266.             }
  267.         }
  268.  
  269.         if (isDynamicMediaEnabled()) {
  270.             nonDestructiveSave(result);
  271.         } else {
  272.             destructiveSave(result);
  273.         }
  274.     });
  275.  
  276.     function getScaleFactorX() {
  277.         var scaleFactor = 1;
  278.         var img = $(rel + " img");
  279.         var actualWidth = img.data("originalWidth") || 1;
  280.         var renditionWidth = parseInt(((img.css("width")) ? img.css("width") : 1), 10);
  281.  
  282.         if (actualWidth > 1 && renditionWidth > 1) {
  283.             scaleFactor = actualWidth / renditionWidth;
  284.         }
  285.         return scaleFactor;
  286.     }
  287.  
  288.  
  289.     function getScaleFactorY() {
  290.         var scaleFactor = 1;
  291.         var img = $(rel + " img");
  292.         var actualHeight = img.data("originalHeight") || 1;
  293.         var renditionHeight = parseInt(((img.css("height")) ? img.css("height") : 1), 10);
  294.  
  295.         if (actualHeight > 1 && renditionHeight > 1) {
  296.             scaleFactor = actualHeight / renditionHeight;
  297.         }
  298.         return scaleFactor;
  299.     }
  300.  
  301.  
  302.     function destructiveSave(result) {
  303.         var form = $(".form_inline_image_edit");
  304.         $("#crop_dimension").val(result.imageCrop);
  305.         $("#rotation_angle").val(result.imageRotate);
  306.         $("#flip_horizontal").val(result.imageFlipHorizontal);
  307.         $("#flip_vertical").val(result.imageFlipVertical);
  308.         $("#image_map").val(result.imageMap);
  309.  
  310.         $.ajax({
  311.             type: form.prop("method"),
  312.             url: form.data("action"),
  313.             data: form.serialize(),
  314.             path: form.data("redirect"),
  315.             success: function(e) {
  316.                 var editCanvas = $(rel);
  317.                 var lastModified = editCanvas.data("lastmodified");
  318.                 var assetPath = editCanvas.data("path");
  319.                 var renditionName = editCanvas.data("renditionname");
  320.                 updateImage({ data: {
  321.                     "lastModified": lastModified,
  322.                     "renditionName": renditionName,
  323.                     "assetPath": assetPath
  324.                 } });
  325.             },
  326.             error: function(e) {
  327.                 $("#image-edit-failure-modal")[0].show();
  328.                 $("#image-edit-failure-modal").find("coral-dialog-footer button")[0].addEventListener("click",
  329.                     function() {
  330.                         back();
  331.                     });
  332.             }
  333.         });
  334.     }
  335.  
  336.     function updateImage(e) {
  337.         var ORIGINAL_RENDITION_SUFFIX = "/jcr:content/renditions/original";
  338.         var lastModified = e.data.lastModified;
  339.         var assetPath = e.data.assetPath;
  340.         var nuienabled = $(".aem-assets-image-edit-canvas").data("nuienabled");
  341.         var renditionName = "cq5dam.thumbnail.48.48.png";
  342.         if (e.data.renditionName) {
  343.             renditionName = e.data.renditionName;
  344.         }
  345.  
  346.         var url = assetPath + "/jcr:content/renditions/" + renditionName + "/jcr:content.json";
  347.  
  348.         if (nuienabled) {
  349.             assetPath = $(".aem-assets-image-edit-canvas").data("path");
  350.             url = assetPath + ORIGINAL_RENDITION_SUFFIX + "/jcr:content.json";
  351.         }
  352.  
  353.         Dam.Util.detectChange(url, "jcr:lastModified", lastModified, 2000, 5, true, function() {
  354.             // if nui is NOT enable exit
  355.             if (!nuienabled) {
  356.                 back();
  357.                 return;
  358.             }
  359.  
  360.             // NUI upload asset if enabled
  361.             var EVENT_UPLOAD_BLOB = "upload-blob";
  362.             var DAM_FILE_UPLOAD_ENDED = "dam-fileupload:loadend";
  363.             var SEL_DAM_CHUNK_UPLOAD = "dam-chunkfileupload";
  364.  
  365.             // get file name
  366.             var fileName = getAssetNameFromPath(assetPath);
  367.             // get folder path
  368.             var folderPath = assetPath.replace("/" + fileName, "");
  369.             // get original for reprocess
  370.             var req = new Request(Granite.HTTP.externalize(assetPath + ORIGINAL_RENDITION_SUFFIX));
  371.  
  372.             // override get path method
  373.             DamFileUpload.prototype._getContentPath = function() {
  374.                 return folderPath;
  375.             };
  376.  
  377.             // add DOM object for event tracking
  378.             DamFileUpload.prototype.fileUpload = $(SEL_DAM_CHUNK_UPLOAD);
  379.             DamFileUpload.prototype.fileUpload._abortFile = function() {};
  380.  
  381.             // override layoutid
  382.             DamFileUpload.prototype.fileUpload.layoutId = "";
  383.  
  384.             // remove handler for file upload end
  385.             $(document).off(DAM_FILE_UPLOAD_ENDED)
  386.                 .on(DAM_FILE_UPLOAD_ENDED, function(e) {
  387.                     // show info dialog
  388.                     var ui = $(window).adaptTo("foundation-ui");
  389.                     ui.clearWait();
  390.  
  391.                     var successMessage = Granite.I18n.get("Asset reprocessing has been initiated.");
  392.                     ui.prompt(Granite.I18n.get("Success"), successMessage, "success", [{
  393.                         text: Granite.I18n.get("OK"),
  394.                         primary: true
  395.                     }]);
  396.                     // listen for confirm
  397.                     $("coral-dialog-footer > button").on("click", function() {
  398.                         back();
  399.                     });
  400.                 });
  401.  
  402.             fetch(req).then(function(response) {
  403.                 return response.blob();
  404.             }).then(function(blob) {
  405.                 // upload to nui for reprocess
  406.                 var file = { file: { name: fileName }, name: fileName, path: folderPath, _originalFile: blob };
  407.                 var evt = $.Event(EVENT_UPLOAD_BLOB, { type: EVENT_UPLOAD_BLOB, file: file });
  408.                 // trigger event
  409.                 $(document).trigger(evt);
  410.             });
  411.         });
  412.     }
  413.  
  414.     channel.on("dynamic-fetch-finished", function() {
  415.         window.location.reload(true);
  416.     });
  417.  
  418.     function getAssetNameFromPath(path) {
  419.         var pathParts = path.split("/");
  420.         return pathParts[pathParts.length - 1];
  421.     }
  422.  
  423.     function nonDestructiveSave(result) {
  424.         // for now use destructiveSave
  425.         // TODO: replace with non-destructive save
  426.         destructiveSave(result);
  427.     }
  428.  
  429.     // create new inline image editor
  430.     new ns.ImageEditor([
  431.  
  432.     ]);
  433.  
  434.     function isDynamicMediaEnabled() {
  435.         return $(rel).data("dynamicmedia") || false;
  436.     }
  437. }(jQuery, Granite, jQuery(document), Class, this));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement