123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- /* ================================================================================
- * document.js v1.0
- * http://git.shemic.com/dever/script
- * ================================================================================
- * Copyright 2017-2018 Dever(dever.cc)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ================================================================================
- */
- var Document =
- {
- option : {},
- reader : '#document',
- show : 1,
- page : 0,
- width : 0,
- left : 0,
- zoom : 18,
- path : '',
- loadNum : 0,
- Init : function(option)
- {
- var self = this;
- this.loadNum = 0;
- $(window).load(function()
- {
- var callback = function() {
- var cb = function() {
- var index = layer.load(0, {shade: false});
- self.Onload(index);
- }
- if (typeof(layer) != 'object') {
- self.Script('layer/layer', cb);
- } else {
- cb();
- }
- }
- if (typeof($) != 'object') {
- self.Script('jquery', callback);
- } else {
- callback();
- }
- });
- }
- ,Script : function(name, callback)
- {
- var self = this;
- if (!self.path) {
- $('script').each(function()
- {
- if ($(this).attr('src') && $(this).attr('src').indexOf('document.js') != -1) {
- path = $(this).attr('src').split('document.js');
- self.path = path[0];
- }
- });
- }
- var script = document.createElement("script");
- script.type = "text/javascript";
- script.async = 'async';
- script.src = self.path + name + ".js";
- document.getElementsByTagName('head')[0].appendChild(script);
- if (script.readyState) {
- script.onreadystatechange=function() {
- if (script.readyState == 'complete'|| script.readyState == 'loaded') {
- script.onreadystatechange = null;
- callback();
- }
- }
- } else {
- script.onload = function(){callback();}
- }
- }
- ,Css : function(name, callback)
- {
- var self = this;
- var link = document.createElement("link");
- link.rel = 'stylesheet';
- link.type = 'text/css';
- link.href = name;
- document.getElementsByTagName('head')[0].appendChild(link);
- if (link.readyState) {
- link.onreadystatechange=function() {
- if (link.readyState == 'complete' || link.readyState == 'loaded') {
- link.onreadystatechange = null;
- callback();
- }
- }
- } else {
- link.onload = function(){callback();}
- }
- }
- ,Sleep : function(n)
- {
- var start = new Date().getTime();
- while (true) if (new Date().getTime() - start > n) break;
- }
- ,Onload : function(index)
- {
- var self = this;
- self.Option(option);
-
- $.getJSON(self.option.document + '&callback=?', function(t)
- {
- $(self.option.reader).html(t.msg);
- var css = $('#page-container').attr('load-css');
- var callback = function()
- {
- layer.close(index);
- self.width = $(self.option.reader).width();
- if (self.option.tool) {
- self.Tool();
- }
- self.Border();
- self.Scroll();
- $('.demeter_document').each(function(i)
- {
- if (i < self.show) {
- self.Load(i);
- }
- });
- };
- callback();
- //self.Css(css, callback);
- });
- }
- ,Load : function(i)
- {
- var self = this;
- self.page = i;
- var doc = $('.demeter_document').eq(i);
- doc.hide();
- var url = doc.attr('data-page-url');
- var parent = doc.parent();
- var index = layer.load(0, {shade: false});
- $.getJSON(url + '&callback=?', function(t)
- {
- doc.html(t.msg);
- self.LoadDoc(i,index,doc);
- });
- }
- ,LoadDoc : function(i, index, doc)
- {
- var html = doc.html();
- if (doc.find('.pc').length) {
- this.SetOffset(i,index,doc);
- } else {
- if (this.loadNum > 5) {
- this.loadNum = 1;
- layer.close(index);
- } else {
- this.loadNum = this.loadNum + 1;
- this.LoadDoc(i,index,doc);
- }
- }
- }
- ,SetOffset : function(i,index,doc)
- {
- var self = this;
- var e = $('.pf');
- var c = e.find('.pc');
- var w = c.width();
- var b = w/self.width;
- if (self.left) {
- c = c.children().eq(0);
- e = c.children().eq(0);
- if (!e.length) {
- e = c;
- }
- var left = parseInt(e.css('left'));
- if (left <= 0) {
- self.left = 0;
- } else {
- var e = c.children().eq(3);
- if (e.length) {
- var left = parseInt(e.css('left'));
- }
- }
-
- self.left = left * -1 + 10;
- self.zoom = 2;
- if (self.left > 20) {
- $(self.option.reader).css('margin-left', self.left);
- self.zoom = 18;
- }
- if (w - self.width > 10) {
- var z = 100/b + self.zoom;
- $(self.option.reader).css('zoom', z + '%');
- }
-
- self.width = w;
- } else {
- var z = 100/b;
- $(self.option.reader).css('zoom', z + '%');
- $('.w2').css('width', '100%');
- }
- doc.show();
- layer.close(index);
- }
- ,Option : function(option)
- {
- this.option = option;
- if (!this.option.border) {
- this.option.border = 'yes';
- }
- if (this.option.reader) {
- this.reader = this.option.reader;
- }
- if (this.option.show) {
- this.show = this.option.show;
- }
- if (this.option.left) {
- this.left = this.option.left;
- }
- if (this.option.zoom) {
- this.zoom = this.option.zoom;
- }
- }
- ,Border : function() {
- if (this.option.border == 'no') {
- $(this.option.reader).find('style').append('<style>.pf{border:0px;width:100%;}</style>');
- }
- }
- ,Tool : function()
- {
- 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;"> / 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>';
- $('#page-container').prepend(html);
- this.Scoll();
- }
- ,Scroll : function()
- {
- var self = this;
- var total = $('.demeter_document').length;
- if (self.option.more) {
- if (self.page > total) {
- $(self.option.more).hide();
- } else {
- $(self.option.more).show();
- $(self.option.more).click(function()
- {
- var p = self.page;
- for (var i = 1; i <= self.show; i++) {
- var page = p + i;
- var state = self.MorePage(total-1, page);
- if (state) {
- self.Load(page);
- }
- }
- });
- self.MorePage(total, self.show);
- }
- } else {
- $(window).scroll(function(){
- var scroll = $(window).scrollTop();
- var height = self.Height();
- if (scroll >= height && self.page < total) {
- var page = self.page + 1;
- self.Load(page);
- }
- });
- }
- }
- ,MorePage : function(total, page)
- {
- var num = total-page;
- if (num <= 0) {
- $(this.option.more).hide();
- return false;
- }
- $(this.option.more + '_page').html(num);
- return true;
- }
- ,Height : function()
- {
- var height = 0;
- for (var i = 0; i < this.page; i++) {
- height += $('.demeter_document').eq(i).height();
- }
- return height;
- }
- };
|