page.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. function Show_tables_in_landing_page (db) {
  2. Load_first_page(db, function (page) {
  3. if (page) {
  4. Load_HTML_for_page(page.pg_nr);
  5. selected_page = page.pg_nr;
  6. } else {
  7. Show_new_page_tables(true);
  8. }
  9. });
  10. }
  11. function Save_to_new_page (db, page_name, table_positions, callback) {
  12. Create_new_page(db, page_name, function (page) {
  13. if (page) {
  14. var tbl_cords = [];
  15. for (var pos = 0; pos < table_positions.length; pos++) {
  16. table_positions[pos].pdf_pg_nr = page.pg_nr;
  17. Save_table_positions(table_positions[pos], function (id) {
  18. tbl_cords.push(id);
  19. if (table_positions.length === tbl_cords.length) {
  20. page.tbl_cords = tbl_cords;
  21. DesignerOfflineDB.addObject('pdf_pages', page);
  22. }
  23. });
  24. }
  25. if (typeof callback !== 'undefined') {
  26. callback(page);
  27. }
  28. }
  29. });
  30. }
  31. function Save_to_selected_page (db, page_id, page_name, table_positions, callback) {
  32. Delete_page(page_id);
  33. Save_to_new_page(db, page_name, table_positions, function (page) {
  34. if (typeof callback !== 'undefined') {
  35. callback(page);
  36. }
  37. selected_page = page.pg_nr;
  38. });
  39. }
  40. function Create_new_page (db, page_name, callback) {
  41. var newPage = new PDFPage(db, page_name);
  42. DesignerOfflineDB.addObject('pdf_pages', newPage, function (pg_nr) {
  43. newPage.pg_nr = pg_nr;
  44. if (typeof callback !== 'undefined') {
  45. callback(newPage);
  46. }
  47. });
  48. }
  49. function Save_table_positions (positions, callback) {
  50. DesignerOfflineDB.addObject('table_coords', positions, callback);
  51. }
  52. function Create_page_list (db, callback) {
  53. DesignerOfflineDB.loadAllObjects('pdf_pages', function (pages) {
  54. var html = '';
  55. for (var p = 0; p < pages.length; p++) {
  56. var page = pages[p];
  57. if (page.db_name === db) {
  58. html += '<option value="' + page.pg_nr + '">';
  59. html += escapeHtml(page.page_descr) + '</option>';
  60. }
  61. }
  62. if (typeof callback !== 'undefined') {
  63. callback(html);
  64. }
  65. });
  66. }
  67. function Delete_page (page_id, callback) {
  68. DesignerOfflineDB.loadObject('pdf_pages', page_id, function (page) {
  69. if (page) {
  70. for (var i = 0; i < page.tbl_cords.length; i++) {
  71. DesignerOfflineDB.deleteObject('table_coords', page.tbl_cords[i]);
  72. }
  73. DesignerOfflineDB.deleteObject('pdf_pages', page_id, callback);
  74. }
  75. });
  76. }
  77. function Load_first_page (db, callback) {
  78. DesignerOfflineDB.loadAllObjects('pdf_pages', function (pages) {
  79. var firstPage = null;
  80. for (var i = 0; i < pages.length; i++) {
  81. var page = pages[i];
  82. if (page.db_name === db) {
  83. // give preference to a page having same name as the db
  84. if (page.page_descr === db) {
  85. callback(page);
  86. return;
  87. }
  88. if (firstPage === null) {
  89. firstPage = page;
  90. }
  91. }
  92. }
  93. callback(firstPage);
  94. });
  95. }
  96. function Show_new_page_tables (check) {
  97. var all_tables = $('#id_scroll_tab').find('td input:checkbox');
  98. all_tables.prop('checked', check);
  99. for (var tab = 0; tab < all_tables.length; tab++) {
  100. var input = all_tables[tab];
  101. if (input.value) {
  102. var element = document.getElementById(input.value);
  103. element.style.top = Get_random(550, 20) + 'px';
  104. element.style.left = Get_random(700, 20) + 'px';
  105. VisibleTab(input, input.value);
  106. }
  107. }
  108. selected_page = -1;
  109. $('#page_name').text(PMA_messages.strUntitled);
  110. MarkUnsaved();
  111. }
  112. function Load_HTML_for_page (page_id) {
  113. Show_new_page_tables(false);
  114. Load_page_objects(page_id, function (page, tbl_cords) {
  115. $('#name-panel').find('#page_name').text(page.page_descr);
  116. MarkSaved();
  117. for (var t = 0; t < tbl_cords.length; t++) {
  118. var tb_id = db + '.' + tbl_cords[t].table_name;
  119. var table = document.getElementById(tb_id);
  120. table.style.top = tbl_cords[t].y + 'px';
  121. table.style.left = tbl_cords[t].x + 'px';
  122. var checkbox = document.getElementById('check_vis_' + tb_id);
  123. checkbox.checked = true;
  124. VisibleTab(checkbox, checkbox.value);
  125. }
  126. selected_page = page.pg_nr;
  127. });
  128. }
  129. function Load_page_objects (page_id, callback) {
  130. DesignerOfflineDB.loadObject('pdf_pages', page_id, function (page) {
  131. var tbl_cords = [];
  132. var count = page.tbl_cords.length;
  133. for (var i = 0; i < count; i++) {
  134. DesignerOfflineDB.loadObject('table_coords', page.tbl_cords[i], function (tbl_cord) {
  135. tbl_cords.push(tbl_cord);
  136. if (tbl_cords.length === count) {
  137. if (typeof callback !== 'undefined') {
  138. callback(page, tbl_cords);
  139. }
  140. }
  141. });
  142. }
  143. });
  144. }
  145. function Get_random (max, min) {
  146. var val = Math.random() * (max - min) + min;
  147. return Math.floor(val);
  148. }