assignment.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. //console.log("Submiting Assignment")
  86. var obj = new Object();
  87. var skipasscreationform = false;
  88. if(typeof newassignment !== "undefined"){
  89. skipasscreationform = true;
  90. obj = newassignment;
  91. }
  92. else{
  93. obj.img = $('#addExternalImageAssignment').val();
  94. obj.station = $('#assignmentstation option:selected').text();
  95. obj.parts = {};
  96. abk.forEach(function(item){
  97. if(typeof $("#FormPatternAssignment_Form_" + item).val() !== 'undefined' && typeof $("#FormPatternAssignment_Form_" + item).val() !== 'undefined'){
  98. //if($("#FormPatternAssignment_Form_" + item).find(":selected").text() != ""){
  99. obj.parts[item] = $("#FormPatternAssignment_Form_" + item).find(":selected").text();
  100. //}
  101. }
  102. });
  103. obj.assignmentText = $('#assignmentText').val();
  104. }
  105. //console.log(obj)
  106. $.ajax({
  107. type: "Post",
  108. data: JSON.stringify(obj),
  109. headers: {"content-id": "list"},
  110. contentType: "application/json",
  111. url: "../server/assignments/",
  112. async: false,
  113. success: function(res) {
  114. //console.log("Posted")
  115. if(!skipasscreationform){
  116. createAssignmentForm(res.id, $('#addExternalImageAssignment').val())
  117. }
  118. },
  119. error: function (request, status, error) {
  120. alert(request.responseText + status + error);
  121. }
  122. });
  123. }
  124. function removeSelectedItems(){
  125. var removedItems = [];
  126. $("#assignmentslist input[type=checkbox]:checked").each(function(index, data){
  127. removedItems.push($(data).attr("name"));
  128. });
  129. if (confirm('Deleting: ' + removedItems)) {
  130. $("#assignmentslist input[type=checkbox]:checked").each(function(index, data){
  131. //only refresh item list when last item is removed
  132. deleteAssignment($(data).attr("name"), false, index === ($("#assignmentslist input[type=checkbox]:checked").length -1))
  133. });
  134. }
  135. }
  136. function deleteAssignment(assignmentID, ask = true, refresh = true) { //Delete From DB
  137. if (!ask || confirm('Are you really, really, REALLY sure!')) {
  138. $.ajax({
  139. type: "DELETE",
  140. url: "../server/assignments/" + assignmentID,
  141. success: function(res) {
  142. if(refresh)
  143. initAssignment();
  144. }
  145. });
  146. }
  147. }
  148. function deleteSingleAssignment(assignmentID, patternID) { //Delete From DB
  149. if (confirm('Are you really, really, REALLY sure!')) {
  150. $.ajax({
  151. type: "DELETE",
  152. url: "../server/assignments/" + assignmentID + "/" + patternID,
  153. success: function(res) {
  154. //initAssignment();
  155. createAssignmentForm(assignmentID, $('#addExternalImageAssignment').val())
  156. }
  157. });
  158. }
  159. }
  160. function newSearchAssignment(station, pattern, searchtext){
  161. console.log(station + pattern + searchtext);
  162. $('#searchAssingmentStation').val(station);
  163. $('#searchAssingmentPattern').val(pattern);
  164. $('#searchAssingmentText').val(searchtext);
  165. searchAssignmentForm();
  166. }
  167. function searchAssignmentForm(){
  168. $('#assignmentslist tr').css('background-color', 'white');
  169. $.getJSON( "../server/assignments/search/?pattern=" + $('#searchAssingmentPattern').val() + "&station=" + $('#searchAssingmentStation option:selected').text() + "&text=" + $('#searchAssingmentText').val(), function( data ) {
  170. $.each( data, function(i, item){
  171. $('tr[name="' + item +'"]').css('background-color', '#73d216') // Matches exactly 'tcol1'
  172. });
  173. });
  174. }
  175. function initAssignment(){
  176. $('#assignmentslist').text("");
  177. $.getJSON( "../server/assignments/", function( data ) {
  178. $.each( data, function(i, item){
  179. var clonePattern = document.importNode(document.querySelector('#assignmentitem').content,true);
  180. $('[data-class=tr]',clonePattern).attr('name', item);
  181. $('[data-class=image]',clonePattern).attr('src', item);
  182. $('[data-class=item] a',clonePattern).text(item);
  183. $('[data-class=item] a',clonePattern).attr('href','javascript:createAssignmentForm(' + i + ',"' + item + '");');
  184. //$('[data-class=duplicate] a',clonePattern).attr('href','javascript:duplicatePattern(' + curstation + ','+ $(this).attr('id') + ');');
  185. $('[data-class=multiRemovelink]',clonePattern).attr('name', i );
  186. $('[data-class=delete] a',clonePattern).attr('href','javascript:deleteAssignment(' + i + ');');
  187. $('#assignmentslist').append(clonePattern);
  188. });
  189. });
  190. $('#searchAssingmentStation').find('option').remove()
  191. $.ajax({
  192. type: "GET",
  193. url: "../server/",
  194. dataType: "xml",
  195. success: function(xml) {
  196. $(xml).find('station').each(function(index){
  197. $('#searchAssingmentStation').append('<option value="' + $(this).attr('id') +'">' + $(this).attr('id') +'</option>');
  198. });
  199. }
  200. });
  201. }
  202. function selectAll(){
  203. $(':input','#assignmentslist')
  204. .not(':button, :submit, :reset, :hidden')
  205. .val('')
  206. .prop('checked', true)
  207. .prop('selected', true);
  208. }
  209. function unselectAll(){
  210. $(':input','#assignmentslist')
  211. .not(':button, :submit, :reset, :hidden')
  212. .val('')
  213. .prop('checked', false)
  214. .prop('selected', false);
  215. }
  216. function initAssignmentTable(){
  217. $('#assignmentdetaillist').text("");
  218. $('#assignmentdetaillist').append("<tr id='copyAssignmentTable'></tr>");
  219. $('#assignmentdetaillist tr').append("<td data-class='station'>Station</td>");
  220. $.getJSON( "../server/json/ProductCode.json", function( data ) {
  221. $.each( data["pattern"], function(i, item){
  222. $('#assignmentdetaillist tr').append("<td data-class='part"+ item["abk"] +"'>"+ item["abk"] +"</td>");
  223. productcodestructure[item["abk"]] = "";
  224. });
  225. $('#assignmentdetaillist tr').append("<td data-class='partTEXT'>Text</td>");
  226. $('#assignmentdetaillist tr').append("<td data-class='delete'>Delete</td>");
  227. $('#assignmentdetaillist tr').append("<td data-class='search'>Search</td>");
  228. });
  229. }
  230. $(document).ready(function() {
  231. initAssignment();
  232. $(document).on('submit', '#assignmentform', function() { //prevent page reload on form submit
  233. return false;
  234. });
  235. });