document.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* ================================================================================
  2. * document.js v1.0
  3. * http://git.shemic.com/dever/script
  4. * ================================================================================
  5. * Copyright 2017-2018 Dever(dever.cc)
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ================================================================================
  19. */
  20. //文档工具包
  21. var Document =
  22. {
  23. option : {},
  24. reader : '#document',
  25. show : 1,
  26. page : 0,
  27. width : 0,
  28. Init : function(option)
  29. {
  30. var self = this;
  31. self.Option(option);
  32. $.getJSON(self.option.document + '&callback=?', function(t)
  33. {
  34. $(self.option.reader).html(t.msg);
  35. self.width = $(self.option.reader).width();
  36. if (self.option.tool) {
  37. self.Tool();
  38. }
  39. self.Border();
  40. self.Scroll();
  41. $('.pf').each(function(i)
  42. {
  43. if (i < self.show) {
  44. self.Load(i);
  45. }
  46. });
  47. });
  48. }
  49. ,Load : function(i)
  50. {
  51. var self = this;
  52. self.page = i;
  53. var doc = $('.pf').eq(i);
  54. var url = doc.attr('data-page-url');
  55. var parent = doc.parent();
  56. $.getJSON(url + '&callback=?', function(t)
  57. {
  58. doc.prop('outerHTML', t.msg).show();
  59. var e = $('.pf').eq(i);
  60. var w = e.find('.w0').width();
  61. if (w > self.width) {
  62. var z = 100 - ((w - self.width)/10);
  63. $(self.option.reader).css('zoom', z + '%');
  64. self.width = w;
  65. }
  66. });
  67. }
  68. ,Option : function(option)
  69. {
  70. this.option = option;
  71. if (!this.option.border) {
  72. this.option.border = 'yes';
  73. }
  74. if (this.option.reader) {
  75. this.reader = this.option.reader;
  76. }
  77. if (this.option.show) {
  78. this.show = this.option.show;
  79. }
  80. }
  81. ,Border : function() {
  82. if (this.option.border == 'no') {
  83. $(this.option.reader).find('style').append('<style>.pf{border:0px;width:100%;}</style>');
  84. }
  85. }
  86. ,Tool : function()
  87. {
  88. var html = '<div class="document-toolsbar-mod" style="visibility: visible;width: 98.2%; position: static;margin-top: 10px;margin-left: 8px;margin-bottom: -14px;"><div class="document-toolsbar-inner"><div class="document-toolsbar-content"><div class="document-toolsbar-nav" style="margin-left: 230px;"><a class="document-toolsbar-top" href="###" style="visibility: visible;"><b>top</b></a><input class="document-toolsbar-input" type="text" value="" style="visibility: visible;"><span class="document-toolsbar-pageCount" style="visibility: visible;">&nbsp;/&nbsp;3</span><a class="document-toolsbar-bottom" href="###" style="visibility: visible;"><b>bottom</b></a></div><div class="document-toolsbar-zoom"><a class="document-toolsbar-zoomin" href="###" style="visibility: visible;">zoom in</a><a class="document-toolsbar-zoomout" href="###" style="visibility: visible;">zoom out</a></div></div></div></div></div>';
  89. $('#page-container').prepend(html);
  90. this.Scoll();
  91. }
  92. ,Scroll : function()
  93. {
  94. var self = this;
  95. var total = $('.pf').length;
  96. if (self.option.more) {
  97. if (self.page > total) {
  98. $(self.option.more).hide();
  99. } else {
  100. $(self.option.more).show();
  101. $(self.option.more).click(function()
  102. {
  103. var p = self.page;
  104. for (var i = 1; i <= self.show; i++) {
  105. var page = p + i;
  106. self.MorePage(total-1, page);
  107. self.Load(page);
  108. }
  109. });
  110. self.MorePage(total, self.show);
  111. }
  112. } else {
  113. $(window).scroll(function(){
  114. var scroll = $(window).scrollTop();
  115. var height = self.Height();
  116. if (scroll >= height && self.page < total) {
  117. var page = self.page + 1;
  118. self.Load(page);
  119. }
  120. });
  121. }
  122. }
  123. ,MorePage : function(total, page)
  124. {
  125. var num = total-page;
  126. if (num <= 0) {
  127. $(this.option.more).hide();
  128. }
  129. $(this.option.more + '_page').html(num);
  130. }
  131. ,Height : function()
  132. {
  133. var height = 0;
  134. for (var i = 0; i < this.page; i++) {
  135. height += $('.pf').eq(i).height();
  136. }
  137. return height;
  138. }
  139. };