document.js 9.6 KB

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