Guest User

Untitled

a guest
Apr 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. //function that creates table of all documents in library
  2. //parameter is an html block for the table
  3. function updateDocumentsTable(object) {
  4. //form a table
  5. var html = "<table id='content-tab' class='adm-table'><tr class='unselectable'><th>ID</th><th>Type</th><th>Title</th><th>Authors</th><th>Price</th><th>Info</th></tr>";
  6.  
  7. //get JSON info about documents through API function
  8. var docs_table = JSON.parse(getDocuments());
  9.  
  10. //form a rows of table with info about each book
  11. docs_table.forEach(function (value) {
  12. var type = "";
  13. if (value.type == 0) {
  14. type = "Book";
  15. } else if (value.type == 1) {
  16. type = "Article";
  17. } else {
  18. type = "Media";
  19. }
  20.  
  21. html += "<tr><td>" + value.id + "</td><td>" + type + "</td><td>" + value.title + "</td><td>" + value.authors + "</td><td>" + value.price + "</td><td class='info-cell' name='" + value.id + "'><a href='#'>More...</a></td></tr>";
  22. });
  23.  
  24. //finish creating table
  25. html += "</table>";
  26.  
  27. //clean old version of table and write a new one
  28. $(object).empty();
  29. $(object).append(html);
  30.  
  31. //popup consists of additional info about the doc
  32. $(".info-cell").click(function () {
  33. //get id of the doc and show popup block
  34. var id = $(this).attr("name");
  35. popupShow();
  36.  
  37. //write the doc info into the popup block
  38. popupMoreDocument($("#popup"), id);
  39. return false;
  40. });
  41.  
  42. //construction that allows choose rows of the table
  43. $("#content-tab tr:not(.unselectable)").click(function () {
  44. $(this).addClass("selected").siblings().removeClass("selected");
  45. });
  46. }
  47.  
  48. //forms a popup window with all necessary fields to create new doc
  49. //parameter is an html block for popups
  50. function popupAddDocument(object) {
  51. //clean old info in the popup block
  52. $(object).empty();
  53.  
  54. //fields for doc info
  55. var html = "<select id='popup-sel'><option selected='true' disabled>Choose type of document</option><option name='0'>Book</option><option name='1'>Article</option><option name='2'>Media</option> </select>" +
  56. "<p>Title: <input id='title'> Count: <input id='count' type='number'> Authors: <input id='authors'></p>" +
  57. "<p>Price: <input id='price' type='number'> Keywords: <input id='keywords'></p>" +
  58. "<div id='add-info'></div>";
  59. //show fields appliable for all docs
  60. $(object).append(html);
  61.  
  62. //while selecting the type of document, new fields appears
  63. $("#popup-sel").change(function () {
  64. //clean old fields and apply new ones depending on type
  65. $("#add-info").empty();
  66. switch ($("#popup-sel option:selected").attr("name")) {
  67. case '0':
  68. html = "<p><label for='bestseller'>Bestseller</label>: <input id='bestseller' type='checkbox' value='best'> Publisher: <input id='publisher'> Edition: <input id='edition' type='number'></p>" +
  69. "<p>Publication year: <input id='pub-year' type='number' min='1900' max='2018'> <label for='reference'>Reference</label>: <input id='reference' type='checkbox' value='ref'></p>";
  70. $("#add-info").append(html);
  71. break;
  72. case '1':
  73. html = "<p>Journal Title: <input id='j-title'> Publication date: <input id='j-pub-date' type='date'> Editors: <input id='j-editors'></p>" +
  74. "<p>Reference: <input id='reference' type='checkbox'></p>";
  75. $("#add-info").append(html);
  76. break;
  77. }
  78. });
  79.  
  80. //create buttons Add and Cancel
  81. html = "<p><button id='apply'>Add</button><button id='exit'>Cancel</button>";
  82. $(object).append(html);
  83.  
  84. //forward info to the API related function to add a document
  85. $("#apply").click(function () {
  86. if ($("#popup-sel option:selected").html()) {
  87. var type = $("#popup-sel option:selected").attr("name");
  88. var title = $("#title").val();
  89. var count = $("#count").val();
  90. var authors = $("#authors").val();
  91. var price = $("#price").val();
  92. var keywords = $("#keywords").val();
  93. var bestseller = $("#besteller:checked").val() ? '1' : '0';
  94. var publisher = $("#publisher").val() || null;
  95. var edition = $("#edition").val() || null;
  96. var pub_year = $("#pub-year").val() || null;
  97. var reference = $("#reference:checked").val() ? '1' : '0';
  98. var j_title = $("#j-title").val() || null;
  99. var j_pub_date = $("#j-pub-date").val() || null;
  100. var j_editors = $("#j-editors").val() || null;
  101.  
  102. //API function
  103. addDocument(type, title, count, authors, price, keywords, bestseller, publisher, edition, pub_year, reference, j_title, j_pub_date, j_editors);
  104. //clean popup block and hide it
  105. $(object).empty();
  106. popupHide();
  107. //update table to see the results
  108. updateDocumentsTable($("#content-table"));
  109. } else {
  110. alert("Choose a type!");
  111. }
  112.  
  113. return false;
  114. });
  115. //events to close the popup
  116. $("#exit, #hider").click(function () {
  117. $(object).empty();
  118. popupHide();
  119. });
  120.  
  121. }
  122.  
  123. //functon that displays additional info about the doc
  124. //parameters are html block for popups and id of the doc
  125. function popupMoreDocument(object, id) {
  126. //clean popup block
  127. $(object).empty();
  128.  
  129. //get JSON info about the document through API function
  130. var doc = JSON.parse(getDocument(id));
  131.  
  132. //collect all the information
  133. var type = doc.type == 0 ? 'Book' : (doc.type == 1 ? 'Article' : 'Media');
  134. var edition = doc.edition == 1 ? doc.edition + 'st' : (doc.edition == 2 ? doc.edition + 'nd' : (doc.edition == 3 ? doc.edition + 'rd' : doc.edition + 'th'));
  135. var descr = doc.type == 1 ? '(published ' + doc.journalIssuePublicationDate + ')' + ' in journal <b>' + doc.journalTitle + '</b> edited by <i>' + doc.journalIssueEditors + '</i>' : (doc.type == 0 ? 'published in ' + doc.publicationYear + ' (' + edition + ' edition)' : '');
  136.  
  137. var bestseller = doc.bestseller == 1 ? "<div style='padding: 10px; background: yellow'>BESTSELLER</div> " : "";
  138. var reference = doc.reference == 1 ? "<div style='padding: 10px; background: red'>REFERENCE</div> " : "";
  139.  
  140. //show info about the doc in a formatted way
  141. var html = "<h2>" + doc.title + "</h2>" +
  142. "<p><i>by " + doc.authors + "</i></p></br></br>" +
  143. "<p><b>" + type + "</b> " + descr + "</p>" +
  144. "</br><p>" + doc.instockCount + " copies left (price: " + doc.price + ")</p>" +
  145. "<p>Keywords: <i>" + doc.keywords + "</i></p>" +
  146. bestseller + "</br>" + reference;
  147.  
  148. //write into the popup block
  149. $(object).append(html);
  150.  
  151. //event to close the popup window
  152. $("#hider").click(function () {
  153. popupHide();
  154. });
  155. }
  156.  
  157. //shows popup block
  158. function popupShow() {
  159. $("#hider, #popup").css("visibility", "visible");
  160. }
  161.  
  162. //hides popup block
  163. function popupHide() {
  164. $("#hider, #popup").css("visibility", "hidden");
  165. }
Add Comment
Please, Sign In to add comment