imageSlicer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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' && $("#FormPattern2_Form_" + item).val() !== ''){
  183. if(pattern[item]){
  184. pattern[item] = $("#FormPattern2_Form_" + item).find(":selected").text();
  185. }
  186. //console.log($("#Form_" + item).val())
  187. }
  188. });
  189. });
  190. return rects;
  191. }
  192. function submitSlices(){
  193. //createJsonCutting();
  194. //console.log(JSON.stringify(createJsonCutting()));
  195. //check if selected options are also selected in drop down menus
  196. var checkedoptions = new Set();
  197. $("#imgRectangles input[type=checkbox]:checked").each(function(index, data){
  198. checkedoptions.add($(data).attr("name"))
  199. });
  200. var allskips = [];
  201. var skippall = false;
  202. checkedoptions.forEach(function(item){
  203. if($("#FormPattern2_Form_" + item).find(":selected").text() == ""){
  204. allskips.push(item);
  205. }
  206. });
  207. if(!confirm("Achtung: [" + allskips + "] nicht ausgefüllt, fortfahren?")){
  208. skippall = true;
  209. }
  210. if(!skippall){
  211. request = $.ajax({
  212. type: "POST",
  213. url: "imageSlicer.php",
  214. data: { url: $("#sliceImg").attr("src"), savelocation: $("#savelocation").val(), alldata: JSON.stringify(createJsonCutting()) },
  215. success: function(data){
  216. if ( data.length <= 4 ) {
  217. //clearFormFull();
  218. alert("Saved Images");
  219. }
  220. else{
  221. overlayOnDupliImages(data);
  222. //clearFormFull();
  223. }
  224. },
  225. error: function(xhr, status, error){
  226. console.error(xhr);
  227. }
  228. });
  229. }
  230. }
  231. function overlayOnDupliImages(data) {
  232. document.getElementById("overlayNotClickable").style.display = "block";
  233. var clone = document.importNode(document.querySelector('#SlicerDupliOverlay').content,true);
  234. $.each( data, function(i, item){
  235. var clone2 = document.importNode(document.querySelector('#SlicerDupliImageRow').content,true);
  236. $('[data-class=oldimg]',clone2).attr("src", item["old"] + '?rand=' + Math.random());
  237. $('[data-class=newimg]',clone2).attr("src", item["new"] + '?rand=' + Math.random());
  238. $('[data-class=changebutton]',clone2).attr("onclick", "javascript:changeimg(\"" + item["old"] + "\", \"" + item["new"] +"\", this )");
  239. $('[data-class=ImgList]',clone).append(clone2);
  240. });
  241. $('#overlaycontentsizeNotClickable').append(clone)
  242. }
  243. function changeimg(oldimg, newimg, buttonelement) {
  244. $.get( "imageOverride.php?overridden=" + oldimg + "&new=" + newimg , function( data ) {
  245. //hide button
  246. $(buttonelement).hide(200);
  247. //console.log(data);
  248. });
  249. }
  250. function showJson(){
  251. copy2Clipboard(JSON.stringify(createJson()))
  252. }
  253. function copy2Clipboard(datalink) {
  254. const el = document.createElement('textarea');
  255. el.value = datalink;
  256. document.body.appendChild(el);
  257. el.select();
  258. document.execCommand('copy');
  259. document.body.removeChild(el);
  260. }
  261. function clearFormFull(){
  262. clearForm();
  263. $('#slicerpatterninput').val("");
  264. }
  265. function clearForm(){
  266. $('#FormPattern2').find("select").prop("selectedIndex",0);
  267. }
  268. function fillFormName(value){
  269. $("#slicerpatterninput").val(value);
  270. fillslicerpattern();
  271. }
  272. function fillpatternForm(e) {
  273. var code = (e.keyCode ? e.keyCode : e.which);
  274. if (code == 13) { //Enter keycode
  275. fillslicerpattern();
  276. }
  277. };
  278. function fillslicerpattern(){
  279. clearForm();
  280. $.getJSON( "TransformProductCode.php?ProductCode=" + $("#slicerpatterninput").val(), function( data ) {
  281. $.each( data, function(i, item){
  282. $("#FormPattern2_Form_" + i).val(item);
  283. });
  284. });
  285. }
  286. $(document).ready(function() {
  287. createPatternForm();
  288. loadStationsForm();
  289. //press enter to fill out patternform
  290. $("#slicerpatterninput").bind("keypress", {}, fillpatternForm);
  291. /*
  292. $("#sliceImg").bind('load', function() {
  293. resizeAllRect();
  294. });*/
  295. /*
  296. $("#sliceImg").on('load', function () {
  297. resizeAllRect();
  298. });
  299. */
  300. /*
  301. $('#sliceImg').on('load',function(){
  302. resizeAllRect();
  303. });
  304. */
  305. $.getJSON( "../server/json/SlicerConfig.json", function( data ) {
  306. if(!data["SlicerRectForm"]){
  307. $("#addRectanglesGroup").hide();
  308. $("#imgRectangles").hide();
  309. $("#Json2ClipboardGroup").hide();
  310. }
  311. });
  312. $(document).on('submit', '#sliceform', function() { //prevent page reload on form submit
  313. return false;
  314. });
  315. $("#showJson").click(function() {
  316. showJson();
  317. });
  318. $('#pattern2').text("");
  319. var clone = document.importNode(document.querySelector('#patternClone').content,true);
  320. $('[data-class=part]',clone).limitkeypress({rexp: keypressRegex()});
  321. $("#pattern2").append(clone);
  322. //handle zoom event in order to fix rectangle position
  323. window.addEventListener("resize", resizeAllRect, false);
  324. });