Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //function that creates table of all documents in library
- //parameter is an html block for the table
- function updateDocumentsTable(object) {
- //form a table
- 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>";
- //get JSON info about documents through API function
- var docs_table = JSON.parse(getDocuments());
- //form a rows of table with info about each book
- docs_table.forEach(function (value) {
- var type = "";
- if (value.type == 0) {
- type = "Book";
- } else if (value.type == 1) {
- type = "Article";
- } else {
- type = "Media";
- }
- 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>";
- });
- //finish creating table
- html += "</table>";
- //clean old version of table and write a new one
- $(object).empty();
- $(object).append(html);
- //popup consists of additional info about the doc
- $(".info-cell").click(function () {
- //get id of the doc and show popup block
- var id = $(this).attr("name");
- popupShow();
- //write the doc info into the popup block
- popupMoreDocument($("#popup"), id);
- return false;
- });
- //construction that allows choose rows of the table
- $("#content-tab tr:not(.unselectable)").click(function () {
- $(this).addClass("selected").siblings().removeClass("selected");
- });
- }
- //forms a popup window with all necessary fields to create new doc
- //parameter is an html block for popups
- function popupAddDocument(object) {
- //clean old info in the popup block
- $(object).empty();
- //fields for doc info
- 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>" +
- "<p>Title: <input id='title'> Count: <input id='count' type='number'> Authors: <input id='authors'></p>" +
- "<p>Price: <input id='price' type='number'> Keywords: <input id='keywords'></p>" +
- "<div id='add-info'></div>";
- //show fields appliable for all docs
- $(object).append(html);
- //while selecting the type of document, new fields appears
- $("#popup-sel").change(function () {
- //clean old fields and apply new ones depending on type
- $("#add-info").empty();
- switch ($("#popup-sel option:selected").attr("name")) {
- case '0':
- 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>" +
- "<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>";
- $("#add-info").append(html);
- break;
- case '1':
- html = "<p>Journal Title: <input id='j-title'> Publication date: <input id='j-pub-date' type='date'> Editors: <input id='j-editors'></p>" +
- "<p>Reference: <input id='reference' type='checkbox'></p>";
- $("#add-info").append(html);
- break;
- }
- });
- //create buttons Add and Cancel
- html = "<p><button id='apply'>Add</button><button id='exit'>Cancel</button>";
- $(object).append(html);
- //forward info to the API related function to add a document
- $("#apply").click(function () {
- if ($("#popup-sel option:selected").html()) {
- var type = $("#popup-sel option:selected").attr("name");
- var title = $("#title").val();
- var count = $("#count").val();
- var authors = $("#authors").val();
- var price = $("#price").val();
- var keywords = $("#keywords").val();
- var bestseller = $("#besteller:checked").val() ? '1' : '0';
- var publisher = $("#publisher").val() || null;
- var edition = $("#edition").val() || null;
- var pub_year = $("#pub-year").val() || null;
- var reference = $("#reference:checked").val() ? '1' : '0';
- var j_title = $("#j-title").val() || null;
- var j_pub_date = $("#j-pub-date").val() || null;
- var j_editors = $("#j-editors").val() || null;
- //API function
- addDocument(type, title, count, authors, price, keywords, bestseller, publisher, edition, pub_year, reference, j_title, j_pub_date, j_editors);
- //clean popup block and hide it
- $(object).empty();
- popupHide();
- //update table to see the results
- updateDocumentsTable($("#content-table"));
- } else {
- alert("Choose a type!");
- }
- return false;
- });
- //events to close the popup
- $("#exit, #hider").click(function () {
- $(object).empty();
- popupHide();
- });
- }
- //functon that displays additional info about the doc
- //parameters are html block for popups and id of the doc
- function popupMoreDocument(object, id) {
- //clean popup block
- $(object).empty();
- //get JSON info about the document through API function
- var doc = JSON.parse(getDocument(id));
- //collect all the information
- var type = doc.type == 0 ? 'Book' : (doc.type == 1 ? 'Article' : 'Media');
- var edition = doc.edition == 1 ? doc.edition + 'st' : (doc.edition == 2 ? doc.edition + 'nd' : (doc.edition == 3 ? doc.edition + 'rd' : doc.edition + 'th'));
- 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)' : '');
- var bestseller = doc.bestseller == 1 ? "<div style='padding: 10px; background: yellow'>BESTSELLER</div> " : "";
- var reference = doc.reference == 1 ? "<div style='padding: 10px; background: red'>REFERENCE</div> " : "";
- //show info about the doc in a formatted way
- var html = "<h2>" + doc.title + "</h2>" +
- "<p><i>by " + doc.authors + "</i></p></br></br>" +
- "<p><b>" + type + "</b> " + descr + "</p>" +
- "</br><p>" + doc.instockCount + " copies left (price: " + doc.price + ")</p>" +
- "<p>Keywords: <i>" + doc.keywords + "</i></p>" +
- bestseller + "</br>" + reference;
- //write into the popup block
- $(object).append(html);
- //event to close the popup window
- $("#hider").click(function () {
- popupHide();
- });
- }
- //shows popup block
- function popupShow() {
- $("#hider, #popup").css("visibility", "visible");
- }
- //hides popup block
- function popupHide() {
- $("#hider, #popup").css("visibility", "hidden");
- }
Add Comment
Please, Sign In to add comment