document.js 8.5 KB

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