design.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. //delete = delets from DB
  2. //remove = removes from HTML
  3. //save = save to DB
  4. //add = changes HTML
  5. //get = get from DB
  6. //new = changes HTML
  7. function overlayOn(value) {
  8. url = "https://centurio.work/customers/evva/universal-storage/ui/?iframe"
  9. document.getElementById("overlay").style.display = "block";
  10. var iframe = $('#overlaycontentsize').append("<iframe id='overlaycontent' src='" + url + "' width='900' height='600'></iframe>");
  11. iframe.find("#overlaycontent").bind('load',{"ka": value}, function(event){
  12. var ref = this.contentWindow.ref;
  13. //You can think that ref = $(this.contentWindow.test) but thery are different
  14. //they are two diferent references this might be a bug
  15. ref.bind('getUrl',{"xyz": event.data.ka}, function(event, extra1){
  16. var data = event.data
  17. overlayOff();
  18. //fill input with url
  19. $(data.xyz).prev().val(extra1.url)
  20. });
  21. });
  22. }
  23. function overlayOnImg(data) {
  24. var url = new URL("" + data, document.baseURI).href
  25. document.getElementById("overlay").style.display = "block";
  26. const imageext = ["png", "jpg", "svg"]
  27. const movieext = ["mp4"]
  28. var ext = url.substr(url.lastIndexOf('.') + 1);
  29. if(imageext.includes(ext)){
  30. $('#overlaycontentsize').append("<img id='overlaycontent' src=" + url + "></img>");
  31. }
  32. else if(movieext.includes(ext)){
  33. $('#overlaycontentsize').append("<video id='overlaycontent' controls> <source src=" + url + " type=\"video/mp4\">Your browser does not support the video tag.</video>");
  34. }
  35. else{
  36. $('#overlaycontentsize').append("<iframe id='overlaycontent' src=" + url + "></iframe>");
  37. }
  38. }
  39. function overlayOff() {
  40. document.getElementById("overlay").style.display = "none";
  41. $('#overlaycontentsize').text("");
  42. }
  43. function saveStation() { //Save 2 DB
  44. if (confirm('Are you really, really, REALLY sure you want to create a New Station?')) {
  45. $.ajax({
  46. type: "POST",
  47. url: "../server/",
  48. success: function(res) {
  49. location.reload();
  50. }
  51. });
  52. }
  53. }
  54. function getStations(stationID , patternID ){ //Get DB 2 HTML
  55. $('#stations').text("");
  56. $.ajax({
  57. type: "GET",
  58. url: "../server/",
  59. dataType: "xml",
  60. success: function(xml) {
  61. $(xml).find('station').each(function(index){
  62. var clone = document.importNode(document.querySelector('#line').content,true);
  63. $('[data-class=station]',clone).text($(this).attr('id'));
  64. $('[data-class=pattern] a',clone).before("[");
  65. $('[data-class=pattern] a',clone).text("Add Pattern");
  66. $('[data-class=pattern] a',clone).after("]");
  67. $('[data-class=pattern] a',clone).attr('href','javascript:newPattern('+ $(this).attr('id') + ');');
  68. $('[data-class=duplicate]',clone).text("");
  69. $('[data-class=delete]',clone).text("");
  70. $('#stations').append(clone);
  71. var curstation=$(this).attr('id');
  72. $(this).find('pattern').each(function(index){
  73. var clonePattern = document.importNode(document.querySelector('#line').content,true);
  74. $('[data-class=pattern] a',clonePattern).text($(this).attr('value'));
  75. $('[data-class=pattern] a',clonePattern).attr('href','javascript:getPattern(' + curstation + ','+ $(this).attr('id') + ');');
  76. $('[data-class=date]',clonePattern).text($.format.date(Date.parse($(this).attr('changed')), 'yyyy/MM/dd HH:mm:ss'));
  77. $('[data-class=duplicate] a',clonePattern).attr('href','javascript:duplicatePattern(' + curstation + ','+ $(this).attr('id') + ');');
  78. $('[data-class=delete] a',clonePattern).attr('href','javascript:deletePattern(' + curstation + ','+ $(this).attr('id') + ');');
  79. if (patternID != null && patternID == $(this).attr('id')){
  80. $('tr',clonePattern).css("background-color","#99cce6");
  81. }
  82. $('#stations').append(clonePattern);
  83. });
  84. });
  85. }
  86. });
  87. }
  88. function newPattern(stationID) { //HTML
  89. getPattern(stationID);
  90. }
  91. function getPattern(stationID, patternID){ //Get DB 2 HTML
  92. getStations(stationID, patternID)
  93. if (patternID == null){
  94. $('#patternform').attr('onsubmit', "savePattern("+stationID+")");
  95. $('#pattern_ID').text("");
  96. $('#pattern_description').val("");
  97. $('#imageListing').text("");
  98. $('#images').css("display", "none");
  99. $('#remove_pattern').css("display", "none");
  100. $('#error').css("display", "none");
  101. $('#replacements').css("display", "none");
  102. }
  103. else{
  104. $('#patternform').attr('onsubmit', "saveChangedPattern("+stationID+","+ patternID +")");
  105. $('#pattern_ID').text(patternID);
  106. getImages(stationID, patternID);
  107. $('#remove_pattern').css("display", "block");
  108. $('#remove_pattern').attr('onclick', "javascript:deletePattern("+stationID+","+ patternID +")");
  109. getError(stationID, patternID);
  110. getReplaceImages(stationID, patternID);
  111. $.ajax({
  112. type: "GET",
  113. url: "../server/"+stationID+ "/" + patternID,
  114. dataType: "xml",
  115. success: function(xml) {
  116. $(xml).find('pattern').each(function(index){
  117. var pattern = $(this).attr('value').split(".")
  118. for(var i = 0; i < pattern.length; i++)
  119. {
  120. if(i == 0){
  121. $("#patternInput .patternPartInput").val(pattern[i]);
  122. }
  123. else{
  124. var clone = document.importNode(document.querySelector('#patternClone').content,true);
  125. $('[data-class=part]',clone).val(pattern[i]);
  126. $('[data-class=part]',clone).limitkeypress({ rexp: keypressRegex() });
  127. $("#patternInput").append(clone);
  128. }
  129. }
  130. $('#pattern').val($(this).attr('value'));
  131. $('#pattern_description').val($(this).attr('description'));
  132. });
  133. }
  134. });
  135. }
  136. $('#pattern_details').css("display", "block");
  137. $('#pattern_station').text(stationID);
  138. $('#pattern_description').text("");
  139. //Reset Pattern
  140. $('#patternInput').text("");
  141. var clone = document.importNode(document.querySelector('#patternClone').content,true);
  142. $('[data-class=part]',clone).limitkeypress({rexp: keypressRegex()});
  143. $("#patternInput").append(clone);
  144. }
  145. function keypressRegex(){ //Helper Function
  146. return /^[A-Za-z0-9\*]*$/;
  147. }
  148. function removePatternPart(value) { //HTML
  149. if($( ".patternPart" ).length > 1)
  150. value.parent().remove();
  151. }
  152. function addPatternPart(useLast) { //HTML
  153. var clone = document.importNode(document.querySelector('#patternClone').content,true);
  154. $('[data-class=part]',clone).val("*");
  155. $('[data-class=part]',clone).limitkeypress({rexp: keypressRegex()});
  156. if(useLast){
  157. $('.patternPart').last().after(clone);
  158. $('.patternPart').last().children(".patternPartInput").focus();
  159. }else{
  160. $(':focus').parent().after(clone);
  161. $(':focus').parent().next().children(".patternPartInput").focus();
  162. }
  163. }
  164. function getPatternString(){ //Helper Function
  165. var val = "";
  166. var first = true;
  167. $(".patternPartInput").each(function() {
  168. if(first){
  169. first = false
  170. val += $(this).val()
  171. }
  172. else{
  173. val += "." + $(this).val()
  174. }
  175. });
  176. return val;
  177. }
  178. function savePattern(stationID) { //Save 2 DB
  179. var xmlDocument = $.parseXML("<pattern/>");
  180. $(xmlDocument).find("pattern").attr('id','0');
  181. $(xmlDocument).find("pattern").attr('value',getPatternString());
  182. $(xmlDocument).find("pattern").attr('description',$('#pattern_description').val());
  183. $(xmlDocument).find("pattern").attr('changed',new Date().toISOString());
  184. $.ajax({
  185. type: "POST",
  186. headers: {"content-id": "pattern"},
  187. data: (new XMLSerializer()).serializeToString(xmlDocument),
  188. contentType: "application/xml; charset=utf-8",
  189. url: "../server/"+stationID,
  190. success: function(res) {
  191. getPattern(stationID, res.id)
  192. loadStations()
  193. },
  194. error: function (request, status, error) {
  195. alert(request.responseText + status + error);
  196. }
  197. });
  198. }
  199. function saveChangedPattern(stationID, patternID) { //Save 2 DB
  200. var xmlDocument = $.parseXML("<pattern/>");
  201. $(xmlDocument).find("pattern").attr('id',$('#pattern_ID').text());
  202. $(xmlDocument).find("pattern").attr('value',getPatternString());
  203. $(xmlDocument).find("pattern").attr('description',$('#pattern_description').val());
  204. $(xmlDocument).find("pattern").attr('changed',new Date().toISOString());
  205. //Save Pattern
  206. $.ajax({
  207. type: "PUT",
  208. headers: {"content-id": "pattern"},
  209. data: (new XMLSerializer()).serializeToString(xmlDocument),
  210. contentType: "application/xml; charset=utf-8",
  211. url: "../server/"+stationID + "/" + patternID,
  212. //processData: false,
  213. success: function(res) {
  214. },
  215. error: function (request, status, error) {
  216. alert(request.responseText + status + error);
  217. }
  218. });
  219. //Update Labels
  220. $(".imageInput").each(function() {
  221. //Optimization only update if lable changed
  222. if($(this).val() != $(this).attr("origValue"))
  223. $.ajax({
  224. type: "PUT",
  225. data: { 'label' : $(this).val()},
  226. headers: {"content-id": "label"},
  227. url: "../server/"+stationID + "/" + patternID + "/images/" + $(this).parent().find(".imageID").text() +"/" + $('#image_language option:selected').text(),
  228. success: function(res) {
  229. },
  230. error: function (request, status, error) {
  231. alert(request.responseText + status + error);
  232. }
  233. });
  234. });
  235. //Update Image Ordering
  236. var order = []
  237. $(".imageID").each(function() {
  238. order.push(parseInt($(this).text()))
  239. });
  240. $.ajax({
  241. type: "PUT",
  242. data: JSON.stringify(order),
  243. headers: {"content-id": "orderlist"},
  244. contentType: "application/json",
  245. url: "../server/"+stationID + "/" + patternID + "/images/reorder/" + $('#image_language option:selected').text(),
  246. success: function(res) {
  247. },
  248. error: function (request, status, error) {
  249. alert(request.responseText + status + error);
  250. }
  251. });
  252. //Update Error
  253. var errorList = $.parseXML("<error/>");
  254. $(".errorInput").each(function() {
  255. $(errorList).find("error").append("<reason>" +$(this).val()+"</reason>")
  256. });
  257. $.ajax({
  258. type: "PUT",
  259. headers: {"content-id": "error"},
  260. data: (new XMLSerializer()).serializeToString(errorList),
  261. contentType: "application/xml; charset=utf-8",
  262. url: "../server/"+stationID + "/" + patternID + "/error",
  263. success: function(res) {
  264. },
  265. error: function (request, status, error) {
  266. alert(request.responseText + status + error);
  267. }
  268. });
  269. //Update Replacement
  270. var replaceList = $.parseXML("<replacement/>");
  271. $(".replaceInput").each(function() {
  272. //console.log($(this).find('.abbreviation').val())
  273. //console.log($(this).find('.url').val())
  274. $(replaceList).find("replacement").append("<item>" + "<abbreviation>" + $(this).find('.abbreviation').val() + "</abbreviation>" + "<url>" + $(this).find('.url').val() + "</url>"+"</item>")
  275. });
  276. $.ajax({
  277. type: "PUT",
  278. headers: {"content-id": "replacement"},
  279. data: (new XMLSerializer()).serializeToString(replaceList),
  280. contentType: "application/xml; charset=utf-8",
  281. url: "../server/"+stationID + "/" + patternID + "/replacement",
  282. success: function(res) {
  283. },
  284. error: function (request, status, error) {
  285. alert(request.responseText + status + error);
  286. }
  287. });
  288. getPattern(stationID, patternID)
  289. }
  290. function deletePattern(stationID, patternID) { //Delete From DB
  291. if (confirm('Are you really, really, REALLY sure!')) {
  292. $.ajax({
  293. type: "DELETE",
  294. url: "../server/" + stationID + "/" + patternID,
  295. success: function(res) {
  296. location.reload();
  297. }
  298. });
  299. }
  300. }
  301. function duplicatePattern(stationID, patternID){ //Get DB 2 HTML
  302. $.ajax({
  303. type: "PUT",
  304. headers: {"content-id": "patternID"},
  305. data: { "patternID":patternID},
  306. contentType: "application/json",
  307. url: "../server/"+stationID + "/" + patternID,
  308. success: function(res) {
  309. getPattern(stationID, res["id"])
  310. },
  311. error: function (request, status, error) {
  312. alert(request.responseText + status + error);
  313. }
  314. });
  315. }
  316. function getImages(stationID, patternID){ //Get From DB 2 HTML
  317. $('#images').css("display", "block");
  318. $('#imageListing').text("");
  319. $('#imgUpload').attr("action", "../server/" + stationID + "/" + patternID + "/images");
  320. $.ajax({
  321. type: "GET",
  322. url: "../server/"+stationID+ "/" + patternID + "/images",
  323. dataType: "xml",
  324. success: function(xml) {
  325. var count = 0;
  326. $(xml).find('image').each(function(index){
  327. curID = $(this).attr('id')
  328. $(this).find('variant').each(function(){
  329. if($(this).attr('lang') == $('#image_language option:selected').text()){
  330. ++count;
  331. var clone = document.importNode(document.querySelector('#imagePreview').content,true);
  332. var imgsrcsingle = "../images/uploads/"+stationID+ "/" + patternID + "/" + curID + "/"+ $(this).attr('lang') + ".svg";
  333. var imgsrc = imgsrcsingle + "?" + Date.now();
  334. $('[data-class=image]',clone).append('<img id="theImg" src="' + imgsrc +'" onclick="overlayOnImg(\'' +imgsrcsingle + '\')" height="100px" />')
  335. $('[data-class=label]',clone).val($(this).attr('label'));
  336. $('[data-class=label]',clone).attr("origValue", $(this).attr('label'));
  337. $('[data-class=imageId]',clone).text(curID);
  338. $('[data-class=imageId]',clone).attr("class", "imageID")
  339. $('[data-class=options] a',clone).attr('href','javascript:deleteImage(' + stationID + ','+ patternID + ',' + curID + ',"' + $(this).attr('lang') + '");');
  340. $('#imageListing').append(clone);
  341. }
  342. });
  343. });
  344. $('#image_count').text(count);
  345. }
  346. });
  347. new Sortable(document.getElementById('imageListing'),{
  348. handle: '.handle',
  349. animation: 150
  350. });
  351. }
  352. function deleteImage(stationID, patternID, imageID, lang) { //Delete From DB
  353. $.ajax({
  354. type: "DELETE",
  355. url: "../server/" + stationID + "/" + patternID + "/images/" + imageID + "/" + lang,
  356. success: function(res) {
  357. getPattern(stationID, patternID)
  358. }
  359. });
  360. }
  361. function addError() { //HTML
  362. //Will be added from DB on save
  363. var clone = document.importNode(document.querySelector('#errorCode').content,true);
  364. $('#errorCodes').append(clone);
  365. }
  366. function getError(stationID, patternID){ //Get From DB 2 HTML
  367. $('#error').css("display", "block");
  368. $('#errorCodes').text("");
  369. $.ajax({
  370. type: "GET",
  371. url: "../server/"+stationID+ "/" + patternID + "/error",
  372. dataType: "xml",
  373. success: function(xml) {
  374. $(xml).find('reason').each(function(index){
  375. var clone = document.importNode(document.querySelector('#errorCode').content,true);
  376. $('[data-class=errorInput]',clone).val($(this).text());
  377. $('#errorCodes').append(clone);
  378. });
  379. }
  380. });
  381. }
  382. function removeError(value) { //HTML
  383. //Will be removed from DB on save
  384. value.parent().parent().parent().remove();
  385. }
  386. function addBlankReplacement() { //HTML
  387. //Will be added from DB on save
  388. var clone = document.importNode(document.querySelector('#imageReplacements').content,true);
  389. $('#replaceImages').append(clone);
  390. }
  391. function getReplaceImages(stationID, patternID){ //Get From DB 2 HTML
  392. $('#replacements').css("display", "block");
  393. $('#replaceImages').text("");
  394. $.ajax({
  395. type: "GET",
  396. url: "../server/"+stationID+ "/" + patternID + "/replacement",
  397. dataType: "xml",
  398. success: function(xml) {
  399. $(xml).find('item').each(function(index){
  400. var clone = document.importNode(document.querySelector('#imageReplacements').content,true);
  401. $('[data-class=abbreviation]',clone).val($(this).find('abbreviation').text());
  402. $('[data-class=url]',clone).val($(this).find('url').text());
  403. $('#replaceImages').append(clone);
  404. });
  405. }
  406. });
  407. }
  408. function removeReplacement(value) { //HTML
  409. //Will be removed from DB on save
  410. value.parent().parent().parent().remove();
  411. }
  412. //Go to next PatternInput on . or space keydown
  413. $(window).keydown(function(e) {
  414. if ((e.which === 32 || e.which === 190) && $(':focus').attr('class') === "patternPartInput") {
  415. e.preventDefault();
  416. addPatternPart(false);
  417. }
  418. });
  419. $(document).ready(function() {
  420. getStations(); //Get all Stations
  421. $("#image_language" ).change(function() {
  422. getImages($('#pattern_station').text(), $('#pattern_ID').text())
  423. });
  424. $(document).on('submit', '#patternform', function() { //prevent page reload on form submit
  425. return false;
  426. });
  427. $('.patternPartInput').limitkeypress({rexp: keypressRegex()});
  428. });