home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2964 < prev    next >
Encoding:
Text File  |  2010-05-30  |  10.2 KB  |  305 lines

  1. /* COLUMNS */
  2. var cmap = ['title', 'authors', 'rating', 'date', 'series'];
  3. /* COLUMNS END */
  4. var column_titles = {
  5.     'title'    : 'Title',
  6.     'authors'  : 'Author(s)',
  7.     'rating'   : 'Rating',
  8.     'date'     : 'Date',
  9.     'tags'     : 'Tags',
  10.     'series'   : 'Series'
  11. };
  12.  
  13. String.prototype.format = function() {
  14.     var pattern = /\{\d+\}/g;
  15.     var args = arguments;
  16.     return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; });
  17. }
  18.  
  19. var last_search = '';
  20. var last_sort = null;
  21. var last_sort_order = null;
  22. var last_start = 0;
  23. var last_num = 20;
  24. var total = 0;
  25. var current_library_request = null;
  26.  
  27. ////////////////////////////// GET BOOK LIST //////////////////////////////
  28.  
  29. var LIBRARY_FETCH_TIMEOUT = 10000; // milliseconds
  30.  
  31. function create_table_headers() {
  32.     var thead = $('table#book_list thead tr');
  33.     var titles = '';
  34.     for (i = 0; i < cmap.length; i++) {
  35.         titles += '<td>{0} <span class="sort_indicator" id="{1}_sort">Γåæ</span></td>'
  36.             .format(column_titles[cmap[i]], cmap[i]);
  37.     }
  38.     thead.html(titles);
  39. }
  40.  
  41.  
  42. function format_url(format, id, title) {
  43.     return 'get/'+format.toLowerCase() + '/'+encodeURIComponent(title) + '_' + id+'.'+format.toLowerCase();
  44. }
  45.  
  46. function render_book(book) {
  47.     // Render title cell
  48.     var title = '<i>{0}</i>'.format(book.attr("title")) + '<br /><span class="subtitle">';
  49.     var id    = book.attr("id");
  50.     var comments = $.trim(book.text()).replace(/\n\n/, '<br/>');
  51.     var formats = new Array();
  52.     var size = (parseFloat(book.attr('size'))/(1024*1024)).toFixed(1);
  53.     var tags = book.attr('tags').replace(/,/g, ', ');
  54.     formats = book.attr("formats").split(",");
  55.     if (formats.length > 0) {
  56.         for (i=0; i < formats.length; i++) {
  57.             title += '<a title="Download in '+formats[i]+' format" class="format" href="'+format_url(formats[i], id, book.attr("title"))+'">'+formats[i]+'</a>, ';
  58.         }
  59.         title = title.slice(0, title.length-2);
  60.         title += ' ({0} MB) '.format(size);
  61.     }
  62.     if (tags) title += '[{0}]'.format(tags);
  63.     title += '<img style="display:none" alt="" src="get/cover/{0}" /></span>'.format(id);
  64.     title += '<div class="comments">{0}</div>'.format(comments)
  65.     // Render authors cell
  66.     var _authors = new Array();
  67.     var authors = '';
  68.     _authors = book.attr('authors').split('|');
  69.     for (i = 0; i < _authors.length; i++) {
  70.         authors += jQuery.trim(_authors[i]).replace(/ /g, ' ')+'<br />';
  71.     }
  72.     if (authors) { authors = authors.slice(0, authors.length-6); }
  73.  
  74.     // Render rating cell
  75.     var _rating = parseFloat(book.attr('rating'))/2.;
  76.     var rating = '';
  77.     for (i = 0; i < _rating; i++) { rating += '★'}
  78.  
  79.     // Render date cell
  80.     var _date = Date.parseExact(book.attr('timestamp'), 'yyyy/MM/dd HH:mm:ss');
  81.     var date = _date.toString('d MMM yyyy').replace(/ /g, ' ');
  82.  
  83.     // Render series cell
  84.     var series = book.attr("series")
  85.     if (series) {
  86.         series += ' [{0}]'.format(book.attr('series_index'));
  87.     }
  88.  
  89.     var cells = {
  90.         'title'   : title,
  91.         'authors' : authors,
  92.         'rating'  : rating,
  93.         'date'    : date,
  94.         'series'  : series
  95.     };
  96.  
  97.     var row = '';
  98.     for (i = 0; i < cmap.length; i++) {
  99.         row += '<td class="{0}">{1}</td>'.format(cmap[i], cells[cmap[i]]);
  100.     }
  101.     return '<tr id="{0}">{1}</tr>'.format(id, row);
  102. }
  103.  
  104. function fetch_library_books(start, num, timeout, sort, order, search) {
  105.     // null, 0, false are False
  106.     data = {"start":start+'', "num":num+''};
  107.     if (sort)   { data["sort"] = sort; }
  108.     if (search) { data["search"] = search; }
  109.     if (order)  { data['order'] = order; }
  110.     last_num = num;
  111.     last_start = start;
  112.     last_search = search;
  113.     last_sort = sort;
  114.     last_sort_order = order;
  115.  
  116.     if (current_library_request != null) {
  117.         current_library_request.abort();
  118.         current_library_request = null;
  119.     }
  120.  
  121.     $('#cover_pane').css('visibility', 'hidden');
  122.     $('#loading').css('visibility', 'visible');
  123.  
  124.     current_library_request = $.ajax({
  125.       type: "GET",
  126.       url: "xml",
  127.       data: data,
  128.       cache: false,
  129.       timeout: timeout, //milliseconds
  130.       dataType: "xml",
  131.  
  132.       error : function(XMLHttpRequest, textStatus, errorThrown) {
  133.           alert('Error: '+textStatus+'\n\n'+errorThrown);
  134.       },
  135.  
  136.       success : function(xml, textStatus) {
  137.           var library = $(xml).find('library');
  138.           total = parseInt(library.attr('total'));
  139.           var num   = parseInt(library.attr('num'));
  140.           var start = parseInt(library.attr('start'));
  141.           update_count_bar(start, num, total);
  142.           var display = '';
  143.           library.find('book').each( function() {
  144.               var book = $(this);
  145.               var row = render_book(book);
  146.               display += row+'\n\n';
  147.           });
  148.           $("#book_list tbody").html(display);
  149.           $("#book_list tbody tr").bind('mouseenter', function() {
  150.               var row = $(this);
  151.               $('#book_list tbody tr:even').css('background-color', '#eeeeee');
  152.               $('#book_list tbody tr:odd').css('background-color', 'white');
  153.  
  154.               row.css('background-color', "#fff2a8");
  155.               row.bind('mouseleave', function(){
  156.                   row.css('background-color', "white");
  157.                   $('#book_list tbody tr:even').css('background-color', '#eeeeee');
  158.                   row.unbind('mouseleave');
  159.               });
  160.           });
  161.           $("#book_list tbody tr").click(function(){
  162.               var row = $(this);
  163.               var cover = row.find('img').attr('src');
  164.               var collapsed = row.find('.comments').css('display') == 'none';
  165.               $("#book_list tbody tr * .comments").css('display', 'none');
  166.               $('#cover_pane').css('visibility', 'hidden');
  167.               if (collapsed) {
  168.                   row.find('.comments').css('display', 'inherit');
  169.                   $('#cover_pane img').attr('src', cover);
  170.                   $('#cover_pane').css('visibility', 'visible');
  171.               }
  172.           });
  173.  
  174.  
  175.           layout();
  176.           $('#book_list tbody tr:even').css('background-color', '#eeeeee');
  177.       },
  178.  
  179.       complete : function(XMLHttpRequest, textStatus) {
  180.           current_library_request = null;
  181.           document.getElementById('main').scrollTop = 0;
  182.           $('#loading').css('visibility', 'hidden');
  183.       }
  184.  
  185.     });
  186.  
  187. }
  188.  
  189.  
  190. ////////////////////////////// COUNT BAR //////////////////////////////
  191.  
  192. function update_count_bar(start, num, total) {
  193.     var cb = $('#count_bar');
  194.     cb.find('#count').html('Books {0} to {1} of {2}'.format(start+1, start+num, total));
  195.     var left = cb.find('#left');
  196.     left.css('opacity', (start <= 0) ? 0.3 : 1);
  197.     var right = cb.find('#right');
  198.     right.css('opacity', (start + num >= total) ? 0.3 : 1);
  199.  
  200. }
  201.  
  202. function setup_count_bar() {
  203.     $('#count_bar * img:eq(0)').click(function(){
  204.         if (last_start > 0) {
  205.             fetch_library_books(0, last_num, LIBRARY_FETCH_TIMEOUT, last_sort, last_sort_order, last_search);
  206.         }
  207.     });
  208.  
  209.     $('#count_bar * img:eq(1)').click(function(){
  210.         if (last_start > 0) {
  211.             var new_start = last_start - last_num;
  212.             if (new_start < 0) {
  213.                 new_start = 0;
  214.             }
  215.             fetch_library_books(new_start, last_num, LIBRARY_FETCH_TIMEOUT, last_sort, last_sort_order, last_search);
  216.         }
  217.     });
  218.  
  219.     $('#count_bar * img:eq(2)').click(function(){
  220.         if (last_start + last_num < total) {
  221.             var new_start = last_start + last_num;
  222.             fetch_library_books(new_start, last_num, LIBRARY_FETCH_TIMEOUT, last_sort, last_sort_order, last_search);
  223.         }
  224.     });
  225.  
  226.     $('#count_bar * img:eq(3)').click(function(){
  227.         if (total - last_num > 0) {
  228.             fetch_library_books(total - last_num, last_num, LIBRARY_FETCH_TIMEOUT, last_sort, last_sort_order, last_search);
  229.         }
  230.     });
  231. }
  232.  
  233. ////////////////////////////// SEARCH /////////////////////////////////////////
  234.  
  235. function search() {
  236.     var search = $.trim($('#search_box * #s').val());
  237.     fetch_library_books(0, last_num, LIBRARY_FETCH_TIMEOUT,
  238.                         last_sort, last_sort_order, search);
  239. }
  240.  
  241.  
  242. /////////////////////////// SORTING /////////////////////////////////////
  243.  
  244. function setup_sorting() {
  245.     $('table#book_list  thead tr td').mouseover(function() {
  246.         this.style.backgroundColor = "#fff2a8";
  247.     });
  248.  
  249.     $('table#book_list  thead tr td').mouseout(function() {
  250.         this.style.backgroundColor = "transparent";
  251.     });
  252.  
  253.     for (i = 0; i < cmap.length; i++) {
  254.         $('table#book_list span#{0}_sort'.format(cmap[i])).parent().click(function() {
  255.             var sort_indicator = $($(this).find('span'));
  256.             var cell = $(sort_indicator.parent());
  257.             var id = sort_indicator.attr("id");
  258.             var col = id.slice(0, id.indexOf("_"));
  259.             var order = 'ascending';
  260.             var html = 'Γåæ';
  261.  
  262.             if (sort_indicator.html() == 'Γåæ') {
  263.                 order = 'descending'; html = 'Γåô';
  264.             }
  265.  
  266.             sort_indicator.html(html);
  267.             $('#book_list * .sort_indicator').css('visibility', 'hidden');
  268.             sort_indicator.css('visibility', 'visible');
  269.             fetch_library_books(last_start, last_num, LIBRARY_FETCH_TIMEOUT, col, order, last_search);
  270.         });
  271.     }
  272. }
  273.  
  274. ///////////////////////// STARTUP ////////////////////////////////////////
  275.  
  276. function layout() {
  277.     var main = $('#main'); var cb = $('#count_bar');
  278.     main.css('height', ($(window).height() - main.offset().top - 20)+'px')
  279.     main.css('width', ($(window).width() - main.offset().left - 15)+'px')
  280.     cb.css('right', '20px');
  281.     cb.css('top', (main.offset().top - cb.height()-5)+'px');
  282.     $('#loading').css('height', ($(window).height()-20)+'px');
  283.     $('#loading').css('width', ($(window).width()-20)+'px');
  284.     var cover = $('#cover_pane');
  285.     var title = $('#book_list thead tr td')
  286.     cover.css('width', (main.width()-title.offset().left - title.width()-15)+'px')
  287.     cover.css('height', main.height()+'px')
  288.     cover.css('left', (title.offset().left+title.width())+'px');
  289.     cover.css('top', main.offset().top+'px');
  290. }
  291.  
  292. $(function() {
  293.     // document is ready
  294.     create_table_headers();
  295.  
  296.     // Setup widgets
  297.     setup_sorting();
  298.     setup_count_bar();
  299.     $('#search_box * #s').val('');
  300.     $(window).resize(layout);
  301.  
  302.     $($('#book_list * span#date_sort').parent()).click();
  303.  
  304. });
  305.