imageSlicer.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. rectID = 0;
  2. colors = ["#edd400","#73d216","#cc0000","#f57900","#3465a4","#c17d11","#75507b","#fce94f","#8ae234","#ef2929","#fcaf3e","#729fcf","#eeeeec","#e9b96e","#ad7fa8","#888a85","#c4a000","#4e9a06","#a40000","#ce5c00","#204a87","#babdb6","#8f5902","#5c3566","#2e3436"].map(i => i + "70"); //Tango Colors with opacity
  3. abk = [];
  4. function addRectangle(){
  5. addRectangle1("", 0,0,1000,100)
  6. }
  7. function scalefactor(){
  8. return $('#sliceImg').innerWidth() / $('#sliceImg').prop('naturalWidth');
  9. }
  10. function refitRect(id){
  11. $('#Rect' + id).css('transform', 'scale(' + scalefactor() + ')');
  12. }
  13. function resizeAllRect() {
  14. console.log("resizing");
  15. for (var i = 0; i < rectID; i++) {
  16. refitRect(i);
  17. $('#Rect' + i).css("top", $('#Rect' + i).attr("y") * scalefactor() + "px");
  18. $('#Rect' + i).css("left", $('#Rect' + i).attr("x") * scalefactor() + "px");
  19. }
  20. }
  21. function changeRectPosition(id, style, amount){
  22. $('#Rect' + id).css(style, amount + "px");
  23. refitRect(id);
  24. }
  25. function addRectangle1(id,x,y,w,h,pattern,idtext){
  26. //Show input fields
  27. var clone = document.importNode(document.querySelector('#RectanglesSetup').content,true);
  28. var localID = rectID
  29. if(id != "")
  30. localID = id;
  31. $('[data-class=RectID]',clone).text("" + localID);
  32. $('[data-class=mainDiv]',clone).hover(function(){
  33. $('#Rect' + localID).css("background", colors[localID % colors.length]);
  34. $('#FormRect' + localID).css("background", colors[localID % colors.length]);
  35. },
  36. function(){
  37. $('#Rect' + localID).css("background", "none");
  38. $('#FormRect' + localID).css("background", "none");
  39. });
  40. $('[data-class=mainDiv]',clone).attr('id', 'FormRect' + localID);
  41. $('[data-class=x]',clone).val(x);
  42. $('[data-class=x]',clone).on('input', function(e) {
  43. changeRectPosition(localID, "left", this.value * scalefactor() );
  44. });
  45. $('[data-class=y',clone).val(y);
  46. $('[data-class=y]',clone).on('input', function(e) {
  47. changeRectPosition(localID, "top", this.value * scalefactor());
  48. });
  49. $('[data-class=w',clone).val(w);
  50. $('[data-class=w]',clone).on('input', function(e) {
  51. changeRectPosition(localID, "width", this.value );
  52. });
  53. $('[data-class=h',clone).val(h);
  54. $('[data-class=h]',clone).on('input', function(e) {
  55. changeRectPosition(localID, "height", this.value );
  56. });
  57. //Alle Abkürzungen einfügen und prüfen ob im pattern angegeben
  58. abk.forEach(function(item){
  59. var clonepattern = document.importNode(document.querySelector('#RectanglePattern').content,true);
  60. $('[data-class=abk]',clonepattern).text(item);
  61. $('[data-class=input]',clonepattern).prop('name', item);
  62. if(typeof pattern !== 'undefined' && pattern[item]){
  63. $('[data-class=input]',clonepattern).prop('checked', true);
  64. }
  65. $('[data-class=RectanglePattern',clone).append(clonepattern);
  66. });
  67. var clonepatternidtext = document.importNode(document.querySelector('#RectanglePatternText').content,true);
  68. if(typeof idtext !== 'undefined'){
  69. $('[data-class=input]',clonepatternidtext).val(idtext);
  70. }
  71. $('[data-class=RectanglePattern',clone).append(clonepatternidtext);
  72. $('#imgRectangles').append(clone);
  73. //add rectangle
  74. var clone = document.importNode(document.querySelector('#RectanglesImage').content,true);
  75. $('[data-class=RectID]',clone).attr("id","Rect" + localID);
  76. $('[data-class=RectID]',clone).attr("y",y);
  77. $('[data-class=RectID]',clone).text("Rect" + localID);
  78. $('[data-class=RectID]',clone).css("top", y * scalefactor() + "px");
  79. $('[data-class=RectID]',clone).css("left", x * scalefactor() + "px");
  80. $('[data-class=RectID]',clone).css("width", w + "px");
  81. $('[data-class=RectID]',clone).css("height", h + "px");
  82. $('[data-class=RectID]',clone).hover(function(){
  83. $('#FormRect' + localID).css("background", colors[localID % colors.length]);
  84. $('#Rect' + localID).css("background", colors[localID % colors.length]);
  85. $('#detailcolumn2').scrollTop( 0 );
  86. $('#detailcolumn2').scrollTop($('#FormRect' + localID).position().top - 50 );
  87. },
  88. function(){
  89. $("#detailcolumn2").stop();
  90. $('#FormRect' + localID).css("background", "none");
  91. $('#Rect' + localID).css("background", "none");
  92. });
  93. $('#imgrect').append(clone);
  94. refitRect(localID);
  95. ++rectID;
  96. new Sortable(document.getElementById('imgRectangles'),{
  97. handle: '.handle3',
  98. animation: 150
  99. });
  100. }
  101. function createPatternForm(){
  102. /* Load pattern config */
  103. //Create form for WAS/Stations/Pattern and WAS/Slicer
  104. var populate = ["FormPattern","FormPattern2"]
  105. var first = true;
  106. populate.forEach(function(populate2){
  107. $.getJSON( "../server/json/ProductCode.json", function( data ) {
  108. $.each( data["pattern"], function(i, item){
  109. var clone = document.importNode(document.querySelector('#PatternFormInit').content,true);
  110. $('[data-class=Name]',clone).text(item["name"]);
  111. $('[data-class=Abkürzung]',clone).text(item["abk"]);
  112. if(first)
  113. abk.push(item["abk"])
  114. $.each( item["types"], function(i, item){
  115. $('[data-class=Optionen]',clone).append('<option value="' + item +'">' + item +'</option>');
  116. });
  117. $('[data-class=Optionen]',clone).attr("id", populate2 + "_Form_" + item["abk"]);
  118. $('#' + populate2).append(clone);
  119. });
  120. //Add "Text" to abk to show it within rects
  121. first = false;
  122. });
  123. });
  124. abk.length = 0;
  125. console.log(abk);
  126. //get Save locations
  127. $.getJSON( "../server/json/SlicerConfig.json", function( data ) {
  128. $.each( data["saveLocation"], function(i, item){
  129. $("#savelocation").append('<option value="' + item +'">' + item +'</option>');
  130. });
  131. });
  132. }
  133. function createRectForm(station){
  134. var imgsrc = $("#sliceImg").attr("src")
  135. rectID = 0;
  136. $('#imgRectangles').text("");
  137. $('#imgrect').html('<img class="image" id="sliceImg" src=' + imgsrc +' alt="Alt text" />');
  138. $.getJSON( "../server/json/Rectangle.json", function( data ) {
  139. $.each( data["Configs"], function(i, item){
  140. if(item["name"] == station){
  141. $.each( item["rects"], function(i, item){
  142. addRectangle1(item["id"],item["X"],item["Y"],item["W"],item["H"],item["pattern"],item["idtext"]);
  143. });
  144. }
  145. });
  146. });
  147. }
  148. function loadStationsForm(){
  149. $.getJSON( "../server/json/Rectangle.json", function( data ) {
  150. $.each( data["Configs"], function(i, item){
  151. $("#loadconfig").append('<option value="' + item["name"] +'">' + item["name"] +'</option>');
  152. });
  153. $("#loadconfig").on('change', function() {
  154. createRectForm(this.value);
  155. });
  156. });
  157. }
  158. function createJson(){
  159. rects = []
  160. $(".singleRect").each(function() {
  161. var pats = {}
  162. $(this).find(".patternPart3").each(function() {
  163. $(this).find('.patternPart3Input:checked').each(function() {
  164. pats[$(this).attr('name')] = true;
  165. });
  166. });
  167. var txt;
  168. $(this).find(".patternPart3Text").each(function() {
  169. txt = $(this).children("input").val();
  170. });
  171. rect = {id: $( this ).find(".RectID").text(), X:$( this ).find('input[data-class="x"]').val(), Y:$( this ).find('input[data-class="y"]').val(), W:$( this ).find('input[data-class="w"]').val(), H:$( this ).find('input[data-class="h"]').val(), pattern: pats, idtext: txt};
  172. rects.push(rect);
  173. });
  174. allrects = {name: $("#loadconfig").val(), rects: rects};
  175. return allrects;
  176. }
  177. function createJsonCutting(){
  178. var rects = createJson();
  179. rects.rects.forEach(function(value) {
  180. pattern = value.pattern;
  181. abk.forEach(function(item){
  182. if(typeof $("#FormPattern2_Form_" + item).val() !== 'undefined' && typeof $("#FormPattern2_Form_" + item).val() !== 'undefined'){
  183. if(pattern[item]){
  184. //if rectangle is checked but nothing has been selected
  185. if($("#FormPattern2_Form_" + item).find(":selected").text() == ""){
  186. pattern[item] = "NoSelection";
  187. }
  188. else{
  189. pattern[item] = $("#FormPattern2_Form_" + item).find(":selected").text();
  190. }
  191. }
  192. }
  193. });
  194. });
  195. return rects;
  196. }
  197. function submitSlices(skip = false){
  198. //createJsonCutting();
  199. //check if selected options are also selected in drop down menus
  200. var skippall = false;
  201. if(!skip){
  202. var checkedoptions = new Set();
  203. $("#imgRectangles input[type=checkbox]:checked").each(function(index, data){
  204. checkedoptions.add($(data).attr("name"))
  205. });
  206. var allskips = [];
  207. checkedoptions.forEach(function(item){
  208. if($("#FormPattern2_Form_" + item).find(":selected").text() == ""){
  209. allskips.push(item);
  210. }
  211. });
  212. if (Array.isArray(allskips) && allskips.length) {
  213. if(!confirm("Achtung: [" + allskips + "] nicht ausgefüllt, fortfahren?")){
  214. skippall = true;
  215. }
  216. }
  217. }
  218. if(!skippall){
  219. request = $.ajax({
  220. type: "POST",
  221. url: "imageSlicer.php",
  222. async: false,
  223. data: { url: $("#sliceImg").attr("src"), savelocation: $("#savelocation").val(), alldata: JSON.stringify(createJsonCutting()) },
  224. success: function(data){
  225. if ( JSON.stringify(data) === '[]') {
  226. //clearFormFull();
  227. if(!skip){
  228. alert("Saved Images");
  229. }
  230. }
  231. else if(data.hasOwnProperty('error')){
  232. alert("Achtung Fehler: \n" + data.error)
  233. console.log(data);
  234. }
  235. else{
  236. if(!skip){
  237. overlayOnDupliImages(data);
  238. }
  239. //clearFormFull();
  240. }
  241. },
  242. error: function(xhr, status, error){
  243. console.error(xhr);
  244. }
  245. });
  246. }
  247. }
  248. function overlayOnDupliImages(data) {
  249. document.getElementById("overlayNotClickable").style.display = "block";
  250. var clone = document.importNode(document.querySelector('#SlicerDupliOverlay').content,true);
  251. $.each( data, function(i, item){
  252. var clone2 = document.importNode(document.querySelector('#SlicerDupliImageRow').content,true);
  253. $('[data-class=oldimg]',clone2).attr("src", item["old"] + '?rand=' + Math.random());
  254. $('[data-class=newimg]',clone2).attr("src", item["new"] + '?rand=' + Math.random());
  255. $('[data-class=changebutton]',clone2).attr("onclick", "javascript:changeimg(\"" + item["old"] + "\", \"" + item["new"] +"\", this )");
  256. $('[data-class=ImgList]',clone).append(clone2);
  257. });
  258. $('#overlaycontentsizeNotClickable').append(clone)
  259. }
  260. function changeimg(oldimg, newimg, buttonelement) {
  261. $.get( "imageOverride.php?overridden=" + oldimg + "&new=" + newimg , function( data ) {
  262. //hide button
  263. $(buttonelement).hide(200);
  264. //console.log(data);
  265. });
  266. }
  267. function showJson(){
  268. copy2Clipboard(JSON.stringify(createJson()))
  269. }
  270. function copy2Clipboard(datalink) {
  271. const el = document.createElement('textarea');
  272. el.value = datalink;
  273. document.body.appendChild(el);
  274. el.select();
  275. document.execCommand('copy');
  276. document.body.removeChild(el);
  277. }
  278. function clearFormFull(){
  279. clearForm();
  280. $('#slicerpatterninput').val("");
  281. }
  282. function clearForm(){
  283. $('#FormPattern2').find("select").prop("selectedIndex",0);
  284. }
  285. function fillFormName(value){
  286. $("#slicerpatterninput").val(value);
  287. fillslicerpattern();
  288. }
  289. function fillpatternForm(e) {
  290. var code = (e.keyCode ? e.keyCode : e.which);
  291. if (code == 13) { //Enter keycode
  292. fillslicerpattern();
  293. }
  294. };
  295. function fillslicerpattern(){
  296. clearForm();
  297. /*
  298. $.getJSON( "TransformProductCode.php?ProductCode=" + $("#slicerpatterninput").val(), function( data ) {
  299. $.each( data, function(i, item){
  300. $("#FormPattern2_Form_" + i).val(item);
  301. });
  302. });
  303. */
  304. jQuery.ajax({
  305. url: "TransformProductCode.php?ProductCode=" + $("#slicerpatterninput").val(),
  306. success: function (data) {
  307. $.each( data, function(i, item){
  308. $("#FormPattern2_Form_" + i).val(item);
  309. });
  310. },
  311. async: false
  312. });
  313. }
  314. function getAllFolders(folder = ""){ //Get DB 2 HTML //has to be synchronus in order to update before loading the rest
  315. $('#selectfolders').find('option').remove()
  316. $('#selectfolders').append("<option></option>")
  317. $.ajax({
  318. type: "GET",
  319. url: "../../universal-storage/server/allfolders/" + folder,
  320. async: false,
  321. dataType: "json",
  322. success: function(data) {
  323. $.each( data, function( key, value ) {
  324. $('#selectfolders').append("<option>" + value.slice(0,-1) +"</option>")
  325. });
  326. }
  327. });
  328. }
  329. function getAllFiles(folder = ""){ //Get DB 2 HTML //has to be synchronus in order to update before loading the rest
  330. $.ajax({
  331. type: "GET",
  332. url: "../../universal-storage/server/images/" + folder,
  333. dataType: "json",
  334. success: function(data) {
  335. $.each( data, function( key, value ) {
  336. console.log(value);
  337. console.log("../../universal-storage/storage/" + folder + "/" + value);
  338. $("#sliceImg").attr("src", "../../universal-storage/storage/" + folder + "/" + value)
  339. fillFormName(value);
  340. submitSlices(true);
  341. /*
  342. $("#slicerpatterninput").val(value);
  343. fillslicerpattern();
  344. */
  345. });
  346. }
  347. });
  348. }
  349. $(document).ready(function() {
  350. createPatternForm();
  351. loadStationsForm();
  352. getAllFolders();
  353. //press enter to fill out patternform
  354. $("#slicerpatterninput").bind("keypress", {}, fillpatternForm);
  355. /*
  356. $("#sliceImg").bind('load', function() {
  357. resizeAllRect();
  358. });*/
  359. /*
  360. $("#sliceImg").on('load', function () {
  361. resizeAllRect();
  362. });
  363. */
  364. /*
  365. $('#sliceImg').on('load',function(){
  366. resizeAllRect();
  367. });
  368. */
  369. $.getJSON( "../server/json/SlicerConfig.json", function( data ) {
  370. if(!data["SlicerRectForm"]){
  371. $("#addRectanglesGroup").hide();
  372. $("#imgRectangles").hide();
  373. $("#Json2ClipboardGroup").hide();
  374. }
  375. });
  376. $(document).on('submit', '#sliceform', function() { //prevent page reload on form submit
  377. return false;
  378. });
  379. $("#showJson").click(function() {
  380. showJson();
  381. });
  382. $("#multiCut").click(function() {
  383. getAllFiles($('#selectfolders').val());
  384. });
  385. $('#pattern2').text("");
  386. var clone = document.importNode(document.querySelector('#patternClone').content,true);
  387. $('[data-class=part]',clone).limitkeypress({rexp: keypressRegex()});
  388. $("#pattern2").append(clone);
  389. //handle zoom event in order to fix rectangle position
  390. window.addEventListener("resize", resizeAllRect, false);
  391. });