/* ================================================================================ * 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(''); } } ,Tool : function() { var html = '
'; $('#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; } };