assignment.js 8.1 KB

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