主执行模块为适应无刷新与立即显示需求,进行功能结构调整

This commit is contained in:
TevinLi 2016-08-14 22:08:45 +08:00
parent f8b6795de6
commit c3fd615336
6 changed files with 188 additions and 143 deletions

View File

@ -24,7 +24,7 @@ amWiki是一套非常简单基于atom编辑器markdown语法的轻量级wiki文
2. 安装Atom插件amWiki并重启Atom
- Atom菜单File -> Setting -> Install -> 搜索`amWiki`
- 或者运行cmd`apm install amWiki`
- 或者从Github的 [amWiki项目托管](https://github.com/TevinLi/amWiki) 下载zip解压到`C:\Users\Administrator\.atom\packages`,并将文件夹名`amWiki-master`改为`amWiki`
- 或者从Github的 [amWiki项目托管·版本发布](https://github.com/TevinLi/amWiki/releases) 下载zip解压到`C:\Users\Administrator\.atom\packages`,并将文件夹名`amWiki-master`改为`amWiki`
3. (在本地服务器静态目录)创建一个文件夹

View File

@ -1,6 +1,6 @@
/**
* @desc 文档加载渲染模块
* @author Tevin
* @desc 文档加载渲染管理
*/
;
@ -12,6 +12,9 @@
var marked = win.marked;
var URL_ENCODE_NAME = 'AMWikiUrlEncode'; //记录url编码的键名
/**
* @class 创建一个文档管理对象
*/
var Docs = function () {
this.$e = {
win: $(win),

View File

@ -1,9 +1,8 @@
/**
* amWiki
* https://github.com/TevinLi/amWiki
* by Tevin
*
* Released under the MIT license.
* @desc amwiki主执行模块
* @author Tevin
* @see {@link https://github.com/TevinLi/amWiki}
* @license MIT - Released under the MIT license.
*/
$(function () {
@ -23,116 +22,144 @@ $(function () {
//是否支持history.state的API (IE9不支持)
var historyStated = 'pushState' in history;
//菜单折叠
//页面基本显示与操作
var $menuBar = $('#menuBar');
$menuBar.on('click', 'h5', function () {
var $this = $(this),
$next = $this.next('ul');
if ($this.hasClass('on')) {
$this.removeClass('on');
$next.slideUp(200);
} else {
$this.addClass('on');
$next.slideDown(200);
}
});
$menuBar.on('click', 'h4', function () {
var $this = $(this);
if (!$this.hasClass('on')) {
$this.addClass('on');
$menuBar.find('a').removeClass('on');
}
$menuBar.find('h5').removeClass('on').next('ul').hide();
});
$menuBar.on('click', 'strong', function () {
var $this = $(this),
$next = $this.next('ul');
if ($this.hasClass('on')) {
$this.removeClass('on');
$next.slideUp(200);
} else {
$this.addClass('on');
$next.slideDown(200);
}
});
//展开折叠所有导航栏位按钮
var setMenuFolding = function () {
var $this = $(this);
if ($this.hasClass('on')) {
$this.removeClass('on').find('use').attr('xlink:href', '#navFolder1');
var pageBase = function () {
//菜单折叠
$menuBar.on('click', 'h5', function () {
var $this = $(this),
$next = $this.next('ul');
if ($this.hasClass('on')) {
$this.removeClass('on');
$next.slideUp(200);
} else {
$this.addClass('on');
$next.slideDown(200);
}
});
$menuBar.on('click', 'h4', function () {
var $this = $(this);
if (!$this.hasClass('on')) {
$this.addClass('on');
$menuBar.find('a').removeClass('on');
}
$menuBar.find('h5').removeClass('on').next('ul').hide();
});
$menuBar.on('click', 'strong', function () {
var $this = $(this),
$next = $this.next('ul');
if ($this.hasClass('on')) {
$this.removeClass('on');
$next.slideUp(200);
} else {
$this.addClass('on');
$next.slideDown(200);
}
});
//展开折叠所有导航栏位按钮
$('.menu-fold').on('click', function () {
var $this = $(this);
if ($this.hasClass('on')) {
$this.removeClass('on').find('use').attr('xlink:href', '#navFolder1');
$menuBar.find('h5').removeClass('on').next('ul').hide();
} else {
$this.addClass('on').find('use').attr('xlink:href', '#navFolder2');
$menuBar.find('h5').addClass('on').next('ul').show();
}
});
//响应式菜单
var $nav = $('.nav'),
$menuIcon = $('.menu_icon');
$menuIcon.on('click', function () {
var $this = $(this);
if ($this.hasClass('close')) {
$this.removeClass('close');
$menuIcon.find('use').attr('xlink:href', '#navStart');
$nav.removeClass('on');
} else {
$this.addClass('close');
$menuIcon.find('use').attr('xlink:href', '#navClose');
$nav.addClass('on');
}
});
$menuBar.on('click', 'li a', function () {
$nav.removeClass('on');
});
$menuBar.on('click', 'h4', function () {
$nav.removeClass('on');
});
//页面筛选
var $filter = $('#menuFilter');
var $filterClean = $filter.next('i');
$filter.on('blur change input propertychange', function () {
var value = $filter.val();
if (value != '') {
$filterClean.removeClass('off');
$menuBar.find('h5').removeClass('on').next('ul').hide();
$menuBar.find('a').each(function () {
var $this = $(this);
if ($this.text().indexOf(value) >= 0) {
$this.parent().removeClass('off').parent().show().prev('h5').addClass('on');
} else {
$this.parent().addClass('off');
}
});
} else {
$filterClean.addClass('off');
$menuBar.find('a').parent().removeClass('off');
}
});
$filterClean.on('click', function () {
$filter.val('').trigger('change');
});
//显示svg图标
if (sessionStorage['AMWikiIconsSvg']) {
$('#svgSymbols').append(sessionStorage['AMWikiIconsSvg']);
} else {
$this.addClass('on').find('use').attr('xlink:href', '#navFolder2');
$menuBar.find('h5').addClass('on').next('ul').show();
$.get('amWiki/images/icons.svg', function (svg) {
sessionStorage['AMWikiIconsSvg'] = svg;
$('#svgSymbols').append(svg);
}, 'text');
}
};
//响应式菜单
var $nav = $('.nav'),
$menuIcon = $('.menu_icon'),
$win = $(window);
$menuIcon.on('click', function () {
var $this = $(this);
if ($this.hasClass('close')) {
$this.removeClass('close');
$menuIcon.find('use').attr('xlink:href', '#navStart');
$nav.removeClass('on');
//改变导航显示
var changeNav = function (path) {
if (path == '首页') {
$menuBar.find('h4').addClass('on');
$menuBar.find('a').removeClass('on');
} else {
$this.addClass('close');
$menuIcon.find('use').attr('xlink:href', '#navClose');
$nav.addClass('on');
}
});
$menuBar.on('click', 'li a', function () {
$nav.removeClass('on');
});
$menuBar.on('click', 'h4', function () {
$nav.removeClass('on');
});
//页面筛选
var $filter = $('#menuFilter');
var $filterClean = $filter.next('i');
$filter.on('blur change input propertychange', function () {
var value = $filter.val();
if (value != '') {
$filterClean.removeClass('off');
$menuBar.find('h5').removeClass('on').next('ul').hide();
var hsLink = false;
$menuBar.find('a').each(function () {
var $this = $(this);
if ($this.text().indexOf(value) >= 0) {
$this.parent().removeClass('off').parent().show().prev('h5').addClass('on');
var path2 = $this.attr('href').split('file=')[1];
if (path2 == path) {
hsLink = true;
//第一层
var $prev = $this.addClass('on').parent().parent().show().prev().addClass('on');
//第二层
if ($prev[0].tagName.toLowerCase() == 'strong') {
$prev.parent().parent().show().prev().addClass('on');
}
} else {
$this.parent().addClass('off');
$this.removeClass('on');
}
});
} else {
$filterClean.addClass('off');
$menuBar.find('a').parent().removeClass('off');
if (hsLink) {
$menuBar.find('h4').removeClass('on');
}
}
});
$filterClean.on('click', function () {
$filter.val('').trigger('change');
});
//显示svg图标
if (sessionStorage['AMWikiIconsSvg']) {
$('#svgSymbols').append(sessionStorage['AMWikiIconsSvg']);
} else {
$.get('amWiki/images/icons.svg', function (svg) {
sessionStorage['AMWikiIconsSvg'] = svg;
$('#svgSymbols').append(svg);
}, 'text');
}
};
//改变页面
var changePage = function (path, firstOpen) {
var changePage = function (path, withOutPushState) {
//第一步,从本地缓存读取并渲染页面
var localDoc = storage.read(path);
docs.renderDoc(localDoc);
testing && testing.crawlContent();
//更新history记录
if (!firstOpen && historyStated) {
history.pushState(null, '', '?file=' + path);
if (!withOutPushState && historyStated) {
history.pushState({path: path}, '', '?file=' + path);
}
//第二步,加载服务器上的文档资源,如果有更新重新渲染
docs.loadPage(path, function (type, content) {
@ -146,7 +173,7 @@ $(function () {
}
});
if (historyStated) {
history.pushState(null, '', '?file=首页');
history.replaceState({path: '首页'}, '', '?file=首页');
}
}
//如果本地缓存不为空,但服务器文档读取失败,不进行任何操作
@ -164,6 +191,29 @@ $(function () {
});
};
//读取目录导航
var loadNav = function (callback) {
$.get('library/$navigation.md', function (data) {
$menuBar.html(marked(data));
$menuBar
.find('h4').prepend('<svg><use xlink:href="#navHome"></use></svg>').end()
.find('h5').prepend('<svg><use xlink:href="#navArrow"></use></svg>');
//支持history api时改变默认事件导航不再跳转页面
$menuBar.find('a').each(function () {
if (historyStated) {
var $this = $(this);
$this.on('click', function () {
var path = $this.attr('href').split('file=')[1];
changeNav(path);
changePage(path);
return false;
});
}
});
callback && callback();
}, 'text');
};
//解析地址参数
var path = tools.getURLParameter('file');
if (!path) {
@ -171,47 +221,28 @@ $(function () {
} else {
path = decodeURI(path);
}
changePage(path, true);
//读取导航目录
$.get('library/$navigation.md', function (data) {
$menuBar.html(marked(data));
$('.menu-fold').on('click', setMenuFolding);
$menuBar
.find('h4').prepend('<svg><use xlink:href="#navHome"></use></svg>').end()
.find('h5').prepend('<svg><use xlink:href="#navArrow"></use></svg>');
//初次打开页面设置导航显示
if (path == '首页') {
$menuBar.find('h4').addClass('on');
} else {
var hsLink = false;
$menuBar.find('a').each(function () {
var $this = $(this);
var path2 = $(this).attr('href').split('file=')[1];
if (path2 == path) {
hsLink = true;
//第一层
var $prev = $this.addClass('on').parent().parent().show().prev().addClass('on');
//第二层
if ($prev[0].tagName.toLowerCase() == 'strong') {
$prev.parent().parent().show().prev().addClass('on');
}
} else {
$this.removeClass('on');
}
//支持历史api时改变默认事件
if (historyStated) {
$this.on('click', function () {
changePage(path2);
return false;
});
}
});
if (hsLink) {
$menuBar.find('h4').removeClass('on');
}
}
}, 'text');
//页面基本
pageBase();
//加载导航
loadNav(function () {
//首次打开改变导航
changeNav(path);
//首次打开改变页面
changePage(path, true);
});
//history api 前进后退响应
if (historyStated) {
$(window).on('popstate', function (e) {
var path = e.originalEvent.state.path;
//改变导航
changeNav(path);
//改变页面
changePage(path, true);
});
}
});

View File

@ -1,6 +1,6 @@
/**
* @desc 本地缓存模块
* @author Tevin
* @desc 本地缓存
*/
;
@ -11,6 +11,9 @@
var tools = win.tools;
var LOCAL_STORAGE_NAME = 'AMWikiDataBase'; //本地数据localStorage键名
/**
* @class 创建一个本地存储管理对象
*/
var Storage = function () {
this.db = null; //内存中的文库缓存
this.bridgeLocalStorage('read');

View File

@ -1,11 +1,16 @@
/**
* @desc 简单ajax测试模块
* @author Tevin
* @desc 简单发送ajax测试工具
* 仅当页面存在请求地址请求类型请求参数三个h3标题时触发
* @summary 仅当页面存在请求地址请求类型请求参数三个h3标题时触发
*/
(function (win, doc, $) {
'use strict';
/**
* @class 创建一个借口测试管理对象
*/
var Testing = function () {
//缓存元素
this.$e = {
@ -121,6 +126,9 @@
//关闭测试面板
Testing.prototype.offPanel = function () {
this.$e.testingShow.removeClass('show');
if (this.$e.testingShow.hasClass('on')) {
this.displayBox('off');
}
//清除抓取参数
this.request.url = '';
this.request.method = '';

View File

@ -1,6 +1,6 @@
/**
* @author Tevin
* @desc 工具集
* @author Tevin
*/
;