document.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 : 18,
  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. ,Css : function(name, callback)
  83. {
  84. var self = this;
  85. var link = document.createElement("link");
  86. link.rel = 'stylesheet';
  87. link.type = 'text/css';
  88. link.href = name;
  89. document.getElementsByTagName('head')[0].appendChild(link);
  90. if (link.readyState) {
  91. link.onreadystatechange=function() {
  92. if (link.readyState == 'complete' || link.readyState == 'loaded') {
  93. link.onreadystatechange = null;
  94. callback();
  95. }
  96. }
  97. } else {
  98. link.onload = function(){callback();}
  99. }
  100. }
  101. ,Sleep : function(n)
  102. {
  103. var start = new Date().getTime();
  104. while (true) if (new Date().getTime() - start > n) break;
  105. }
  106. ,Onload : function(index)
  107. {
  108. var self = this;
  109. self.Option(option);
  110. $.getJSON(self.option.document + '&callback=?', function(t)
  111. {
  112. $(self.option.reader).html(t.msg);
  113. var css = $('#page-container').attr('load-css');
  114. var callback = function()
  115. {
  116. layer.close(index);
  117. self.width = $(self.option.reader).width();
  118. if (self.option.tool) {
  119. self.Tool();
  120. }
  121. self.Border();
  122. self.Scroll();
  123. $('.demeter_document').each(function(i)
  124. {
  125. if (i < self.show) {
  126. self.Load(i);
  127. }
  128. });
  129. };
  130. callback();
  131. //self.Css(css, callback);
  132. });
  133. }
  134. ,Load : function(i)
  135. {
  136. var self = this;
  137. self.page = i;
  138. var doc = $('.demeter_document').eq(i);
  139. doc.hide();
  140. var url = doc.attr('data-page-url');
  141. var parent = doc.parent();
  142. var index = layer.load(0, {shade: false});
  143. $.getJSON(url + '&callback=?', function(t)
  144. {
  145. doc.html(t.msg);
  146. self.LoadDoc(i,index,doc);
  147. });
  148. }
  149. ,LoadDoc : function(i, index, doc)
  150. {
  151. var html = doc.html();
  152. if (doc.find('.w0').length) {
  153. this.SetOffset(i,index,doc);
  154. } else {
  155. this.LoadDoc(i,index,doc);
  156. }
  157. }
  158. ,SetOffset : function(i,index,doc)
  159. {
  160. var self = this;
  161. var e = $('.pf');
  162. var c = e.find('.w0');
  163. var w = c.width();
  164. var b = w/self.width;
  165. if (self.left) {
  166. c = c.children().eq(0);
  167. e = c.children().eq(0);
  168. if (!e.length) {
  169. e = c;
  170. }
  171. var left = parseInt(e.css('left'));
  172. if (left <= 0) {
  173. self.left = 0;
  174. } else {
  175. var e = c.children().eq(3);
  176. if (e.length) {
  177. var left = parseInt(e.css('left'));
  178. }
  179. }
  180. self.left = left * -1 + 10;
  181. self.zoom = 0;
  182. if (self.left) {
  183. $(self.option.reader).css('margin-left', self.left);
  184. self.zoom = 20;
  185. }
  186. if (w - self.width > 10) {
  187. var z = 100/b + self.zoom;
  188. $(self.option.reader).css('zoom', z + '%');
  189. }
  190. self.width = w;
  191. } else {
  192. var z = 100/b;
  193. $(self.option.reader).css('zoom', z + '%');
  194. }
  195. doc.show();
  196. layer.close(index);
  197. }
  198. ,Option : function(option)
  199. {
  200. this.option = option;
  201. if (!this.option.border) {
  202. this.option.border = 'yes';
  203. }
  204. if (this.option.reader) {
  205. this.reader = this.option.reader;
  206. }
  207. if (this.option.show) {
  208. this.show = this.option.show;
  209. }
  210. if (this.option.left) {
  211. this.left = this.option.left;
  212. }
  213. if (this.option.zoom) {
  214. this.zoom = this.option.zoom;
  215. }
  216. }
  217. ,Border : function() {
  218. if (this.option.border == 'no') {
  219. $(this.option.reader).find('style').append('<style>.pf{border:0px;width:100%;}</style>');
  220. }
  221. }
  222. ,Tool : function()
  223. {
  224. 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>';
  225. $('#page-container').prepend(html);
  226. this.Scoll();
  227. }
  228. ,Scroll : function()
  229. {
  230. var self = this;
  231. var total = $('.demeter_document').length;
  232. if (self.option.more) {
  233. if (self.page > total) {
  234. $(self.option.more).hide();
  235. } else {
  236. $(self.option.more).show();
  237. $(self.option.more).click(function()
  238. {
  239. var p = self.page;
  240. for (var i = 1; i <= self.show; i++) {
  241. var page = p + i;
  242. var state = self.MorePage(total-1, page);
  243. if (state) {
  244. self.Load(page);
  245. }
  246. }
  247. });
  248. self.MorePage(total, self.show);
  249. }
  250. } else {
  251. $(window).scroll(function(){
  252. var scroll = $(window).scrollTop();
  253. var height = self.Height();
  254. if (scroll >= height && self.page < total) {
  255. var page = self.page + 1;
  256. self.Load(page);
  257. }
  258. });
  259. }
  260. }
  261. ,MorePage : function(total, page)
  262. {
  263. var num = total-page;
  264. if (num <= 0) {
  265. $(this.option.more).hide();
  266. return false;
  267. }
  268. $(this.option.more + '_page').html(num);
  269. return true;
  270. }
  271. ,Height : function()
  272. {
  273. var height = 0;
  274. for (var i = 0; i < this.page; i++) {
  275. height += $('.demeter_document').eq(i).height();
  276. }
  277. return height;
  278. }
  279. };