design.js 19 KB

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