From a0ab7c363927924e214e55ece6048e0f0e16cb43 Mon Sep 17 00:00:00 2001 From: suVene Date: Sat, 13 Dec 2008 15:51:22 +0000 Subject: * nextlink.js - 1.2対応 * _libly.js - add $U.xmlToDom(xml, doc, nodes) git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@26642 d0d07461-0603-4401-acd4-de1884942a52 --- nextlink.js | 131 +++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 94 insertions(+), 37 deletions(-) (limited to 'nextlink.js') diff --git a/nextlink.js b/nextlink.js index 2de72d7..e5627e3 100755 --- a/nextlink.js +++ b/nextlink.js @@ -5,12 +5,12 @@ var PLUGIN_INFO = mapping "[[", "]]" by AutoPagerize XPath. AutoPagerize 用の XPath より "[[", "]]" をマッピングします。 suVene - 0.2.0 - 2.0 + 0.2.1 + 1.2 2.0pre ; //}}} liberator.plugins.nextlink = (function() { + // initialize //{{{ if (!liberator.plugins.libly) { liberator.log('nextlink: needs _libly.js'); return; @@ -43,12 +44,13 @@ liberator.plugins.nextlink = (function() { var pageNaviCss = ; + //}}} var NextLink = function() {//{{{ this.initialize.apply(this, arguments); @@ -73,6 +76,8 @@ liberator.plugins.nextlink = (function() { this.siteinfo = []; this.cache = {}; // {url: {xpath: xpath, next: element, prev: url}} or null this.pager = pager; + this.browserModes = config.browserModes || [modes.NORMAL, modes.VISUAL]; + this.isNew = autocommands['DOMLoad'] ? false : true; // toriaezu var req = new libly.Request(this.WEDATA_AUTOPAGERIZE); req.addEventListener('onSuccess', $U.bind(this, @@ -85,13 +90,21 @@ liberator.plugins.nextlink = (function() { } )); req.get(); - /* // for debug + // for debug + /* this.initialized = true; - this.siteinfo = [{ - url: '^http:\\/\\/192\\.168\\.', - nextLink: 'id("next")', - pageElement: '//*' - }]; + this.siteinfo = [ + { + url: '^http:\\/\\/192\\.168\\.', + nextLink: 'id("next")', + pageElement: '//*' + }, + { + url: '^http:\\/\\/localhost', + nextLink: 'id("next")', + pageElement: '//*' + } + ]; */ commands.addUserCommand(['nextlink'], 'map ]] by AutoPagerize XPath.', @@ -117,7 +130,7 @@ liberator.plugins.nextlink = (function() { } for (let i = 0, len = this.siteinfo.length; i < len; i++) { - if (url.match(this.siteinfo[i].url)) { + if (url.match(this.siteinfo[i].url) && !/^\^https\?:\/\/\.$/.test(this.siteinfo[i].url)) { this.setCache(url, ['doc', 'xpath', 'siteinfo'], [window.content.document, this.siteinfo[i].nextLink, this.siteinfo[i]] @@ -130,7 +143,6 @@ liberator.plugins.nextlink = (function() { }, onLocationChange: function(url, isCallerLoaded) { - if (!this.initialized || !this.cache[url] || !this.cache[url].hasOwnProperty('xpath')) return; @@ -151,27 +163,36 @@ liberator.plugins.nextlink = (function() { var next = cache.next; if (!prev) - mappings.remove(config.browserModes, '[['); + this.removeMap('[['); if (!next) - mappings.remove(config.browserModes, ']]'); + this.removeMap(']]'); this.pager.customizeMap(context, url, prev, next); }, restorOrginalMap: function() { if (this.isCurOriginalMap) return; - mappings.remove(config.browserModes, '[['); - mappings.remove(config.browserModes, ']]'); + this.removeMap('[['); + this.removeMap(']]'); this.isCurOriginalMap = true; }, setCache: function(key, subKeys, values) { - if (!this.cache[key]) this.cache[key] = {}; values = [].concat(values); [].concat(subKeys).forEach($U.bind(this, function(subKey, i) { this.cache[key][subKey] = values[i]; })); + }, + removeMap: function(cmd) { + try { + if (mappings.hasMap(this.browserModes, cmd)) {; + mappings.remove(this.browserModes, cmd); + } + return true; + } catch (e) { + return false; + } } };//}}} @@ -196,8 +217,13 @@ liberator.plugins.nextlink = (function() { insertPoint = lastPageElement.nextSibling || lastPageElement.parentNode.appendChild(doc.createTextNode(' ')) - let css = util.xmlToDom(pageNaviCss, doc); - doc.body.appendChild(util.xmlToDom(pageNaviCss, doc)); + + if (context.isNew) { + let css = $U.xmlToDom(pageNaviCss, doc); + let node = doc.importNode(css, true); + doc.body.insertBefore(node, doc.body.firstChild); + //doc.body.appendChild(css); + } $U.getNodesFromXPath(cache.xpath, doc, function(item) elem = item, this); @@ -212,7 +238,7 @@ liberator.plugins.nextlink = (function() { var cache = context.cache[url]; var doc = cache.doc; - mappings.addUserMap(config.browserModes, ['[['], 'customize by nextlink.js', + mappings.addUserMap(context.browserModes, ['[['], 'customize by nextlink.js', $U.bind(this, function(count) { if (cache.curPage == 1) { return; @@ -225,15 +251,21 @@ liberator.plugins.nextlink = (function() { } } }), - { flags: Mappings.flags.count }); + { flags: Mappings.flags.COUNT }); - mappings.addUserMap(config.browserModes, [']]'], 'customize by nextlink.js', + mappings.addUserMap(context.browserModes, [']]'], 'customize by nextlink.js', $U.bind(this, function(count) { var reqUrl, lastReqUrl; reqUrl = $U.pathToURL(cache.next[cache.curPage - 1], doc); lastReqUrl = cache.lastReqUrl; + if (cache.isLoading) { + logger.echo('loading now...'); + return; + } + if (!reqUrl || cache.curPage == cache.terminate) { + logger.echo('end of pages.'); return; } if (cache.loadedURLs[reqUrl]) { @@ -241,15 +273,15 @@ liberator.plugins.nextlink = (function() { this.focusPagenavi(context, url, cache.curPage); return; } - context.setCache(url, 'lastReqUrl', reqUrl); + context.setCache(url, ['lastReqUrl', 'isLoading'], [reqUrl, true]); var req = new libly.Request( - //reqUrl + '?' + new Date(), null, reqUrl, null, - { asynchronous: false, encoding: doc.characterSet, + { asynchronous: true, encoding: doc.characterSet, context: context, url: url } ); req.addEventListener('onSuccess', $U.bind(this, this.onSuccess)); req.addEventListener('onFailure', $U.bind(this, this.onFailure)); + req.addEventListener('onException', $U.bind(this, this.onFailure)); req.get(); }), { flags: Mappings.flags.COUNT } @@ -266,6 +298,7 @@ liberator.plugins.nextlink = (function() { var prev = cache.next[cache.curPage]; var next = $U.getNodesFromXPath(cache.xpath, htmlDoc); + cache.isLoading = false; cache.loadedURLs[res.req.url] = true; if (!page || page.length < 1) { @@ -287,12 +320,15 @@ liberator.plugins.nextlink = (function() { addPage: function(context, doc, url, page, reqUrl) { var cache = context.cache[url]; - //var doc = cache.doc; var HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml'; - var hr = doc.createElementNS(HTML_NAMESPACE, 'hr'); + //var hr = doc.createElementNS(HTML_NAMESPACE, 'hr'); var p = doc.createElementNS(HTML_NAMESPACE, 'p'); + var tagName; + + if (page[0] && page[0].tagName) + tagName = page[0].tagName.toLowerCase(); - if (page[0] && page[0].tagName == 'TR') { + if (tagName == 'tr') { let insertParent = cache.insertPoint.parentNode; let colNodes = getElementsByXPath('child::tr[1]/child::*[self::td or self::th]', insertParent); @@ -308,24 +344,35 @@ liberator.plugins.nextlink = (function() { td.setAttribute('colspan', colums); tr.appendChild(td); insertParent.insertBefore(tr, cache.insertPoint); + } else if (tagName == 'li') { + let li = doc.createElementNS(HTML_NAMESPACE, 'li'); + li.id = 'vimp' + cache.curPage; + cache.insertPoint.parentNode.insertBefore(li, cache.insertPoint); + li.appendChild(p); } else { - cache.insertPoint.parentNode.insertBefore(hr, cache.insertPoint); + //cache.insertPoint.parentNode.insertBefore(hr, cache.insertPoint); cache.insertPoint.parentNode.insertBefore(p, cache.insertPoint); } - hr.id = 'vimperator-nextlink-' + cache.curPage; + p.id = 'vimperator-nextlink-' + cache.curPage; p.innerHTML = 'page: ' + cache.curPage + ''; p.className = 'vimperator-nextlink-page'; cache.mark.push(p); - return page.map(function(i) { - var pe = doc.importNode(i, true); + return page.map(function(elem) { + var pe = doc.importNode(elem, true); cache.insertPoint.parentNode.insertBefore(pe, cache.insertPoint); return pe; }); }, onFailure: function(res) { - res.res.options.context.setCache(res.req.options.url, 'terminate', cache.curPage); + logger.log('onFailure'); + var context = res.req.options.context; + var url = res.req.options.url; + var cache = context.cache[url]; + cache.isLoading = false; + logger.echoerr('nextlink: loading failed. ' + '[' + res.status + ']' + res.statusText); + res.req.options.context.setCache(res.req.options.url, 'terminate', cache.curPage); }, focusPagenavi: function(context, url, page) { var elem, p; @@ -352,6 +399,10 @@ liberator.plugins.nextlink = (function() { $U.getNodesFromXPath(cache.xpath, doc, function(item) elem = item, this); var nextUrl = $U.pathToURL(elem, doc); + var prev = $U.getNodesFromXPath('//a[@rel="prev"] | //link[@rel="prev"]', doc); + if (prev.length) { + context.setCache(url, 'prev', prev[0]); + } context.setCache(nextUrl, 'prev', url); context.setCache(url, 'next', elem); }, @@ -361,12 +412,18 @@ liberator.plugins.nextlink = (function() { var doc = cache.doc; if (prev) - mappings.addUserMap(config.browserModes, ['[['], 'customize by nextlink.js', - function(count) { liberator.open(prev, liberator.CURRENT_TAB); }, - { flags: Mappings.flags.count }); + mappings.addUserMap(context.browserModes, ['[['], 'customize by nextlink.js', + function(count) { + if (prev.href) { + buffer.followLink(prev, liberator.CURRENT_TAB); + } else { + liberator.open(prev, liberator.CURRENT_TAB); + } + }, + { flags: Mappings.flags.COUNT }); if (next) - mappings.addUserMap(config.browserModes, [']]'], 'customize by nextlink.js', + mappings.addUserMap(context.browserModes, [']]'], 'customize by nextlink.js', function(count) { buffer.followLink(next, liberator.CURRENT_TAB); }, { flags: Mappings.flags.COUNT }); } -- cgit v1.2.3