document.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if (typeof($) != 'object') {
  34. self.Script('jquery');
  35. }
  36. if (typeof(layer) != 'object') {
  37. self.Script('layer/layer');
  38. }
  39. var index = layer.load(0, {shade: false});
  40. $(window).load(function()
  41. {
  42. self.Onload(index);
  43. });
  44. }
  45. ,Script : function(name)
  46. {
  47. var path = location.href.split('document.js');
  48. var script = document.createElement("script");
  49. script.type = "text/javascript";
  50. script.src = path[0] + name + ".js";
  51. document.getElementsByTagName('head')[0].appendChild(script);
  52. }
  53. ,Onload : function(index)
  54. {
  55. var self = this;
  56. self.Option(option);
  57. $.getJSON(self.option.document + '&callback=?', function(t)
  58. {
  59. $(self.option.reader).html(t.msg);
  60. layer.close(index);
  61. self.width = $(self.option.reader).width();
  62. if (self.option.tool) {
  63. self.Tool();
  64. }
  65. self.Border();
  66. self.Scroll();
  67. $('.demeter_document').each(function(i)
  68. {
  69. if (i < self.show) {
  70. self.Load(i);
  71. }
  72. });
  73. });
  74. }
  75. ,Load : function(i)
  76. {
  77. var self = this;
  78. self.page = i;
  79. var doc = $('.demeter_document').eq(i);
  80. doc.hide();
  81. var url = doc.attr('data-page-url');
  82. var parent = doc.parent();
  83. var index = layer.load(0, {shade: false});
  84. $.getJSON(url + '&callback=?', function(t)
  85. {
  86. doc.html(t.msg);
  87. self.LoadDoc(i,index,doc);
  88. });
  89. }
  90. ,LoadDoc : function(i, index, doc)
  91. {
  92. var html = doc.html();
  93. if (doc.find('.w0').length) {
  94. this.SetOffset(i,index,doc);
  95. } else {
  96. this.LoadDoc(i,index,doc);
  97. }
  98. }
  99. ,SetOffset : function(i,index,doc)
  100. {
  101. var self = this;
  102. var e = $('.pf');
  103. var c = e.find('.w0');
  104. var w = c.width();
  105. var w = c.get(0).offsetWidth;
  106. if (w - self.width > 10) {
  107. var z = 100 - ((w - self.width)/self.zoom);
  108. $(self.option.reader).css('zoom', z + '%');
  109. if (self.left) {
  110. c = c.children().eq(0);
  111. e = c.children().eq(0);
  112. if (!e.length) {
  113. e = c;
  114. }
  115. var left = parseInt(e.css('left'));
  116. if (left <= 0) {
  117. self.left = 0;
  118. } else {
  119. var e = c.children().eq(3);
  120. if (e.length) {
  121. var left = parseInt(e.css('left'));
  122. }
  123. }
  124. self.left = left * -1;
  125. $(self.option.reader).css('margin-left', self.left);
  126. }
  127. self.width = w;
  128. }
  129. doc.show();
  130. layer.close(index);
  131. }
  132. ,Option : function(option)
  133. {
  134. this.option = option;
  135. if (!this.option.border) {
  136. this.option.border = 'yes';
  137. }
  138. if (this.option.reader) {
  139. this.reader = this.option.reader;
  140. }
  141. if (this.option.show) {
  142. this.show = this.option.show;
  143. }
  144. if (this.option.left) {
  145. this.left = this.option.left;
  146. }
  147. if (this.option.zoom) {
  148. this.zoom = this.option.zoom;
  149. }
  150. }
  151. ,Border : function() {
  152. if (this.option.border == 'no') {
  153. $(this.option.reader).find('style').append('<style>.pf{border:0px;width:100%;}</style>');
  154. }
  155. }
  156. ,Tool : function()
  157. {
  158. 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>';
  159. $('#page-container').prepend(html);
  160. this.Scoll();
  161. }
  162. ,Scroll : function()
  163. {
  164. var self = this;
  165. var total = $('.demeter_document').length;
  166. if (self.option.more) {
  167. if (self.page > total) {
  168. $(self.option.more).hide();
  169. } else {
  170. $(self.option.more).show();
  171. $(self.option.more).click(function()
  172. {
  173. var p = self.page;
  174. for (var i = 1; i <= self.show; i++) {
  175. var page = p + i;
  176. var state = self.MorePage(total-1, page);
  177. if (state) {
  178. self.Load(page);
  179. }
  180. }
  181. });
  182. self.MorePage(total, self.show);
  183. }
  184. } else {
  185. $(window).scroll(function(){
  186. var scroll = $(window).scrollTop();
  187. var height = self.Height();
  188. if (scroll >= height && self.page < total) {
  189. var page = self.page + 1;
  190. self.Load(page);
  191. }
  192. });
  193. }
  194. }
  195. ,MorePage : function(total, page)
  196. {
  197. var num = total-page;
  198. if (num <= 0) {
  199. $(this.option.more).hide();
  200. return false;
  201. }
  202. $(this.option.more + '_page').html(num);
  203. return true;
  204. }
  205. ,Height : function()
  206. {
  207. var height = 0;
  208. for (var i = 0; i < this.page; i++) {
  209. height += $('.demeter_document').eq(i).height();
  210. }
  211. return height;
  212. }
  213. };