Skip to content

Commit 1cb47d6

Browse files
committed
Merge pull request #26 from zhangxinxu/develop
Develop
2 parents 4b942b8 + f04b34c commit 1cb47d6

15 files changed

+589
-20
lines changed

docs/Mobilebone.ajax.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ <h2>Mobilebone.ajax(trigger_or_options)</h2>
1616
<p>ajax请求页面数据并动态载入。很重要,应该会比较多用到的API,大家需要留意。</p>
1717
<h3>参数</h3>
1818
<ul>
19-
<li><code>trigger_or_options</code> <code>&lt;a&gt;</code>元素或者Ajax请求参数对象。必须参数。</li>
19+
<li><strong>trigger_or_options</strong> <code>&lt;a&gt;</code>元素或者Ajax请求参数对象。必须参数。</li>
2020
</ul>
2121
<h3>返回值</h3>
2222
<p>-。</p>
2323
<h3>使用示例</h3>
24-
<pre>Mobilebone.ajax(document.querySelector(“a”));
24+
<pre>Mobilebone.ajax(document.querySelector("a"));
2525
Mobilebone.ajax({
26-
url: xxx.html,
26+
url: 'xxx.html',
2727
success: function() {}
2828
});</pre>
2929
</div>

docs/Mobilebone.captureForm.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
6+
<title>Mobilebone.js API文档-Mobilebone.captureForm</title>
7+
<link rel="stylesheet" href="../src/mobilebone.css">
8+
<link rel="stylesheet" href="docs.css">
9+
</head>
10+
11+
<body>
12+
<aside></aside>
13+
<div class="page out">
14+
<div class="content">
15+
<h2>Mobilebone.captureForm</h2>
16+
<p>是否捕获页面上的<code>&lt;form&gt;</code>表单,执行Ajax请求并把返回结果装载成新页面无刷新载入。此为全局设置,影响整个页面。默认为<code>true</code>.</p>
17+
<h3>值类型</h3>
18+
<p>布尔值。设置为<code>true</code><code>false</code>.</p>
19+
<h3>使用</h3>
20+
<pre>Mobilebone.captureForm = false;</pre>
21+
<h3>效果</h3>
22+
<p>如果按照上面的设置,页面上所有的<code>&lt;form&gt;</code>表单提交会走原生<code>submit</code>提交,除非有<code>data-ajax="true"</code>重置。</p>
23+
<h3>其他说明</h3>
24+
<ul>
25+
<li>此为全局设置,会影响整个页面<code>&lt;form&gt;</code>表单<code>submit</code>行为。但是可以设置<code>data-ajax</code>的值为<code>true</code>还是<code>false</code>做局部重置。</li>
26+
</ul>
27+
</div>
28+
</div>
29+
30+
<script src="../src/mobilebone.js"></script>
31+
<script>
32+
Mobilebone.captureForm = false;
33+
window.navKey = "captureForm";
34+
</script>
35+
<script src="nav.js"></script>
36+
<script src="docs.js"></script>
37+
</body>
38+
</html>

docs/Mobilebone.submit.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
6+
<title>Mobilebone.js API文档-Mobilebone.submit</title>
7+
<link rel="stylesheet" href="../src/mobilebone.css">
8+
<link rel="stylesheet" href="docs.css">
9+
</head>
10+
11+
<body>
12+
<aside></aside>
13+
<div class="page out">
14+
<div class="content">
15+
<h2>Mobilebone.submit(formElement)</h2>
16+
<p>让表单元素告别原生<code>submit</code>提交,走Mobilebone的<code>ajax</code>提交方法,并根据返回结果执行对应的过场效果。</p>
17+
<h3>参数</h3>
18+
<ul>
19+
<li><strong>formElement</strong> <code>&lt;form&gt;</code>表单元素。必须参数。</li>
20+
</ul>
21+
<h3>返回值</h3>
22+
<p>-</p>
23+
<h3>使用示例</h3>
24+
<pre>Mobilebone.submit(document.querySelector("form"));</pre>
25+
</div>
26+
</div>
27+
28+
<script src="../src/mobilebone.js"></script>
29+
<script>
30+
Mobilebone.captureLink = false;
31+
window.navKey = "submit";
32+
</script>
33+
<script src="nav.js"></script>
34+
<script src="docs.js"></script>
35+
</body>
36+
</html>

docs/nav.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
var key = window.navKey, html = '<nav>', active = '', matchs = false;
66

7-
["Mobilebone.support", "Mobilebone.VERSION", "Mobilebone.autoInit", "Mobilebone.captureLink", "Mobilebone.rootTransition", "Mobilebone.mergeCallback", "Mobilebone.classPage", "Mobilebone.classMask", "Mobilebone.pushStateEnabled", "Mobilebone.transition()", "Mobilebone.getCleanUrl()", "Mobilebone.getPage()", "Mobilebone.createPage()", "Mobilebone.getFunction()", "Mobilebone.ajax()", "Mobilebone.isBack()", "Mobilebone.jsonHandle()", "Mobilebone.init()", "Mobilebone.handleTapEvent()", "otherAnonymousAPIs", "data-title", "data-root", "data-params", "data-callbackKeys", "data-mask", "data-rel", "data-ajax", "data-ajaxKeys"].forEach(function(innerhtml) {
7+
["Mobilebone.support", "Mobilebone.VERSION", "Mobilebone.autoInit", "Mobilebone.captureLink", "Mobilebone.captureForm", "Mobilebone.rootTransition", "Mobilebone.mergeCallback", "Mobilebone.classPage", "Mobilebone.classMask", "Mobilebone.pushStateEnabled", "Mobilebone.transition()", "Mobilebone.getCleanUrl()", "Mobilebone.getPage()", "Mobilebone.createPage()", "Mobilebone.getFunction()", "Mobilebone.ajax()", "Mobilebone.submit()", "Mobilebone.isBack()", "Mobilebone.jsonHandle()", "Mobilebone.init()", "Mobilebone.handleTapEvent()", "otherAnonymousAPIs", "data-title", "data-root", "data-params", "data-callbackKeys", "data-mask", "data-rel", "data-ajax", "data-ajaxKeys"].forEach(function(innerhtml) {
88
if (matchs == true) {
99
active = '';
1010
} else if (key && (key == innerhtml || new RegExp(key).test(innerhtml))) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mobilebone",
3-
"version": "1.1.8",
3+
"version": "1.2.0",
44
"description": "Bone main for mobile web APP with a sigle page mode.",
55
"main": "src/mobilebone.js",
66
"directories": {

src/mobilebone.js

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*
4141
* @type string
4242
**/
43-
Mobilebone.VERSION = '1.1.8';
43+
Mobilebone.VERSION = '1.2.0';
4444

4545
/**
4646
* Whether catch attribute of href from element with tag 'a'
@@ -53,6 +53,17 @@
5353
**/
5454
Mobilebone.captureLink = true;
5555

56+
/**
57+
* Whether catch events of 'submit' from <form> element
58+
* If the value set to false, <form> is a normal form except data-ajax="true"
59+
* If the value set to true, <form> will submit as a ajax request,
60+
and the return value will be used to create a new page and transition into meanwhile.
61+
However, if data-ajax="false", <form> won't submit as a ajax.
62+
*
63+
* @type boolean
64+
**/
65+
Mobilebone.captureForm = true;
66+
5667
/**
5768
* The root of transition-callback
5869
* Default value is 'root', you can consider as window-object.
@@ -150,8 +161,7 @@
150161

151162
// rules as follow:
152163
// data-* > data-params > options > defaults
153-
["title", "root", "form"].forEach(function(key) {
154-
164+
["title", "root", "form"].forEach(function(key) {
155165
_params[key] = element.getAttribute("data-" + key) || _dataparams[key] || options[key] || defaults[key];
156166
});
157167

@@ -182,7 +192,6 @@
182192
}
183193
});
184194

185-
186195
return _params;
187196
};
188197

@@ -243,6 +252,10 @@
243252
} else if (typeof onpagefirstinto == "function") {
244253
onpagefirstinto(pageInto, pageOut, options.response);
245254
}
255+
// capture form submit
256+
slice.call(pageInto.querySelectorAll("form")).forEach(function(form) {
257+
Mobilebone.submit(form);
258+
});
246259
}
247260

248261
// do callback when animation start/end
@@ -253,12 +266,13 @@
253266
});
254267
if (!store[pageid]) {
255268
var animateEventName = isWebkit? webkitkey: animationkey;
269+
// if it's the out element, hide it when 'animationend'
256270
index && pageInto.addEventListener(animateEventName, function() {
257271
if (this.classList.contains("in") == false) {
258272
this.style.display = "none";
259273
}
260274
});
261-
275+
// bind animation events
262276
if (typeof animition == "string" && params_in.root[animition]) {
263277
pageInto.addEventListener(animateEventName, function() {
264278
params_in.root[animition](this, this.classList.contains("in")? "into": "out");
@@ -312,6 +326,9 @@
312326
var href = "", formdata = "", clean_url = "";
313327
if (trigger) {
314328
if (trigger.nodeType == 1) {
329+
// form element
330+
if (trigger.action) return trigger.getAttribute("action");
331+
// a element
315332
href = trigger.getAttribute("href");
316333
formdata = trigger.getAttribute("data-formdata") || trigger.getAttribute("data-data");
317334
} else if (trigger.url) {
@@ -386,7 +403,7 @@
386403
if (element_or_options) {
387404
if (element_or_options.nodeType == 1) {
388405
// legal elements
389-
if (element_or_options.href) {
406+
if (element_or_options.href || element_or_options.action) {
390407
page_title = element_or_options.getAttribute("data-title") || options.title;
391408
}
392409
response = options.response;
@@ -425,10 +442,20 @@
425442
create = null;
426443

427444
// do transition
428-
this.transition(create_page, current_page, {
445+
var optionsTransition = {
429446
response: response || dom_or_html,
430447
id: this.getCleanUrl(element_or_options) || create_page.id || ("unique" + Date.now())
431-
});
448+
};
449+
// 'if' statement below added on v2.0.0
450+
if (typeof options == "object") {
451+
if (typeof options.history != "undefined") {
452+
optionsTransition.history = options.history;
453+
}
454+
if (typeof options.remove != "undefined") {
455+
optionsTransition.remove = options.remove;
456+
}
457+
}
458+
this.transition(create_page, current_page, optionsTransition);
432459
};
433460

434461
/**
@@ -458,7 +485,7 @@
458485
* For ajax request to get HTML or JSON.
459486
460487
* @params trigger_or_options - Necessary
461-
1. dom-object. or~
488+
1. dom-object:<a>|<form>.
462489
2. object.
463490
* @returns undefined
464491
* @example Mobilebone.ajax(document.querySelector("a"));
@@ -474,6 +501,7 @@
474501
// default params
475502
var defaults = {
476503
url: "",
504+
type: "",
477505
dataType: "",
478506
data: {},
479507
timeout: 10000,
@@ -485,7 +513,7 @@
485513
complete: function() {}
486514
};
487515

488-
var params = {}, ele_mask = null;
516+
var params = {}, ele_mask = null, formData = null;
489517

490518
// if 'trigger_or_options' is a element, we should turn it to options-object
491519
var params_from_trigger = {}, attr_mask;
@@ -503,10 +531,16 @@
503531
}
504532
}
505533
}
506-
507-
// the ajax url is special, we need special treatment
534+
508535
params.url = this.getCleanUrl(trigger_or_options, params.url);
509536

537+
var tagName = trigger_or_options.tagName.toLowerCase();
538+
if (tagName == "form") {
539+
params.type = trigger_or_options.method;
540+
541+
formData = new FormData(trigger_or_options);
542+
}
543+
510544
// get mask element
511545
attr_mask = trigger_or_options.getAttribute("data-mask");
512546
if (attr_mask == "true" || attr_mask == "") {
@@ -547,7 +581,7 @@
547581

548582
// ajax request
549583
var xhr = new XMLHttpRequest();
550-
xhr.open("GET", params.url + (/\?/.test(params.url)? "&" : "?") + "r=" + Date.now(), params.async, params.username, params.password);
584+
xhr.open(params.type || "GET", params.url + (/\?/.test(params.url)? "&" : "?") + "r=" + Date.now(), params.async, params.username, params.password);
551585
xhr.timeout = params.timeout;
552586

553587
xhr.onload = function() {
@@ -565,6 +599,11 @@
565599
params.error.call(params, xhr, xhr.status);
566600
}
567601
} else if (params.dataType == "unknown") {
602+
// ajax send by url
603+
// no history hush
604+
// no element remove
605+
params.history = false;
606+
params.remove = false;
568607
try {
569608
// as json
570609
response = JSON.parse(xhr.response);
@@ -606,7 +645,25 @@
606645
ele_mask.style.visibility = "hidden";
607646
};
608647

609-
xhr.send(null);
648+
xhr.send(formData);
649+
};
650+
651+
/**
652+
* capture form submit events to a ajax request.
653+
654+
* @params form: formElement. - Necessary
655+
* @example Mobilebone.form(document.querySelector("form"));
656+
*
657+
**/
658+
Mobilebone.submit = function(form) {
659+
if (!form || typeof form.action != "string") return;
660+
var ajax = form.getAttribute("data-ajax");
661+
if (ajax == "false" || (this.captureForm == false && ajax != "true")) return;
662+
663+
form.addEventListener("submit", function(event) {
664+
Mobilebone.ajax(this);
665+
event.preventDefault();
666+
});
610667
};
611668

612669

@@ -710,6 +767,7 @@
710767
});
711768
}
712769
}
770+
713771
// change flag-var for avoiding repeat init
714772
hasInited = true;
715773
};
@@ -819,6 +877,7 @@
819877
popup();
820878
return element;
821879
};
880+
822881
/**
823882
* private method: convert query string to key-value object
824883
**/
@@ -849,11 +908,17 @@
849908
**/
850909
window.addEventListener("popstate", function() {
851910
var hash = location.hash.replace("#&", "").replace("#", "");
911+
if (hash == "") return;
852912

853913
var page_in = store[hash];
854914

855915
if (!page_in) {
856916
if(isSimple.test(hash) == false) {
917+
// as a url
918+
Mobilebone.ajax({
919+
url: hash,
920+
dataType: "unknown"
921+
});
857922
return;
858923
}
859924
page_in = document.querySelector("#" + hash)
@@ -862,7 +927,6 @@
862927
var page_out = document.querySelector(".in." + Mobilebone.classPage);
863928

864929
if ((page_in && page_in == page_out) || Mobilebone.pushStateEnabled == false) return;
865-
866930

867931
// hash ↔ id
868932
if (page_in) {

test/ajax-html/b.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
4+
<title>b页面</title>
5+
</head>
6+
7+
<body>
8+
<div class="page out">
9+
<ul>
10+
<li><a href="c.html">此为b页面,点击加载c页面</a></li>
11+
</ul>
12+
</div>
13+
</body>
14+
</html>

test/ajax-html/c.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
4+
<title>c页面</title>
5+
</head>
6+
7+
<body>
8+
<div class="page out">
9+
<ul>
10+
<li><a href="#pageHome" data-rel="back">c页面,直接刷新,浏览器返回</a></li>
11+
</ul>
12+
</div>
13+
</body>
14+
</html>

test/ajax-html/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<ul>
2222
<li><a href="ajax-without-page.html" data-title="纯a标签元素返回页面">点击加载-返回不含page, 自动创建page</a></li>
2323
</ul>
24+
<ul>
25+
<li><a href="b.html">连续加载测试-加载b页面</a></li>
26+
</ul>
2427
<ul>
2528
<li><a href="../index.html" data-ajax="false">&laquo; 返回测试引导首页</a></li>
2629
</ul>

0 commit comments

Comments
 (0)