Advertisement
Guest User

Editor

a guest
Feb 23rd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.04 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Dependency editor
  3. // @namespace http://www.blog-badwolf.eu/
  4. // @version 0.1
  5. // @description Shows UI for editing dependency between product's attributes
  6. // @author OS
  7. // @match http://www.blog-badwolf.eu/zarzadzanie/produkty/produkty_edytuj.php*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. var addNewDependencyButtonTemplate = '<span class="dodaj dependency-button" style="cursor:pointer">Dodaj zależność</span>';
  15. var dependencyMenuTemplate =
  16. '<div class="dependency-menu">\
  17. <div style="display: inline;">\
  18. <div style="display: inline;">Jeśli cecha:</div>\
  19. <div style="display: inline;">\
  20. <select class="attribute-select">\
  21. </select>\
  22. </div>\
  23. </div>\
  24. <div style="display: inline;">\
  25. <div style="display: inline;">ma wartość:</div>\
  26. <div style="display: inline;">\
  27. <select class="attribute-option-select">\
  28. </select>\
  29. </div>\
  30. </div>\
  31. <div style="display: inline;">\
  32. <div style="display: inline;">to:</div>\
  33. <div style="display: inline;">\
  34. <select class="action-select">\
  35. <option value="show">Pokaż</option>\
  36. <option value="hide">Schowaj</option>\
  37. <option value="lock">Zablokuj</option>\
  38. <option value="unlock">Odblokuj</option>\
  39. </select>\
  40. </div>\
  41. </div>\
  42. <div style="display: inline;">\
  43. <div style="display: inline;">i ustaw na:</div>\
  44. <div style="display: inline;">\
  45. <select class="default-value-select">\
  46. </select>\
  47. </div>\
  48. </div>\
  49. <div style="display: inline;">\
  50. <img class="remove-dependency" src="obrazki/kasuj.png" style="cursor:pointer">\
  51. </div>\
  52. </div>';
  53.  
  54. var groupBy = function(xs, key) {
  55. return xs.reduce(function(rv, x) {
  56. (rv[key(x)] = rv[key(x)] || []).push(x);
  57. return rv;
  58. }, {});
  59. };
  60. var getAllAttributeHeaders = () => $('.NazwaCechy');
  61. var getAllNewDependencyButtons = () => $('.dependency-button');
  62. var getIdOfAttributeByName = (name) => $('#id_cecha option').filter((x,y) => y.text == name).val();
  63.  
  64. var setupNewEmptyDependencyMenu = function (attributeHeader, attributeHeaderName) {
  65. attributeHeader.append(dependencyMenuTemplate);
  66. var newDependencyMenu = attributeHeader.find('.dependency-menu:last');
  67. newDependencyMenu.attr('attribute-id', getIdOfAttributeByName(attributeHeaderName));
  68.  
  69. newDependencyMenu.find('.remove-dependency').click(function () {
  70. newDependencyMenu.remove();
  71. saveDependencyConfig();
  72. });
  73.  
  74. return newDependencyMenu;
  75. }
  76.  
  77. var setupNewAttributeSelect = function (newDependencyMenu, attributeHeaderName) {
  78. var otherAttributeNames = getAllAttributeHeaders()
  79. .map((x,y) => y.firstChild.wholeText)
  80. .filter((x,y) => y != attributeHeaderName);
  81.  
  82. var attributeSelect = newDependencyMenu.find('.attribute-select');
  83. attributeSelect.change(function() {
  84. onAttributeSelectChanged($(this), newDependencyMenu);
  85. });
  86. for (let index = 0; index < otherAttributeNames.length; index++) {
  87. const otherAttributeName = otherAttributeNames[index];
  88. attributeSelect.append(
  89. '<option value="'+getIdOfAttributeByName(otherAttributeName)+'">'
  90. + otherAttributeName +
  91. '</option>');
  92. }
  93. attributeSelect.change();
  94. }
  95.  
  96. var getAllAttributeValues = function (selectedAttributeId) {
  97. var selectedProductId = $('#id_unikalne').val();
  98. var attributeOptionsPrefix = 'td_prefix_'+selectedProductId+'_'+selectedAttributeId+'_';
  99. var allAttributeValues = $('.TabelaCechy td')
  100. return allAttributeValues
  101. .filter((_,x) => x.id.indexOf(attributeOptionsPrefix) != -1)
  102. .map((_,x) => { return { name: x.previousSibling.innerText, id: x.id.split('_')[4] }});
  103. };
  104.  
  105. var onAttributeSelectChanged = function (attributeSelect, newDependencyMenu) {
  106. var selectedAttributeId = attributeSelect.val();
  107. var attributeOptions = getAllAttributeValues(selectedAttributeId);
  108. var attributeOptionSelect = newDependencyMenu.find('.attribute-option-select');
  109. attributeOptionSelect.find('option').remove();
  110. for (let index = 0; index < attributeOptions.length; index++) {
  111. const attributeOption = attributeOptions[index];
  112. attributeOptionSelect.append(
  113. '<option value="'+attributeOption.id+'">'
  114. + attributeOption.name +
  115. '</option>');
  116. }
  117. attributeOptionSelect.append('<option value="*">-pozostałe opcje-</option>');
  118. }
  119.  
  120. var setupNewAttributeOptionSelect = function (newDependencyMenu, attributeHeaderName) {
  121. var selectedProductId = $('#id_unikalne').val();
  122. var selectedAttributeId = getIdOfAttributeByName(attributeHeaderName);
  123. var attributeOptionsPrefix = 'td_prefix_'+selectedProductId+'_'+selectedAttributeId+'_';
  124. var allAttributeValues = $('.TabelaCechy td')
  125. var attributeOptions = allAttributeValues
  126. .filter((_,x) => x.id.indexOf(attributeOptionsPrefix) != -1)
  127. .map((_,x) => { return { name: x.previousSibling.innerText, id: x.id.split('_')[4] }});
  128. var defaultOptionSelect = newDependencyMenu.find('.default-value-select');
  129. for (let index = 0; index < attributeOptions.length; index++) {
  130. const attributeOption = attributeOptions[index];
  131. defaultOptionSelect.append(
  132. '<option value="'+attributeOption.id+'">'
  133. + attributeOption.name +
  134. '</option>');
  135. }
  136. defaultOptionSelect.append('<option value="">-nie zmieniaj-</option>');
  137. }
  138.  
  139. var setupNewDependencyMenu = function (attributeHeader, attributeHeaderName) {
  140. var newDependencyMenu = setupNewEmptyDependencyMenu(attributeHeader, attributeHeaderName);
  141. setupNewAttributeSelect(newDependencyMenu, attributeHeaderName);
  142. setupNewAttributeOptionSelect(newDependencyMenu, attributeHeaderName);
  143. newDependencyMenu.find('select').change(saveDependencyConfig);
  144. return newDependencyMenu;
  145. }
  146.  
  147. var onNewDependencyButtonClicked = function () {
  148. var attributeHeader = $(this.parentElement);
  149. var attributeHeaderName = this.parentElement.firstChild.wholeText;
  150. setupNewDependencyMenu(attributeHeader, attributeHeaderName);
  151. saveDependencyConfig();
  152. }
  153.  
  154. var selectHasOption = function (select, value) {
  155. return select.find('option').map((_,x) => $(x).val()).toArray().indexOf(value) != -1;
  156. }
  157.  
  158. var buildDependencyControls = function (dependencyConfig) {
  159. var attributeHeaders = getAllAttributeHeaders()
  160. .map((_,x) => {
  161. return {
  162. element: x,
  163. id: getIdOfAttributeByName(x.firstChild.wholeText),
  164. name: x.firstChild.wholeText
  165. }
  166. });
  167.  
  168. for (let index = 0; index < dependencyConfig.length; index++) {
  169. let dependency = dependencyConfig[index];
  170. if (attributeHeaders.map((_,x) => x.id).toArray().indexOf(dependency.attributeId) != -1) {
  171. var attributeHeader = attributeHeaders.filter((_, x) => x.id == dependency.attributeId)[0];
  172. let dependencyMenu = setupNewDependencyMenu($(attributeHeader.element), attributeHeader.name);
  173.  
  174.  
  175. var attributeSelect = dependencyMenu.find('.attribute-select');
  176. if (selectHasOption(attributeSelect, dependency.dependentAttributeId)) {
  177. attributeSelect.val(dependency.dependentAttributeId);
  178. attributeSelect.change();
  179. }
  180.  
  181. var attributeOptionSelect = dependencyMenu.find('.attribute-option-select');
  182. if (selectHasOption(attributeOptionSelect, dependency.dependentAttributeValue)) {
  183. attributeOptionSelect.val(dependency.dependentAttributeValue);
  184. attributeOptionSelect.change();
  185. }
  186.  
  187. var defaultValueSelect = dependencyMenu.find('.default-value-select');
  188. if (selectHasOption(defaultValueSelect, dependency.attributeValue)) {
  189. defaultValueSelect.val(dependency.attributeValue);
  190. defaultValueSelect.change();
  191. }
  192.  
  193. var actionSelect = dependencyMenu.find('.action-select');
  194. actionSelect.val(dependency.action);
  195. actionSelect.change();
  196. }
  197. }
  198. }
  199.  
  200. var buildDependencyUI = function () {
  201. getAllAttributeHeaders().append(addNewDependencyButtonTemplate);
  202. getAllNewDependencyButtons().click(onNewDependencyButtonClicked);
  203. buildDependencyControls(restoreDependencyConfig());
  204. saveDependencyConfig();
  205. };
  206.  
  207. var getDependencyConfig = function () {
  208. return $('.dependency-menu')
  209. .map(function (_, x) {
  210. return {
  211. attributeId: $(x).attr('attribute-id'),
  212. attributeValue: $(x).find('.default-value-select').val(),
  213. dependentAttributeId: $(x).find('.attribute-select').val(),
  214. dependentAttributeValue: $(x).find('.attribute-option-select').val(),
  215. action: $(x).find('.action-select').val()
  216. }
  217. })
  218. .toArray();
  219. };
  220.  
  221. var expandFillerDependencies = function (dependencies) {
  222. var dependencies = getDependencyConfig();
  223. var allDefinedDependencies = dependencies.filter((x) => x.dependentAttributeValue != '*');
  224. var groupedByAttributePairs = groupBy(dependencies, (x) => x.attributeId + '-' + x.dependentAttributeId);
  225.  
  226. for (let key in groupedByAttributePairs) {
  227. var group = groupedByAttributePairs[key];
  228. var definedValues = group.map((x) => x.dependentAttributeValue);
  229. if (definedValues.indexOf('*') != -1) {
  230. var fillerDependency = group.filter((x) => x.dependentAttributeValue == '*')[0];
  231. var notDefinedDependencies = getAllAttributeValues(fillerDependency.dependentAttributeId)
  232. .map((_, x) => x.id)
  233. .filter((_, x) => definedValues.indexOf(x) == -1)
  234. .map((_, x) => {
  235. return {
  236. attributeId: fillerDependency.attributeId,
  237. attributeValue: fillerDependency.attributeValue,
  238. dependentAttributeId: fillerDependency.dependentAttributeId,
  239. dependentAttributeValue: x,
  240. action: fillerDependency.action
  241. }
  242. })
  243. .toArray();
  244. allDefinedDependencies = allDefinedDependencies.concat(notDefinedDependencies);
  245. }
  246. }
  247.  
  248. return allDefinedDependencies;
  249. }
  250. var saveDependencyConfigForViewer = function () {
  251. var key = 'DependencyConfigViewer-' + $('#id_unikalne').val();
  252. var value = JSON.stringify(expandFillerDependencies(getDependencyConfig()));
  253. window.localStorage.setItem(key, value);
  254. }
  255. var saveDependencyConfigForEditor = function () {
  256. var key = 'DependencyConfigEditor-' + $('#id_unikalne').val();
  257. var value = JSON.stringify(getDependencyConfig());
  258. window.localStorage.setItem(key, value);
  259. }
  260. var saveDependencyConfig = function () {
  261. saveDependencyConfigForViewer();
  262. saveDependencyConfigForEditor();
  263. }
  264. var restoreDependencyConfig = function () {
  265. var key = 'DependencyConfigEditor-' + $('#id_unikalne').val();
  266. var config = JSON.parse(window.localStorage.getItem(key)) || [];
  267. console.log(config);
  268. return config;
  269. }
  270.  
  271. var initialized = false;
  272.  
  273. $('#zakl_link_5').click(function() {
  274. if (!initialized){
  275. initialized = true;
  276. $('#ListaCechProduktu').on('DOMNodeInserted', (e) => {
  277. if ($(e.target).attr('id') == 'ListaCechProduktu') {
  278. buildDependencyUI();
  279. }
  280. });
  281. buildDependencyUI();
  282. }
  283. })
  284. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement