assignment.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. var productcodestructure = {};
  2. function createAssignmentForm(assID, url){
  3. initAssignmentTable();
  4. $('#assignmentstation').find('option').remove()
  5. $.ajax({
  6. type: "GET",
  7. url: "../server/",
  8. dataType: "xml",
  9. success: function(xml) {
  10. $(xml).find('station').each(function(index){
  11. $('#assignmentstation').append('<option value="' + $(this).attr('id') +'">' + $(this).attr('id') +'</option>');
  12. });
  13. }
  14. });
  15. if (assID == null){
  16. $('#storedassignments').css("display", "none");
  17. $('#assignmentLupe').css("display", "block");
  18. $('#addExternalImageAssignment').removeAttr('disabled');
  19. $('#addExternalImageAssignment').val("");
  20. }
  21. else{
  22. $('#storedassignments').css("display", "block");
  23. $('#assignmentLupe').css("display", "none");
  24. $('#addExternalImageAssignment').attr('disabled','disabled');
  25. $('#addExternalImageAssignment').val(url);
  26. //Show Assignments
  27. $.getJSON( "../server/assignments/" + assID, function( data ) {
  28. $.each( data, function(curstation, alldata){
  29. var clonePattern = $("#copyAssignmentTable").clone();
  30. $('td',clonePattern).text("");
  31. $('[data-class=station]',clonePattern).text(curstation);
  32. $('#assignmentdetaillist').append(clonePattern);
  33. $.each( alldata, function(i, pattern){
  34. var clonePattern = $("#copyAssignmentTable").clone();
  35. $('td',clonePattern).text("");
  36. var searchpattern = productcodestructure;
  37. $.each( pattern, function(i, item){
  38. $('[data-class=part' + i + ']',clonePattern).text(item);
  39. if(i != "TEXT"){
  40. searchpattern[i] = item;
  41. }
  42. });
  43. $('[data-class=delete]',clonePattern).html('<a href="javascript:deleteSingleAssignment(' + assID +', ' + i + ');">[⨯]</a>');
  44. arr = jQuery.map( searchpattern, function( n, i ) {
  45. return ( n + "." );
  46. });
  47. $('[data-class=search]',clonePattern).html('<a href="javascript:newSearchAssignment(\'' + curstation + '\', \'' + arr.join( "" ) +'\', \'' + pattern.TEXT + '\' );">[?]</a>');
  48. $('#assignmentdetaillist').append(clonePattern);
  49. });
  50. });
  51. });
  52. }
  53. $('#assignment_details').css("display", "block");
  54. }
  55. //called when new image is selected
  56. function getImgText(imgtext){
  57. var result = /[^;]*$/.exec(imgtext)[0];
  58. if(result != imgtext){
  59. $('#assignmentText').val(result.replace(/\.[^/.]+$/, ""))
  60. }
  61. }
  62. function submitAssignment(newassignment){
  63. var obj = new Object();
  64. var skipasscreationform = false;
  65. if(typeof newassignment !== "undefined"){
  66. skipasscreationform = true;
  67. obj = newassignment;
  68. }
  69. else{
  70. obj.img = $('#addExternalImageAssignment').val();
  71. obj.station = $('#assignmentstation option:selected').text();
  72. obj.parts = {};
  73. abk.forEach(function(item){
  74. if(typeof $("#FormPatternAssignment_Form_" + item).val() !== 'undefined' && typeof $("#FormPatternAssignment_Form_" + item).val() !== 'undefined'){
  75. //if($("#FormPatternAssignment_Form_" + item).find(":selected").text() != ""){
  76. obj.parts[item] = $("#FormPatternAssignment_Form_" + item).find(":selected").text();
  77. //}
  78. }
  79. });
  80. obj.assignmentText = $('#assignmentText').val();
  81. }
  82. $.ajax({
  83. type: "Post",
  84. data: JSON.stringify(obj),
  85. headers: {"content-id": "list"},
  86. contentType: "application/json",
  87. url: "../server/assignments/",
  88. success: function(res) {
  89. if(!skipasscreationform){
  90. createAssignmentForm(res.id, $('#addExternalImageAssignment').val())
  91. }
  92. },
  93. error: function (request, status, error) {
  94. alert(request.responseText + status + error);
  95. }
  96. });
  97. }
  98. function deleteAssignment(assignmentID) { //Delete From DB
  99. if (confirm('Are you really, really, REALLY sure!')) {
  100. $.ajax({
  101. type: "DELETE",
  102. url: "../server/assignments/" + assignmentID,
  103. success: function(res) {
  104. initAssignment();
  105. }
  106. });
  107. }
  108. }
  109. function deleteSingleAssignment(assignmentID, patternID) { //Delete From DB
  110. if (confirm('Are you really, really, REALLY sure!')) {
  111. $.ajax({
  112. type: "DELETE",
  113. url: "../server/assignments/" + assignmentID + "/" + patternID,
  114. success: function(res) {
  115. initAssignment();
  116. createAssignmentForm(assignmentID, $('#addExternalImageAssignment').val())
  117. }
  118. });
  119. }
  120. }
  121. function newSearchAssignment(station, pattern, searchtext){
  122. console.log(station + pattern + searchtext);
  123. $('#searchAssingmentStation').val(station);
  124. $('#searchAssingmentPattern').val(pattern);
  125. $('#searchAssingmentText').val(searchtext);
  126. searchAssignmentForm();
  127. }
  128. function searchAssignmentForm(){
  129. $('#assignmentslist tr').css('background-color', 'white');
  130. $.getJSON( "../server/assignments/search/?pattern=" + $('#searchAssingmentPattern').val() + "&station=" + $('#searchAssingmentStation option:selected').text() + "&text=" + $('#searchAssingmentText').val(), function( data ) {
  131. $.each( data, function(i, item){
  132. $('tr[name="' + item +'"]').css('background-color', '#73d216') // Matches exactly 'tcol1'
  133. });
  134. });
  135. }
  136. function initAssignment(){
  137. $('#assignmentslist').text("");
  138. $.getJSON( "../server/assignments/", function( data ) {
  139. $.each( data, function(i, item){
  140. var clonePattern = document.importNode(document.querySelector('#assignmentitem').content,true);
  141. $('[data-class=tr]',clonePattern).attr('name', item);
  142. $('[data-class=image]',clonePattern).attr('src', item);
  143. $('[data-class=item] a',clonePattern).text(item);
  144. $('[data-class=item] a',clonePattern).attr('href','javascript:createAssignmentForm(' + i + ',"' + item + '");');
  145. //$('[data-class=duplicate] a',clonePattern).attr('href','javascript:duplicatePattern(' + curstation + ','+ $(this).attr('id') + ');');
  146. $('[data-class=delete] a',clonePattern).attr('href','javascript:deleteAssignment(' + i + ');');
  147. $('#assignmentslist').append(clonePattern);
  148. });
  149. });
  150. $('#searchAssingmentStation').find('option').remove()
  151. $.ajax({
  152. type: "GET",
  153. url: "../server/",
  154. dataType: "xml",
  155. success: function(xml) {
  156. $(xml).find('station').each(function(index){
  157. $('#searchAssingmentStation').append('<option value="' + $(this).attr('id') +'">' + $(this).attr('id') +'</option>');
  158. });
  159. }
  160. });
  161. }
  162. function initAssignmentTable(){
  163. $('#assignmentdetaillist').text("");
  164. $('#assignmentdetaillist').append("<tr id='copyAssignmentTable'></tr>");
  165. $('#assignmentdetaillist tr').append("<td data-class='station'>Station</td>");
  166. $.getJSON( "../server/json/ProductCode.json", function( data ) {
  167. $.each( data["pattern"], function(i, item){
  168. $('#assignmentdetaillist tr').append("<td data-class='part"+ item["abk"] +"'>"+ item["abk"] +"</td>");
  169. productcodestructure[item["abk"]] = "";
  170. });
  171. $('#assignmentdetaillist tr').append("<td data-class='partTEXT'>Text</td>");
  172. $('#assignmentdetaillist tr').append("<td data-class='delete'>Delete</td>");
  173. $('#assignmentdetaillist tr').append("<td data-class='search'>Search</td>");
  174. });
  175. }
  176. $(document).ready(function() {
  177. initAssignment();
  178. $(document).on('submit', '#assignmentform', function() { //prevent page reload on form submit
  179. return false;
  180. });
  181. });