document.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. var callback = function() {
  35. var cb = function() {
  36. var index = layer.load(0, {shade: false});
  37. $(window).load(function()
  38. {
  39. self.Onload(index);
  40. });
  41. }
  42. if (typeof(layer) != 'object') {
  43. self.Script('layer/layer', cb);
  44. } else {
  45. cb();
  46. }
  47. }
  48. if (typeof($) != 'object') {
  49. self.Script('jquery', callback);
  50. } else {
  51. callback();
  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);
  94. layer.close(index);
  95. self.width = $(self.option.reader).width();
  96. if (self.option.tool) {
  97. self.Tool();
  98. }
  99. self.Border();
  100. self.Scroll();
  101. $('.demeter_document').each(function(i)
  102. {
  103. if (i < self.show) {
  104. self.Load(i);
  105. }
  106. });
  107. });
  108. }
  109. ,Load : function(i)
  110. {
  111. var self = this;
  112. self.page = i;
  113. var doc = $('.demeter_document').eq(i);
  114. doc.hide();
  115. var url = doc.attr('data-page-url');
  116. var parent = doc.parent();
  117. var index = layer.load(0, {shade: false});
  118. $.getJSON(url + '&callback=?', function(t)
  119. {
  120. doc.html(t.msg);
  121. self.LoadDoc(i,index,doc);
  122. });
  123. }
  124. ,LoadDoc : function(i, index, doc)
  125. {
  126. var html = doc.html();
  127. if (doc.find('.w0').length) {
  128. this.SetOffset(i,index,doc);
  129. } else {
  130. this.LoadDoc(i,index,doc);
  131. }
  132. }
  133. ,SetOffset : function(i,index,doc)
  134. {
  135. var self = this;
  136. var e = $('.pf');
  137. var c = e.find('.w0');
  138. var w = c.width();
  139. var w = c.get(0).offsetWidth;
  140. if (w - self.width > 10) {
  141. var z = 100 - ((w - self.width)/self.zoom);
  142. $(self.option.reader).css('zoom', z + '%');
  143. if (self.left) {
  144. c = c.children().eq(0);
  145. e = c.children().eq(0);
  146. if (!e.length) {
  147. e = c;
  148. }
  149. var left = parseInt(e.css('left'));
  150. if (left <= 0) {
  151. self.left = 0;
  152. } else {
  153. var e = c.children().eq(3);
  154. if (e.length) {
  155. var left = parseInt(e.css('left'));
  156. }
  157. }
  158. self.left = left * -1;
  159. $(self.option.reader).css('margin-left', self.left);
  160. }
  161. self.width = w;
  162. }
  163. doc.show();
  164. layer.close(index);
  165. }
  166. ,Option : function(option)
  167. {
  168. this.option = option;
  169. if (!this.option.border) {
  170. this.option.border = 'yes';
  171. }
  172. if (this.option.reader) {
  173. this.reader = this.option.reader;
  174. }
  175. if (this.option.show) {
  176. this.show = this.option.show;
  177. }
  178. if (this.option.left) {
  179. this.left = this.option.left;
  180. }
  181. if (this.option.zoom) {
  182. this.zoom = this.option.zoom;
  183. }
  184. }
  185. ,Border : function() {
  186. if (this.option.border == 'no') {
  187. $(this.option.reader).find('style').append('<style>.pf{border:0px;width:100%;}</style>');
  188. }
  189. }
  190. ,Tool : function()
  191. {
  192. 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>';
  193. $('#page-container').prepend(html);
  194. this.Scoll();
  195. }
  196. ,Scroll : function()
  197. {
  198. var self = this;
  199. var total = $('.demeter_document').length;
  200. if (self.option.more) {
  201. if (self.page > total) {
  202. $(self.option.more).hide();
  203. } else {
  204. $(self.option.more).show();
  205. $(self.option.more).click(function()
  206. {
  207. var p = self.page;
  208. for (var i = 1; i <= self.show; i++) {
  209. var page = p + i;
  210. var state = self.MorePage(total-1, page);
  211. if (state) {
  212. self.Load(page);
  213. }
  214. }
  215. });
  216. self.MorePage(total, self.show);
  217. }
  218. } else {
  219. $(window).scroll(function(){
  220. var scroll = $(window).scrollTop();
  221. var height = self.Height();
  222. if (scroll >= height && self.page < total) {
  223. var page = self.page + 1;
  224. self.Load(page);
  225. }
  226. });
  227. }
  228. }
  229. ,MorePage : function(total, page)
  230. {
  231. var num = total-page;
  232. if (num <= 0) {
  233. $(this.option.more).hide();
  234. return false;
  235. }
  236. $(this.option.more + '_page').html(num);
  237. return true;
  238. }
  239. ,Height : function()
  240. {
  241. var height = 0;
  242. for (var i = 0; i < this.page; i++) {
  243. height += $('.demeter_document').eq(i).height();
  244. }
  245. return height;
  246. }
  247. };