|
46 | 46 | *
|
47 | 47 | * @type string
|
48 | 48 | **/
|
49 |
| - Mobilebone.VERSION = '2.5.7'; |
| 49 | + Mobilebone.VERSION = '2.6.0'; |
50 | 50 |
|
51 | 51 | /**
|
52 | 52 | * Whether catch attribute of href from element with tag 'a'
|
|
252 | 252 | return false;
|
253 | 253 | }
|
254 | 254 |
|
| 255 | + // set animation callback as a method |
| 256 | + var fun_animationCall = function(page, data) { |
| 257 | + if (page.flagAniBind == true) return; |
| 258 | + // do callback when animation start/end |
| 259 | + ["animationstart", "animationend"].forEach(function(animationkey, index) { |
| 260 | + var animition = params_in[animationkey], webkitkey = "webkit" + animationkey.replace(/^a|s|e/g, function(matchs) { |
| 261 | + return matchs.toUpperCase(); |
| 262 | + }); |
| 263 | + var animateEventName = isWebkit? webkitkey: animationkey; |
| 264 | + // if it's the out element, hide it when 'animationend' |
| 265 | + index && page.addEventListener(animateEventName, function() { |
| 266 | + if (this.classList.contains("in") == false) { |
| 267 | + this.style.display = "none"; |
| 268 | + // add on v2.5.5 |
| 269 | + // move here on v2.5.8 |
| 270 | + if (this.removeSelf == true) { |
| 271 | + this.parentElement.removeChild(this); |
| 272 | + this.removeSelf = null; |
| 273 | + } |
| 274 | + } |
| 275 | + this.classList.remove(params(this).form); |
| 276 | + }); |
| 277 | + // bind animation events |
| 278 | + if (typeof animition == "string" && params_in.root[animition]) { |
| 279 | + page.addEventListener(animateEventName, function() { |
| 280 | + data.root[animition].call(data.root, this, this.classList.contains("in")? "into": "out", options); |
| 281 | + }); |
| 282 | + } else if (typeof animition == "function") { |
| 283 | + page.addEventListener(animateEventName, function() { |
| 284 | + animition.call(data.root, this, this.classList.contains("in")? "into": "out", options); |
| 285 | + }); |
| 286 | + } |
| 287 | + // set a flag |
| 288 | + page.flagAniBind = true; |
| 289 | + }); |
| 290 | + }; |
| 291 | + |
255 | 292 | if (pageOut != null && pageOut.classList) {
|
256 | 293 | // do transition if there are no 'prevent'
|
257 | 294 | if (isPreventOut != true) {
|
|
266 | 303 | pageOut.classList[back? "add": "remove"]("reverse");
|
267 | 304 |
|
268 | 305 | // add on v2.5.5
|
269 |
| - pageOut.removeSelf = null; |
| 306 | + pageOut.removeSelf = pageOut.removeSelf || null; |
| 307 | + |
| 308 | + // set animation callback for 'pageInto' |
| 309 | + // for issues #153 |
| 310 | + fun_animationCall(pageOut, params_out); |
270 | 311 |
|
271 | 312 | // do fallback every time
|
272 | 313 | var fallback = params_out.fallback;
|
|
282 | 323 | first_page = document.querySelector("." + this.classPage);
|
283 | 324 |
|
284 | 325 | // do title change
|
285 |
| - if (title) { |
| 326 | + if (title && options.title !== false) { |
286 | 327 | document.title = title;
|
287 | 328 | if (header) {
|
288 | 329 | header.innerHTML = title;
|
|
300 | 341 | pageid = pageid.split("?")[0];
|
301 | 342 | }
|
302 | 343 | var relid = store["_" + pageid];
|
303 |
| - if (options.remove !== false && store[pageid] && store[pageid] != pageInto && store[pageid].parentElement) { |
| 344 | + |
| 345 | + if (options.remove !== false && store[pageid] && store[pageid] != pageInto) { |
304 | 346 | // hashid may store the same page, we should delete also
|
305 | 347 | // when data-reload not 'false' or null
|
306 | 348 | // v2.4.4+
|
307 | 349 | if (relid && store[relid] && options.reload == true) {
|
308 | 350 | delete store[relid];
|
309 | 351 | delete store["_" + pageid];
|
310 | 352 | }
|
| 353 | + |
| 354 | + if (options.reload == true) { |
| 355 | + // v2.5.8 for issues #147 |
| 356 | + pageInto.removeSelf = true; |
| 357 | + } |
| 358 | + |
311 | 359 | if (store[pageid] != pageOut) {
|
312 |
| - store[pageid].parentElement.removeChild(store[pageid]); |
| 360 | + store[pageid].parentElement && store[pageid].parentElement.removeChild(store[pageid]); |
313 | 361 | } else {
|
314 | 362 | pageOut.removeSelf = true;
|
315 | 363 | }
|
|
347 | 395 | pageInto.firstintoBind = true;
|
348 | 396 | }
|
349 | 397 |
|
350 |
| - // do callback when animation start/end |
351 |
| - ["animationstart", "animationend"].forEach(function(animationkey, index) { |
352 |
| - var animition = params_in[animationkey], webkitkey = "webkit" + animationkey.replace(/^a|s|e/g, function(matchs) { |
353 |
| - return matchs.toUpperCase(); |
354 |
| - }); |
355 |
| - if (!store[pageid]) { |
356 |
| - var animateEventName = isWebkit? webkitkey: animationkey; |
357 |
| - // if it's the out element, hide it when 'animationend' |
358 |
| - index && pageInto.addEventListener(animateEventName, function() { |
359 |
| - if (this.classList.contains("in") == false) { |
360 |
| - this.style.display = "none"; |
361 |
| - } |
362 |
| - this.classList.remove(params(this).form); |
363 |
| - |
364 |
| - // add on v2.5.5 |
365 |
| - if (this.removeSelf == true) { |
366 |
| - this.parentElement.removeChild(this); |
367 |
| - this.removeSelf = null; |
368 |
| - } |
369 |
| - }); |
370 |
| - // bind animation events |
371 |
| - if (typeof animition == "string" && params_in.root[animition]) { |
372 |
| - pageInto.addEventListener(animateEventName, function() { |
373 |
| - params_in.root[animition].call(params_in.root, this, this.classList.contains("in")? "into": "out", options); |
374 |
| - }); |
375 |
| - } else if (typeof animition == "function") { |
376 |
| - pageInto.addEventListener(animateEventName, function() { |
377 |
| - animition.call(params_in.root, this, this.classList.contains("in")? "into": "out", options); |
378 |
| - }); |
379 |
| - } |
380 |
| - } |
381 |
| - }); |
| 398 | + // set animation callback for 'pageInto' |
| 399 | + fun_animationCall(pageInto, params_in); |
382 | 400 |
|
383 | 401 | // history
|
384 | 402 | // hashid should a full url address
|
|
580 | 598 | // v2.5.2
|
581 | 599 | // is back? for issues #128
|
582 | 600 | optionsTransition.back = eleOrObj.getAttribute("data-rel") == "back";
|
| 601 | + |
| 602 | + // v2.6.0 history |
| 603 | + if (eleOrObj.getAttribute("data-history") == "false") { |
| 604 | + optionsTransition.history = false; |
| 605 | + } |
583 | 606 | } else {
|
584 | 607 | response = eleOrObj.response || options.response;
|
585 | 608 | page_title = eleOrObj.title || options.title;
|
|
660 | 683 | if (typeof options.target != "undefined") {
|
661 | 684 | optionsTransition.target = options.target;
|
662 | 685 | }
|
| 686 | + if (typeof options.title != "undefined") { |
| 687 | + optionsTransition.title = options.title; |
| 688 | + } |
663 | 689 | }
|
664 | 690 | if (classPage == classPageInside) {
|
665 | 691 | optionsTransition.history = false;
|
|
756 | 782 | params.type = aOrFormOrObj.method;
|
757 | 783 |
|
758 | 784 | formData = new FormData(aOrFormOrObj);
|
759 |
| - } |
| 785 | + } else if (tagName == "a") { |
| 786 | + // v2.5.8 for issues #157 |
| 787 | + var idContainer = aOrFormOrObj.getAttribute("data-container"), |
| 788 | + classPageInside = aOrFormOrObj.getAttribute("data-classpage"), |
| 789 | + container = idContainer && document.getElementById(idContainer); |
| 790 | + if (container && classPageInside && classPageInside != Mobilebone.classPage) { |
| 791 | + // inner ajax no history change |
| 792 | + params.history = false; |
| 793 | + // title do not change |
| 794 | + params.title = false; |
| 795 | + } |
| 796 | + } |
760 | 797 |
|
761 | 798 | // get mask element
|
762 | 799 | attr_mask = aOrFormOrObj.getAttribute("data-mask");
|
|
784 | 821 |
|
785 | 822 | // do ajax
|
786 | 823 | // get mask and loading element
|
| 824 | + var body = container || document.body; |
787 | 825 | if (typeof attr_mask != "string") {
|
788 |
| - ele_mask = document.querySelector("body > ." + this.classMask); |
| 826 | + ele_mask = body.querySelector("." + this.classMask); |
789 | 827 | }
|
790 | 828 | if (ele_mask == null) {
|
791 | 829 | ele_mask = document.createElement("div");
|
|
794 | 832 | if (typeof attr_mask == "string") {
|
795 | 833 | aOrFormOrObj.appendChild(ele_mask);
|
796 | 834 | } else {
|
797 |
| - document.body.appendChild(ele_mask); |
| 835 | + body.appendChild(ele_mask); |
798 | 836 | }
|
799 | 837 | }
|
800 | 838 | // show loading
|
801 |
| - ele_mask.style.visibility = "visible"; |
| 839 | + ele_mask.style.display = "block"; |
802 | 840 |
|
803 | 841 | // ajax request
|
804 | 842 | var xhr = new XMLHttpRequest();
|
|
821 | 859 | }
|
822 | 860 | } else if (params.dataType == "unknown") {
|
823 | 861 | // ajax send by url
|
824 |
| - // no history hush |
825 |
| - // no element remove |
| 862 | + // no history hush |
826 | 863 | params.history = false;
|
827 |
| - params.remove = false; |
| 864 | + // I don't remember why add 'params.remove = false' here, |
| 865 | + // but it seems that this will cause issues #147 |
| 866 | + // no element remove |
| 867 | + // del → v2.5.8 // params.remove = false; |
828 | 868 | try {
|
829 | 869 | // as json
|
830 | 870 | response = JSON.parse(xhr.response);
|
|
849 | 889 | params.complete.call(params, xhr, xhr.status);
|
850 | 890 |
|
851 | 891 | // hide loading
|
852 |
| - ele_mask.style.visibility = "hidden"; |
| 892 | + ele_mask.style.display = "none"; |
853 | 893 | }
|
854 | 894 |
|
855 | 895 | xhr.onerror = function(e) {
|
856 | 896 | params.message = "Illegal request address or an unexpected network error!";
|
857 | 897 | params.error.call(params, xhr, xhr.status);
|
858 | 898 | // hide loading
|
859 |
| - ele_mask.style.visibility = "hidden"; |
| 899 | + ele_mask.style.display = "none"; |
860 | 900 | }
|
861 | 901 |
|
862 | 902 | xhr.ontimeout = function() {
|
863 | 903 | params.message = "The request timeout!";
|
864 | 904 | params.error.call(params, xhr, xhr.status);
|
865 | 905 | // hide loading
|
866 |
| - ele_mask.style.visibility = "hidden"; |
| 906 | + ele_mask.style.display = "none"; |
867 | 907 | };
|
868 | 908 |
|
869 | 909 | // set request header for server
|
|
1017 | 1057 | /**
|
1018 | 1058 | * If 'a' element has href, slide auto when tapping~
|
1019 | 1059 | **/
|
1020 |
| - Mobilebone.handleTapEvent = function(event) { |
| 1060 | + Mobilebone.handleTapEvent = function(event) { |
1021 | 1061 | /**
|
1022 | 1062 | // iscroll(set tap: true) may cause twice tap problem
|
1023 | 1063 | // which is none of Mobilebone's business
|
|
1033 | 1073 | }
|
1034 | 1074 | store.timerTap = Date.now();
|
1035 | 1075 | */
|
| 1076 | + var target = null; |
| 1077 | + // you can pass target as params directly |
| 1078 | + if (event && event.nodeType == 1) { |
| 1079 | + target = event; |
| 1080 | + target.preventDefault = function() {}; |
| 1081 | + } |
1036 | 1082 | // get target and href
|
1037 |
| - var target = event.target || event.touches[0], href = target.href; |
| 1083 | + target = target || event.target || event.touches[0], href = target.href; |
1038 | 1084 | if ((!href || /a/i.test(target.tagName) == false) && (target = target.getParentElementByTag("a"))) {
|
1039 | 1085 | href = target.href;
|
1040 | 1086 | }
|
|
1061 | 1107 |
|
1062 | 1108 | // if mask element exist and displaying, prevent double trigger
|
1063 | 1109 | var ele_mask = target.getElementsByClassName(Mobilebone.classMask)[0];
|
1064 |
| - if (ele_mask && ele_mask.style.visibility != "hidden") { |
| 1110 | + if (ele_mask && ele_mask.style.display != "none") { |
1065 | 1111 | event.preventDefault();
|
1066 | 1112 | return false;
|
1067 | 1113 | }
|
|
1071 | 1117 | container = idContainer && document.getElementById(idContainer);
|
1072 | 1118 | if (container && classPageInside && classPageInside != Mobilebone.classPage) {
|
1073 | 1119 | self_page = container.querySelector(".in." + classPageInside) || container.querySelector(classPageInside);
|
1074 |
| - if (self_page == null) return false; |
| 1120 | + // if (self_page == null) return false; |
1075 | 1121 | options.history = false;
|
| 1122 | + options.title = false; |
1076 | 1123 | options.classPage = classPageInside;
|
1077 | 1124 | }
|
1078 | 1125 |
|
|
1137 | 1184 | back = Mobilebone.isBack(store[clean_url], self_page);
|
1138 | 1185 | }
|
1139 | 1186 | options.id = clean_url;
|
1140 |
| - if (document.body.contains(store[clean_url]) == false) { |
1141 |
| - document.body.appendChild(store[clean_url]); |
| 1187 | + |
| 1188 | + var body = container || document.body; |
| 1189 | + |
| 1190 | + if (body.contains(store[clean_url]) == false) { |
| 1191 | + body.appendChild(store[clean_url]); |
1142 | 1192 | }
|
1143 | 1193 | Mobilebone.transition(store[clean_url], self_page, back, options);
|
1144 | 1194 | } else {
|
|
0 commit comments