Advertisement
the51ck

Untitled

Jun 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const getTextareas = () => {
  2.     const textareas = {};
  3.     let trs = [...document.querySelectorAll("tr.dynamic-gallery[id^=\"gallery\"]")];
  4.     while (trs[0]) {
  5.         const tr = trs.shift();
  6.         const view = tr.querySelector(".field-view select[name^=\"gallery\"]").value;
  7.         if (view) textareas[view] = tr.querySelector(".field-details textarea");
  8.     }
  9.     return textareas;
  10. };
  11.  
  12.  
  13. const patchPatches = patch => {
  14.     const textareas = getTextareas();
  15.     for (let view in textareas) {
  16.         const textarea = textareas[view];
  17.         console.debug(textarea.value);
  18.         textarea.value = patch(textarea.value);
  19.     }
  20. };
  21.  
  22.  
  23. const removeCustomizationId = json => {
  24.     try {
  25.         const obj = JSON.parse(json);
  26.  
  27.         if (obj.patch) {
  28.             for (let placement in obj.patch) {
  29.                 const anyCustomId = Object.keys(obj.patch[placement])[0];
  30.  
  31.                 const customPatch = obj.patch[placement][anyCustomId];
  32.  
  33.                 if (customPatch) {
  34.                     obj.patch[placement] = customPatch;
  35.                 }
  36.             }
  37.         }
  38.         return JSON.stringify(obj, null, '  ');
  39.     } catch (error) {
  40.         console.error(error.message);
  41.     }
  42.     return json;
  43. };
  44.  
  45.  
  46. const getIdPatch = idsMap => {
  47.     return json => {
  48.         try {
  49.             const obj = JSON.parse(json);
  50.  
  51.             if (obj.patch) {
  52.                 for (let placement in obj.patch) {
  53.                     for (let id in obj.patch[placement]) {
  54.                         const customDetails = obj.patch[placement][id];
  55.                         if (id in idsMap) {
  56.                             obj.patch[placement][idsMap[id]] = customDetails;
  57.                             delete obj.patch[placement][id];
  58.                         }
  59.                     }
  60.                 }
  61.             }
  62.             return JSON.stringify(obj, null, '  ');
  63.         } catch (error) {
  64.             console.error(error.message);
  65.         }
  66.         return json;
  67.     };
  68. };
  69.  
  70.  
  71. const prettify = json => JSON.stringify(JSON.parse(json), null, '  ');
  72.  
  73.  
  74. const idsMap = {
  75.     "16": "24",
  76.     "8": "22",
  77.     "17": "23",
  78.     "25": "26"
  79. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement