document.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. left : 0,
  29. zoom : 11,
  30. Init : function(option)
  31. {
  32. var self = this;
  33. self.Option(option);
  34. $.getJSON(self.option.document + '&callback=?', function(t)
  35. {
  36. $(self.option.reader).html(t.msg);
  37. self.width = $(self.option.reader).width();
  38. if (self.option.tool) {
  39. self.Tool();
  40. }
  41. self.Border();
  42. self.Scroll();
  43. $('.pf').each(function(i)
  44. {
  45. if (i < self.show) {
  46. self.Load(i);
  47. }
  48. });
  49. });
  50. }
  51. ,Load : function(i)
  52. {
  53. var self = this;
  54. self.page = i;
  55. var doc = $('.pf').eq(i);
  56. var url = doc.attr('data-page-url');
  57. var parent = doc.parent();
  58. $.getJSON(url + '&callback=?', function(t)
  59. {
  60. doc.prop('outerHTML', t.msg).show();
  61. var e = $('.pf').eq(i);
  62. var c = e.find('.w0');
  63. var w = c.width();
  64. var w = c.get(0).offsetWidth;
  65. if (w - self.width > 10) {
  66. var z = 100 - ((w - self.width)/self.zoom);
  67. $(self.option.reader).css('zoom', z + '%');
  68. if (self.left) {
  69. c = c.children().eq(0);
  70. e = c.children().eq(0);
  71. if (!e.length) {
  72. e = c;
  73. }
  74. var left = parseInt(e.css('left'));
  75. if (left <= 0) {
  76. self.left = 0;
  77. } else {
  78. var e = c.children().eq(3);
  79. if (e.length) {
  80. var left = parseInt(e.css('left'));
  81. }
  82. }
  83. self.left = left * -1;
  84. $(self.option.reader).css('margin-left', self.left);
  85. }
  86. self.width = w;
  87. }
  88. });
  89. }
  90. ,Option : function(option)
  91. {
  92. this.option = option;
  93. if (!this.option.border) {
  94. this.option.border = 'yes';
  95. }
  96. if (this.option.reader) {
  97. this.reader = this.option.reader;
  98. }
  99. if (this.option.show) {
  100. this.show = this.option.show;
  101. }
  102. if (this.option.left) {
  103. this.left = this.option.left;
  104. }
  105. if (this.option.zoom) {
  106. this.zoom = this.option.zoom;
  107. }
  108. }
  109. ,Border : function() {
  110. if (this.option.border == 'no') {
  111. $(this.option.reader).find('style').append('<style>.pf{border:0px;width:100%;}</style>');
  112. }
  113. }
  114. ,Tool : function()
  115. {
  116. 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>';
  117. $('#page-container').prepend(html);
  118. this.Scoll();
  119. }
  120. ,Scroll : function()
  121. {
  122. var self = this;
  123. var total = $('.pf').length;
  124. if (self.option.more) {
  125. if (self.page > total) {
  126. $(self.option.more).hide();
  127. } else {
  128. $(self.option.more).show();
  129. $(self.option.more).click(function()
  130. {
  131. var p = self.page;
  132. for (var i = 1; i <= self.show; i++) {
  133. var page = p + i;
  134. var state = self.MorePage(total-1, page);
  135. if (state) {
  136. self.Load(page);
  137. }
  138. }
  139. });
  140. self.MorePage(total, self.show);
  141. }
  142. } else {
  143. $(window).scroll(function(){
  144. var scroll = $(window).scrollTop();
  145. var height = self.Height();
  146. if (scroll >= height && self.page < total) {
  147. var page = self.page + 1;
  148. self.Load(page);
  149. }
  150. });
  151. }
  152. }
  153. ,MorePage : function(total, page)
  154. {
  155. var num = total-page;
  156. if (num <= 0) {
  157. $(this.option.more).hide();
  158. return false;
  159. }
  160. $(this.option.more + '_page').html(num);
  161. return true;
  162. }
  163. ,Height : function()
  164. {
  165. var height = 0;
  166. for (var i = 0; i < this.page; i++) {
  167. height += $('.pf').eq(i).height();
  168. }
  169. return height;
  170. }
  171. };