ustore.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. //CrossSiteReference needed for WAS
  8. var test = {};
  9. var ref = $(test);
  10. $(document).on('change', '#selectfolders', function(e) {
  11. //getFolders(this.options[e.target.selectedIndex].text);
  12. listFolders(this.options[e.target.selectedIndex].text);
  13. });
  14. function listFolders(folder = ""){
  15. $('#Folders').text("");
  16. $('#CurFolder').text("");
  17. toggleRemoveLinks(true);
  18. getAllFolders();
  19. if(folder != "" && folder != "." && folder != ".."){
  20. $("#selectfolders").val(folder);
  21. }
  22. else{
  23. $("#selectfolders").val(".");
  24. }
  25. $.ajax({
  26. type: "GET",
  27. url: "../server/folders/" + folder,
  28. dataType: "json",
  29. success: function(data) {
  30. if(folder != "" && folder != "." && folder != ".."){
  31. var clone = document.importNode(document.querySelector('#cpyFolder').content,true);
  32. $('[data-class=text]',clone).text("..");
  33. $('[data-class=link]',clone).attr('onclick','listFolders("' + folder.substr(0, folder.lastIndexOf("/")) + '")');
  34. // dont show remove link on .. folder
  35. $('[data-class=removefolder]',clone).remove()
  36. $('#Folders').append(clone);
  37. }
  38. $.each( data, function( key, value ) {
  39. var clone = document.importNode(document.querySelector('#cpyFolder').content,true);
  40. //remove /
  41. value = value.slice(0,-1)
  42. $('[data-class=text]',clone).text(value);
  43. if(folder == ""){
  44. $('[data-class=link]',clone).attr('onclick','listFolders("' + value + '")');
  45. $('[data-class=removefolder]',clone).attr('onclick','deleteFolder("' + value + '")');
  46. }
  47. else{
  48. $('[data-class=link]',clone).attr('onclick','listFolders("' + folder + "/" + value + '")');
  49. $('[data-class=removefolder]',clone).attr('onclick','deleteFolder("' + folder + "/" + value + '")');
  50. }
  51. $('#Folders').append(clone);
  52. });
  53. //new Folder
  54. var clone = document.importNode(document.querySelector('#cpyFolderNew').content,true);
  55. $('[data-class=text]',clone).text("+");
  56. $('[data-class=link]',clone).attr('onclick','createFolder("' + folder + '")');
  57. $('#Folders').append(clone);
  58. }
  59. });
  60. listData(folder)
  61. }
  62. function createFolder(folder = ""){
  63. var newFolder = window.prompt("Please enter a folder name.","");
  64. if (newFolder != null && newFolder != "") {
  65. $.ajax({
  66. type: "Post",
  67. url: "../server/folders/" + folder + "/" + newFolder,
  68. success: function(res) {
  69. listFolders(folder)
  70. }
  71. });
  72. }
  73. }
  74. function deleteFolder(datalink) {
  75. if (datalink != "" && datalink != "." && datalink != ".." && confirm('Deleting ' + datalink)) {
  76. $.ajax({
  77. type: "DELETE",
  78. url: "../server/folders/" + datalink,
  79. success: function(res) {
  80. listFolders(datalink.substr(0, datalink.lastIndexOf("/")))
  81. }
  82. });
  83. }
  84. }
  85. function listData(folder = ""){
  86. $('#Data').text("");
  87. const imageext = ["png", "jpg", "svg", "JPG", "PNG", "SVG"]
  88. const movieext = ["mp4"]
  89. $('#imgUpload').attr("action", "../server/data/" + folder);
  90. $.ajax({
  91. type: "GET",
  92. url: "../server/images/" + folder,
  93. dataType: "json",
  94. success: function(data) {
  95. $.each( data, function( key, value ) {
  96. var ext = value.substr(value.lastIndexOf('.') + 1);
  97. if(folder == ""){
  98. fullfolder = value
  99. }
  100. else{
  101. fullfolder = folder + "/" + value;
  102. }
  103. var clone = document.importNode(document.querySelector('#DataItem').content,true);
  104. $('[data-class=name]',clone).text(value);
  105. //Image
  106. if(imageext.includes(ext)){
  107. $('[data-class=image]',clone).append("<img src='./../storage/"+ fullfolder + "' alt=''>")
  108. }
  109. else if(movieext.includes(ext)){
  110. $('[data-class=image]',clone).append("<div class='imageAlternative' style='padding-top: 0.12em;'>▶</div>")
  111. }
  112. else{
  113. $('[data-class=image]',clone).append("<div class='imageAlternative'>🗎</div>")
  114. }
  115. $('[data-class=image]',clone).attr('onclick',"overlayOn('" + fullfolder + "')");
  116. $('[data-class=inspect]',clone).attr('href','../storage/' + fullfolder + '');
  117. $('[data-class=delete]',clone).attr('href','javascript:deleteData("' + fullfolder + '");');
  118. $('[data-class=multiRemovelink]',clone).attr('name', fullfolder );
  119. $('[data-class=getLink]',clone).attr('href','javascript:copy2Clipboard("' + fullfolder + '");');
  120. $('[data-class=createSymlink]',clone).attr('href','javascript:createSymlink("' + fullfolder + '", "' + value.slice(0, - (ext.length +1)) +'");');
  121. var queryString = window.location.search;
  122. var urlParams = new URLSearchParams(queryString);
  123. if(urlParams.has('iframe')){
  124. $('[data-class=eventTrigger]',clone).css("visibility", "visible")
  125. $('[data-class=eventTrigger]',clone).attr('href','javascript:triggerEvent("' + fullfolder + '");');
  126. console.log("I am an Iframe")
  127. }
  128. $('#Data').append(clone);
  129. });
  130. }
  131. });
  132. if(folder != "" && folder != "." && folder != ".."){
  133. $('#RemoveFolder').css("display", "block")
  134. $('#removeFolderLink').attr('href','javascript:deleteFolder("' + folder + '");');
  135. }
  136. else{
  137. $('#RemoveFolder').css("display", "none")
  138. }
  139. }
  140. function triggerEvent(data) {
  141. //get url
  142. var datalink = new URL("../storage/" + data, document.baseURI).href;
  143. ref = $(test);
  144. setTimeout(function(){
  145. ref.triggerHandler('getUrl', {"url": datalink});
  146. },500);
  147. }
  148. function overlayOn(data) {
  149. var url = new URL("../storage/" + data, document.baseURI).href
  150. document.getElementById("overlay").style.display = "block";
  151. const imageext = ["png", "jpg", "svg", "PNG", "JPG", "SVG"]
  152. const movieext = ["mp4"]
  153. var ext = url.substr(url.lastIndexOf('.') + 1);
  154. if(imageext.includes(ext)){
  155. $('#overlaycontentsize').append("<img id='overlaycontent' src=" + url + "></img>");
  156. }
  157. else if(movieext.includes(ext)){
  158. $('#overlaycontentsize').append("<video id='overlaycontent' controls> <source src=" + url + " type=\"video/mp4\">Your browser does not support the video tag.</video>");
  159. }
  160. else{
  161. $('#overlaycontentsize').append("<iframe id='overlaycontent' src=" + url + "></iframe>");
  162. }
  163. }
  164. function overlayOff() {
  165. document.getElementById("overlay").style.display = "none";
  166. $('#overlaycontentsize').text("");
  167. }
  168. function toggleRemoveLinks(setoff = false) {
  169. if(setoff || $(".showremoveitems").is(":visible")){
  170. $(".showremoveitems").css("display", "none")
  171. $(".showremoveitemsNot").css("display", "inline")
  172. }
  173. else{
  174. $(".showremoveitems").css("display", "inline")
  175. $(".showremoveitemsNot").css("display", "none")
  176. }
  177. }
  178. function selectRemoveItems() {
  179. toggleRemoveLinks()
  180. }
  181. function unselectAll(){
  182. $(':input','#Data')
  183. .not(':button, :submit, :reset, :hidden')
  184. .val('')
  185. .prop('checked', false)
  186. .prop('selected', false);
  187. toggleRemoveLinks()
  188. }
  189. function selectAll(){
  190. $(':input','#Data')
  191. .not(':button, :submit, :reset, :hidden')
  192. .val('')
  193. .prop('checked', true)
  194. .prop('selected', true);
  195. }
  196. function removeSelectedItems(){
  197. var removedItems = [];
  198. $("#Data input[type=checkbox]:checked").each(function(index, data){
  199. removedItems.push($(data).attr("name"));
  200. });
  201. if (confirm('Deleting: ' + removedItems)) {
  202. $("#Data input[type=checkbox]:checked").each(function(index, data){
  203. //only refresh item list when last item is removed
  204. deleteDataItem($(data).attr("name"), index === ($("#Data input[type=checkbox]:checked").length -1))
  205. });
  206. toggleRemoveLinks()
  207. }
  208. }
  209. function deleteData(datalink) {
  210. if (confirm('Deleting ' + datalink)) {
  211. deleteDataItem(datalink)
  212. }
  213. }
  214. function deleteDataItem(datalink, refresh = true) {
  215. $.ajax({
  216. type: "DELETE",
  217. url: "../server/data/" + encodeURIComponent(datalink),
  218. success: function(res) {
  219. if(refresh)
  220. listFolders(datalink.substr(0, datalink.lastIndexOf("/")))
  221. }
  222. });
  223. }
  224. function copy2Clipboard(datalink) {
  225. new URL("../storage/" + datalink, document.baseURI).href
  226. datalink = new URL("../storage/" + datalink, document.baseURI).href;
  227. const el = document.createElement('textarea');
  228. el.value = datalink;
  229. document.body.appendChild(el);
  230. el.select();
  231. document.execCommand('copy');
  232. document.body.removeChild(el);
  233. }
  234. function createSymlink(datalink, oldfilename) {
  235. var filename = prompt("Please enter symlink filename");
  236. if (filename != null && filename != "") {
  237. var sendfilename = {"filename": filename, "oldfilename": oldfilename}
  238. $.ajax({
  239. type: "POST",
  240. data: JSON.stringify(sendfilename),
  241. headers: {"content-id": "list"},
  242. contentType: "application/json",
  243. url: "../server/data/" + datalink,
  244. success: function(res) {
  245. },
  246. error: function (request, status, error) {
  247. alert(request.responseText + status + error);
  248. }
  249. });
  250. }
  251. }
  252. function getAllFolders(folder = ""){ //Get DB 2 HTML //has to be synchronus in order to update before loading the rest
  253. $('#selectfolders').find('option').remove()
  254. $('#selectfolders').append("<option>.</option>")
  255. $.ajax({
  256. type: "GET",
  257. url: "../server/allfolders/" + folder,
  258. async: false,
  259. dataType: "json",
  260. success: function(data) {
  261. $.each( data, function( key, value ) {
  262. $('#selectfolders').append("<option>" + value.slice(0,-1) +"</option>")
  263. });
  264. }
  265. });
  266. }
  267. function saveexternalfolder(folder = ""){
  268. var newFolder = window.prompt("Please enter URL","");
  269. if (newFolder != null && newFolder != "") {
  270. $.ajax({
  271. type: "Post",
  272. data: JSON.stringify({"folder": newFolder}),
  273. contentType: "application/json; charset=utf-8",
  274. dataType: "json",
  275. async:true,
  276. url: "../server/externalFolder/",
  277. success: function(res) {
  278. }
  279. });
  280. }
  281. }
  282. function listExternalFolders(folder = ""){
  283. $('#ExternalFolders').text("");
  284. if(folder == ""){
  285. $.ajax({
  286. type: "GET",
  287. url: "../server/externalFolder",
  288. dataType: "json",
  289. success: function(data) {
  290. $.each( data, function( key, value ) {
  291. var clone = document.importNode(document.querySelector('#cpyFolder').content,true);
  292. //remove /
  293. value = value.slice(0,-1)
  294. $('[data-class=text]',clone).text(value);
  295. $('[data-class=link]',clone).attr('onclick','listExternalFolders("' + value + '")');
  296. $('#ExternalFolders').append(clone);
  297. });
  298. }
  299. });
  300. }
  301. else{
  302. $('#Folders').text("");
  303. //alert($.get(folder));
  304. /*
  305. $.ajax({
  306. type: "GET",
  307. url: folder,
  308. crossDomain: true,
  309. success: function(data) {
  310. alert(data);
  311. }
  312. });
  313. */
  314. }
  315. //listData(folder)
  316. }
  317. var getUrlParameter = function getUrlParameter(sParam) {
  318. var sPageURL = window.location.search.substring(1),
  319. sURLVariables = sPageURL.split('&'),
  320. sParameterName,
  321. i;
  322. for (i = 0; i < sURLVariables.length; i++) {
  323. sParameterName = sURLVariables[i].split('=');
  324. if (sParameterName[0] === sParam) {
  325. return typeof sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
  326. }
  327. }
  328. return false;
  329. };
  330. $(document).ready(function() {
  331. var path = getUrlParameter('path');
  332. if(!path){
  333. path = "";
  334. }
  335. listFolders(path);
  336. //listExternalFolders();
  337. getAllFolders(path);
  338. });