|
53 | 53 | **/
|
54 | 54 | Mobilebone.captureLink = true;
|
55 | 55 |
|
| 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 | + |
56 | 67 | /**
|
57 | 68 | * The root of transition-callback
|
58 | 69 | * Default value is 'root', you can consider as window-object.
|
|
150 | 161 |
|
151 | 162 | // rules as follow:
|
152 | 163 | // data-* > data-params > options > defaults
|
153 |
| - ["title", "root", "form"].forEach(function(key) { |
154 |
| - |
| 164 | + ["title", "root", "form"].forEach(function(key) { |
155 | 165 | _params[key] = element.getAttribute("data-" + key) || _dataparams[key] || options[key] || defaults[key];
|
156 | 166 | });
|
157 | 167 |
|
|
182 | 192 | }
|
183 | 193 | });
|
184 | 194 |
|
185 |
| - |
186 | 195 | return _params;
|
187 | 196 | };
|
188 | 197 |
|
|
243 | 252 | } else if (typeof onpagefirstinto == "function") {
|
244 | 253 | onpagefirstinto(pageInto, pageOut, options.response);
|
245 | 254 | }
|
| 255 | + // capture form submit |
| 256 | + slice.call(pageInto.querySelectorAll("form")).forEach(function(form) { |
| 257 | + Mobilebone.submit(form); |
| 258 | + }); |
246 | 259 | }
|
247 | 260 |
|
248 | 261 | // do callback when animation start/end
|
|
313 | 326 | var href = "", formdata = "", clean_url = "";
|
314 | 327 | if (trigger) {
|
315 | 328 | if (trigger.nodeType == 1) {
|
| 329 | + // form element |
| 330 | + if (trigger.action) return trigger.getAttribute("action"); |
| 331 | + // a element |
316 | 332 | href = trigger.getAttribute("href");
|
317 | 333 | formdata = trigger.getAttribute("data-formdata") || trigger.getAttribute("data-data");
|
318 | 334 | } else if (trigger.url) {
|
|
387 | 403 | if (element_or_options) {
|
388 | 404 | if (element_or_options.nodeType == 1) {
|
389 | 405 | // legal elements
|
390 |
| - if (element_or_options.href) { |
| 406 | + if (element_or_options.href || element_or_options.action) { |
391 | 407 | page_title = element_or_options.getAttribute("data-title") || options.title;
|
392 | 408 | }
|
393 | 409 | response = options.response;
|
|
459 | 475 | * For ajax request to get HTML or JSON.
|
460 | 476 |
|
461 | 477 | * @params trigger_or_options - Necessary
|
462 |
| - 1. dom-object. or~ |
| 478 | + 1. dom-object:<a>|<form>. |
463 | 479 | 2. object.
|
464 | 480 | * @returns undefined
|
465 | 481 | * @example Mobilebone.ajax(document.querySelector("a"));
|
|
475 | 491 | // default params
|
476 | 492 | var defaults = {
|
477 | 493 | url: "",
|
| 494 | + type: "", |
478 | 495 | dataType: "",
|
479 | 496 | data: {},
|
480 | 497 | timeout: 10000,
|
|
486 | 503 | complete: function() {}
|
487 | 504 | };
|
488 | 505 |
|
489 |
| - var params = {}, ele_mask = null; |
| 506 | + var params = {}, ele_mask = null, formData = null; |
490 | 507 |
|
491 | 508 | // if 'trigger_or_options' is a element, we should turn it to options-object
|
492 | 509 | var params_from_trigger = {}, attr_mask;
|
|
504 | 521 | }
|
505 | 522 | }
|
506 | 523 | }
|
507 |
| - |
508 |
| - // the ajax url is special, we need special treatment |
| 524 | + |
509 | 525 | params.url = this.getCleanUrl(trigger_or_options, params.url);
|
510 | 526 |
|
| 527 | + var tagName = trigger_or_options.tagName.toLowerCase(); |
| 528 | + if (tagName == "form") { |
| 529 | + params.type = trigger_or_options.method; |
| 530 | + |
| 531 | + formData = new FormData(trigger_or_options); |
| 532 | + } |
| 533 | + |
511 | 534 | // get mask element
|
512 | 535 | attr_mask = trigger_or_options.getAttribute("data-mask");
|
513 | 536 | if (attr_mask == "true" || attr_mask == "") {
|
|
548 | 571 |
|
549 | 572 | // ajax request
|
550 | 573 | var xhr = new XMLHttpRequest();
|
551 |
| - xhr.open("GET", params.url + (/\?/.test(params.url)? "&" : "?") + "r=" + Date.now(), params.async, params.username, params.password); |
| 574 | + xhr.open(params.type || "GET", params.url + (/\?/.test(params.url)? "&" : "?") + "r=" + Date.now(), params.async, params.username, params.password); |
552 | 575 | xhr.timeout = params.timeout;
|
553 | 576 |
|
554 | 577 | xhr.onload = function() {
|
|
607 | 630 | ele_mask.style.visibility = "hidden";
|
608 | 631 | };
|
609 | 632 |
|
610 |
| - xhr.send(null); |
| 633 | + xhr.send(formData); |
| 634 | + }; |
| 635 | + |
| 636 | + /** |
| 637 | + * capture form submit events to a ajax request. |
| 638 | + |
| 639 | + * @params form: formElement. - Necessary |
| 640 | + * @example Mobilebone.form(document.querySelector("form")); |
| 641 | + * |
| 642 | + **/ |
| 643 | + Mobilebone.submit = function(form) { |
| 644 | + if (!form || typeof form.action != "string") return; |
| 645 | + var ajax = form.getAttribute("data-ajax"); |
| 646 | + if (ajax == "false" || (this.captureForm == false && ajax != "true")) return; |
| 647 | + |
| 648 | + form.addEventListener("submit", function(event) { |
| 649 | + Mobilebone.ajax(this); |
| 650 | + event.preventDefault(); |
| 651 | + }); |
611 | 652 | };
|
612 | 653 |
|
613 | 654 |
|
|
711 | 752 | });
|
712 | 753 | }
|
713 | 754 | }
|
| 755 | + |
714 | 756 | // change flag-var for avoiding repeat init
|
715 | 757 | hasInited = true;
|
716 | 758 | };
|
|
0 commit comments