document.js 7.5 KB

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