common.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. var supporttouch = "ontouchend" in document;
  2. !supporttouch && (window.location.href = 'forum.php?mobile=1');
  3. var platform = navigator.platform;
  4. var ua = navigator.userAgent;
  5. var ios = /iPhone|iPad|iPod/.test(platform) && ua.indexOf( "AppleWebKit" ) > -1;
  6. var andriod = ua.indexOf( "Android" ) > -1;
  7. (function($, window, document, undefined) {
  8. var dataPropertyName = "virtualMouseBindings",
  9. touchTargetPropertyName = "virtualTouchID",
  10. virtualEventNames = "vmouseover vmousedown vmousemove vmouseup vclick vmouseout vmousecancel".split(" "),
  11. touchEventProps = "clientX clientY pageX pageY screenX screenY".split( " " ),
  12. mouseHookProps = $.event.mouseHooks ? $.event.mouseHooks.props : [],
  13. mouseEventProps = $.event.props.concat( mouseHookProps ),
  14. activeDocHandlers = {},
  15. resetTimerID = 0,
  16. startX = 0,
  17. startY = 0,
  18. didScroll = false,
  19. clickBlockList = [],
  20. blockMouseTriggers = false,
  21. blockTouchTriggers = false,
  22. eventCaptureSupported = "addEventListener" in document,
  23. $document = $(document),
  24. nextTouchID = 1,
  25. lastTouchID = 0, threshold;
  26. $.vmouse = {
  27. moveDistanceThreshold: 10,
  28. clickDistanceThreshold: 10,
  29. resetTimerDuration: 1500
  30. };
  31. function getNativeEvent(event) {
  32. while( event && typeof event.originalEvent !== "undefined" ) {
  33. event = event.originalEvent;
  34. }
  35. return event;
  36. }
  37. function createVirtualEvent(event, eventType) {
  38. var t = event.type, oe, props, ne, prop, ct, touch, i, j, len;
  39. event = $.Event(event);
  40. event.type = eventType;
  41. oe = event.originalEvent;
  42. props = $.event.props;
  43. if(t.search(/^(mouse|click)/) > -1 ) {
  44. props = mouseEventProps;
  45. }
  46. if(oe) {
  47. for(i = props.length, prop; i;) {
  48. prop = props[ --i ];
  49. event[ prop ] = oe[ prop ];
  50. }
  51. }
  52. if(t.search(/mouse(down|up)|click/) > -1 && !event.which) {
  53. event.which = 1;
  54. }
  55. if(t.search(/^touch/) !== -1) {
  56. ne = getNativeEvent(oe);
  57. t = ne.touches;
  58. ct = ne.changedTouches;
  59. touch = (t && t.length) ? t[0] : (( ct && ct.length) ? ct[0] : undefined);
  60. if(touch) {
  61. for(j = 0, len = touchEventProps.length; j < len; j++) {
  62. prop = touchEventProps[j];
  63. event[prop] = touch[prop];
  64. }
  65. }
  66. }
  67. return event;
  68. }
  69. function getVirtualBindingFlags(element) {
  70. var flags = {},
  71. b, k;
  72. while(element) {
  73. b = $.data(element, dataPropertyName);
  74. for(k in b) {
  75. if(b[k]) {
  76. flags[k] = flags.hasVirtualBinding = true;
  77. }
  78. }
  79. element = element.parentNode;
  80. }
  81. return flags;
  82. }
  83. function getClosestElementWithVirtualBinding(element, eventType) {
  84. var b;
  85. while(element) {
  86. b = $.data( element, dataPropertyName );
  87. if(b && (!eventType || b[eventType])) {
  88. return element;
  89. }
  90. element = element.parentNode;
  91. }
  92. return null;
  93. }
  94. function enableTouchBindings() {
  95. blockTouchTriggers = false;
  96. }
  97. function disableTouchBindings() {
  98. blockTouchTriggers = true;
  99. }
  100. function enableMouseBindings() {
  101. lastTouchID = 0;
  102. clickBlockList.length = 0;
  103. blockMouseTriggers = false;
  104. disableTouchBindings();
  105. }
  106. function disableMouseBindings() {
  107. enableTouchBindings();
  108. }
  109. function startResetTimer() {
  110. clearResetTimer();
  111. resetTimerID = setTimeout(function() {
  112. resetTimerID = 0;
  113. enableMouseBindings();
  114. }, $.vmouse.resetTimerDuration);
  115. }
  116. function clearResetTimer() {
  117. if(resetTimerID ) {
  118. clearTimeout(resetTimerID);
  119. resetTimerID = 0;
  120. }
  121. }
  122. function triggerVirtualEvent(eventType, event, flags) {
  123. var ve;
  124. if((flags && flags[eventType]) ||
  125. (!flags && getClosestElementWithVirtualBinding(event.target, eventType))) {
  126. ve = createVirtualEvent(event, eventType);
  127. $(event.target).trigger(ve);
  128. }
  129. return ve;
  130. }
  131. function mouseEventCallback(event) {
  132. var touchID = $.data(event.target, touchTargetPropertyName);
  133. if(!blockMouseTriggers && (!lastTouchID || lastTouchID !== touchID)) {
  134. var ve = triggerVirtualEvent("v" + event.type, event);
  135. if(ve) {
  136. if(ve.isDefaultPrevented()) {
  137. event.preventDefault();
  138. }
  139. if(ve.isPropagationStopped()) {
  140. event.stopPropagation();
  141. }
  142. if(ve.isImmediatePropagationStopped()) {
  143. event.stopImmediatePropagation();
  144. }
  145. }
  146. }
  147. }
  148. function handleTouchStart(event) {
  149. var touches = getNativeEvent(event).touches,
  150. target, flags;
  151. if(touches && touches.length === 1) {
  152. target = event.target;
  153. flags = getVirtualBindingFlags(target);
  154. if(flags.hasVirtualBinding) {
  155. lastTouchID = nextTouchID++;
  156. $.data(target, touchTargetPropertyName, lastTouchID);
  157. clearResetTimer();
  158. disableMouseBindings();
  159. didScroll = false;
  160. var t = getNativeEvent(event).touches[0];
  161. startX = t.pageX;
  162. startY = t.pageY;
  163. triggerVirtualEvent("vmouseover", event, flags);
  164. triggerVirtualEvent("vmousedown", event, flags);
  165. }
  166. }
  167. }
  168. function handleScroll(event) {
  169. if(blockTouchTriggers) {
  170. return;
  171. }
  172. if(!didScroll) {
  173. triggerVirtualEvent("vmousecancel", event, getVirtualBindingFlags(event.target));
  174. }
  175. didScroll = true;
  176. startResetTimer();
  177. }
  178. function handleTouchMove(event) {
  179. if(blockTouchTriggers) {
  180. return;
  181. }
  182. var t = getNativeEvent(event).touches[0],
  183. didCancel = didScroll,
  184. moveThreshold = $.vmouse.moveDistanceThreshold,
  185. flags = getVirtualBindingFlags(event.target);
  186. didScroll = didScroll ||
  187. (Math.abs(t.pageX - startX) > moveThreshold ||
  188. Math.abs(t.pageY - startY) > moveThreshold);
  189. if(didScroll && !didCancel) {
  190. triggerVirtualEvent("vmousecancel", event, flags);
  191. }
  192. triggerVirtualEvent("vmousemove", event, flags);
  193. startResetTimer();
  194. }
  195. function handleTouchEnd(event) {
  196. if(blockTouchTriggers) {
  197. return;
  198. }
  199. disableTouchBindings();
  200. var flags = getVirtualBindingFlags(event.target), t;
  201. triggerVirtualEvent("vmouseup", event, flags);
  202. if(!didScroll) {
  203. var ve = triggerVirtualEvent("vclick", event, flags);
  204. if(ve && ve.isDefaultPrevented()) {
  205. t = getNativeEvent(event).changedTouches[0];
  206. clickBlockList.push({
  207. touchID: lastTouchID,
  208. x: t.clientX,
  209. y: t.clientY
  210. });
  211. blockMouseTriggers = true;
  212. }
  213. }
  214. triggerVirtualEvent("vmouseout", event, flags);
  215. didScroll = false;
  216. startResetTimer();
  217. }
  218. function hasVirtualBindings(ele) {
  219. var bindings = $.data( ele, dataPropertyName ), k;
  220. if(bindings) {
  221. for(k in bindings) {
  222. if(bindings[k]) {
  223. return true;
  224. }
  225. }
  226. }
  227. return false;
  228. }
  229. function dummyMouseHandler() {}
  230. function getSpecialEventObject(eventType) {
  231. var realType = eventType.substr(1);
  232. return {
  233. setup: function(data, namespace) {
  234. if(!hasVirtualBindings(this)) {
  235. $.data(this, dataPropertyName, {});
  236. }
  237. var bindings = $.data(this, dataPropertyName);
  238. bindings[eventType] = true;
  239. activeDocHandlers[eventType] = (activeDocHandlers[eventType] || 0) + 1;
  240. if(activeDocHandlers[eventType] === 1) {
  241. $document.bind(realType, mouseEventCallback);
  242. }
  243. $(this).bind(realType, dummyMouseHandler);
  244. if(eventCaptureSupported) {
  245. activeDocHandlers["touchstart"] = (activeDocHandlers["touchstart"] || 0) + 1;
  246. if(activeDocHandlers["touchstart"] === 1) {
  247. $document.bind("touchstart", handleTouchStart)
  248. .bind("touchend", handleTouchEnd)
  249. .bind("touchmove", handleTouchMove)
  250. .bind("scroll", handleScroll);
  251. }
  252. }
  253. },
  254. teardown: function(data, namespace) {
  255. --activeDocHandlers[eventType];
  256. if(!activeDocHandlers[eventType]) {
  257. $document.unbind(realType, mouseEventCallback);
  258. }
  259. if(eventCaptureSupported) {
  260. --activeDocHandlers["touchstart"];
  261. if(!activeDocHandlers["touchstart"]) {
  262. $document.unbind("touchstart", handleTouchStart)
  263. .unbind("touchmove", handleTouchMove)
  264. .unbind("touchend", handleTouchEnd)
  265. .unbind("scroll", handleScroll);
  266. }
  267. }
  268. var $this = $(this),
  269. bindings = $.data(this, dataPropertyName);
  270. if(bindings) {
  271. bindings[eventType] = false;
  272. }
  273. $this.unbind(realType, dummyMouseHandler);
  274. if(!hasVirtualBindings(this)) {
  275. $this.removeData(dataPropertyName);
  276. }
  277. }
  278. };
  279. }
  280. for(var i = 0; i < virtualEventNames.length; i++) {
  281. $.event.special[virtualEventNames[i]] = getSpecialEventObject(virtualEventNames[i]);
  282. }
  283. if(eventCaptureSupported) {
  284. document.addEventListener("click", function(e) {
  285. var cnt = clickBlockList.length,
  286. target = e.target,
  287. x, y, ele, i, o, touchID;
  288. if(cnt) {
  289. x = e.clientX;
  290. y = e.clientY;
  291. threshold = $.vmouse.clickDistanceThreshold;
  292. ele = target;
  293. while(ele) {
  294. for(i = 0; i < cnt; i++) {
  295. o = clickBlockList[i];
  296. touchID = 0;
  297. if((ele === target && Math.abs(o.x - x) < threshold && Math.abs(o.y - y) < threshold) ||
  298. $.data(ele, touchTargetPropertyName) === o.touchID) {
  299. e.preventDefault();
  300. e.stopPropagation();
  301. return;
  302. }
  303. }
  304. ele = ele.parentNode;
  305. }
  306. }
  307. }, true);
  308. }
  309. })(jQuery, window, document);
  310. (function($, window, undefined) {
  311. function triggercustomevent(obj, eventtype, event) {
  312. var origtype = event.type;
  313. event.type = eventtype;
  314. $.event.handle.call(obj, event);
  315. event.type = origtype;
  316. }
  317. $.event.special.tap = {
  318. setup : function() {
  319. var thisobj = this;
  320. var obj = $(thisobj);
  321. obj.on('vmousedown', function(e) {
  322. if(e.which && e.which !== 1) {
  323. return false;
  324. }
  325. var origtarget = e.target;
  326. var origevent = e.originalEvent;
  327. var timer;
  328. function cleartaptimer() {
  329. clearTimeout(timer);
  330. }
  331. function cleartaphandlers() {
  332. cleartaptimer();
  333. obj.off('vclick', clickhandler)
  334. .off('vmouseup', cleartaptimer);
  335. $(document).off('vmousecancel', cleartaphandlers);
  336. }
  337. function clickhandler(e) {
  338. cleartaphandlers();
  339. if(origtarget === e.target) {
  340. triggercustomevent(thisobj, 'tap', e);
  341. }
  342. return false;
  343. }
  344. obj.on('vmouseup', cleartaptimer)
  345. .on('vclick', clickhandler)
  346. $(document).on('touchcancel', cleartaphandlers);
  347. timer = setTimeout(function() {
  348. triggercustomevent(thisobj, 'taphold', $.Event('taphold', {target:origtarget}));
  349. }, 750);
  350. return false;
  351. });
  352. }
  353. };
  354. $.each(('tap').split(' '), function(index, name) {
  355. $.fn[name] = function(fn) {
  356. return this.on(name, fn);
  357. };
  358. });
  359. })(jQuery, this);
  360. var page = {
  361. converthtml : function() {
  362. var prevpage = $('div.pg .prev').prop('href');
  363. var nextpage = $('div.pg .nxt').prop('href');
  364. var lastpage = $('div.pg label span').text().replace(/[^\d]/g, '') || 0;
  365. var curpage = $('div.pg input').val() || 1;
  366. if(!lastpage) {
  367. prevpage = $('div.pg .pgb a').prop('href');
  368. }
  369. var prevpagehref = nextpagehref = '';
  370. if(prevpage == undefined) {
  371. prevpagehref = 'javascript:;" class="grey';
  372. } else {
  373. prevpagehref = prevpage;
  374. }
  375. if(nextpage == undefined) {
  376. nextpagehref = 'javascript:;" class="grey';
  377. } else {
  378. nextpagehref = nextpage;
  379. }
  380. var selector = '';
  381. if(lastpage) {
  382. selector += '<a id="select_a" style="margin:0 2px;padding:1px 0 0 0;border:0;display:inline-block;position:relative;width:100px;height:31px;line-height:27px;background:url('+STATICURL+'/image/mobile/images/pic_select.png) no-repeat;text-align:left;text-indent:20px;">';
  383. selector += '<select id="dumppage" style="position:absolute;left:0;top:0;height:27px;opacity:0;width:100px;">';
  384. for(var i=1; i<=lastpage; i++) {
  385. selector += '<option value="'+i+'" '+ (i == curpage ? 'selected' : '') +'>第'+i+'页</option>';
  386. }
  387. selector += '</select>';
  388. selector += '<span>第'+curpage+'页</span>';
  389. }
  390. $('div.pg').removeClass('pg').addClass('page').html('<a href="'+ prevpagehref +'">上一页</a>'+ selector +'<a href="'+ nextpagehref +'">下一页</a>');
  391. $('#dumppage').on('change', function() {
  392. var href = (prevpage || nextpage);
  393. window.location.href = href.replace(/page=\d+/, 'page=' + $(this).val());
  394. });
  395. },
  396. };
  397. var scrolltop = {
  398. obj : null,
  399. init : function(obj) {
  400. scrolltop.obj = obj;
  401. var fixed = this.isfixed();
  402. obj.css('opacity', '.618');
  403. if(fixed) {
  404. obj.css('bottom', '8px');
  405. } else {
  406. obj.css({'visibility':'visible', 'position':'absolute'});
  407. }
  408. $(window).on('resize', function() {
  409. if(fixed) {
  410. obj.css('bottom', '8px');
  411. } else {
  412. obj.css('top', ($(document).scrollTop() + $(window).height() - 40) + 'px');
  413. }
  414. });
  415. obj.on('tap', function() {
  416. $(document).scrollTop($(document).height());
  417. });
  418. $(document).on('scroll', function() {
  419. if(!fixed) {
  420. obj.css('top', ($(document).scrollTop() + $(window).height() - 40) + 'px');
  421. }
  422. if($(document).scrollTop() >= 400) {
  423. obj.removeClass('bottom')
  424. .off().on('tap', function() {
  425. window.scrollTo('0', '1');
  426. });
  427. } else {
  428. obj.addClass('bottom')
  429. .off().on('tap', function() {
  430. $(document).scrollTop($(document).height());
  431. });
  432. }
  433. });
  434. },
  435. isfixed : function() {
  436. var offset = scrolltop.obj.offset();
  437. var scrollTop = $(window).scrollTop();
  438. var screenHeight = document.documentElement.clientHeight;
  439. if(offset == undefined) {
  440. return false;
  441. }
  442. if(offset.top < scrollTop || (offset.top - scrollTop) > screenHeight) {
  443. return false;
  444. } else {
  445. return true;
  446. }
  447. }
  448. };
  449. var img = {
  450. init : function(is_err_t) {
  451. var errhandle = this.errorhandle;
  452. $('img').on('load', function() {
  453. var obj = $(this);
  454. obj.attr('zsrc', obj.attr('src'));
  455. if(obj.width() < 5 && obj.height() < 10 && obj.css('display') != 'none') {
  456. return errhandle(obj, is_err_t);
  457. }
  458. obj.css('display', 'inline');
  459. obj.css('visibility', 'visible');
  460. if(obj.width() > window.innerWidth) {
  461. obj.css('width', window.innerWidth);
  462. }
  463. obj.parent().find('.loading').remove();
  464. obj.parent().find('.error_text').remove();
  465. })
  466. .on('error', function() {
  467. var obj = $(this);
  468. obj.attr('zsrc', obj.attr('src'));
  469. errhandle(obj, is_err_t);
  470. });
  471. },
  472. errorhandle : function(obj, is_err_t) {
  473. if(obj.attr('noerror') == 'true') {
  474. return;
  475. }
  476. obj.css('visibility', 'hidden');
  477. obj.css('display', 'none');
  478. var parentnode = obj.parent();
  479. parentnode.find('.loading').remove();
  480. parentnode.append('<div class="loading" style="background:url('+ IMGDIR +'/imageloading.gif) no-repeat center center;width:'+parentnode.width()+'px;height:'+parentnode.height()+'px"></div>');
  481. var loadnums = parseInt(obj.attr('load')) || 0;
  482. if(loadnums < 3) {
  483. obj.attr('src', obj.attr('zsrc'));
  484. obj.attr('load', ++loadnums);
  485. return false;
  486. }
  487. if(is_err_t) {
  488. var parentnode = obj.parent();
  489. parentnode.find('.loading').remove();
  490. parentnode.append('<div class="error_text">点击重新加载</div>');
  491. parentnode.find('.error_text').one('click', function() {
  492. obj.attr('load', 0).find('.error_text').remove();
  493. parentnode.append('<div class="loading" style="background:url('+ IMGDIR +'/imageloading.gif) no-repeat center center;width:'+parentnode.width()+'px;height:'+parentnode.height()+'px"></div>');
  494. obj.attr('src', obj.attr('zsrc'));
  495. });
  496. }
  497. return false;
  498. }
  499. };
  500. var atap = {
  501. init : function() {
  502. $('.atap').on('tap', function() {
  503. var obj = $(this);
  504. obj.css({'background':'#6FACD5', 'color':'#FFFFFF', 'font-weight':'bold', 'text-decoration':'none', 'text-shadow':'0 1px 1px #3373A5'});
  505. return false;
  506. });
  507. $('.atap a').off('click');
  508. }
  509. };
  510. var POPMENU = new Object;
  511. var popup = {
  512. init : function() {
  513. var $this = this;
  514. $('.popup').each(function(index, obj) {
  515. obj = $(obj);
  516. var pop = $(obj.attr('href'));
  517. if(pop && pop.attr('popup')) {
  518. pop.css({'display':'none'});
  519. obj.on('click', function(e) {
  520. $this.open(pop);
  521. });
  522. }
  523. });
  524. this.maskinit();
  525. },
  526. maskinit : function() {
  527. var $this = this;
  528. $('#mask').off().on('tap', function() {
  529. $this.close();
  530. });
  531. },
  532. open : function(pop, type, url) {
  533. this.close();
  534. this.maskinit();
  535. if(typeof pop == 'string') {
  536. $('#ntcmsg').remove();
  537. if(type == 'alert') {
  538. pop = '<div class="tip"><dt>'+ pop +'</dt><dd><input class="button2" type="button" value="确定" onclick="popup.close();"></dd></div>'
  539. } else if(type == 'confirm') {
  540. pop = '<div class="tip"><dt>'+ pop +'</dt><dd><input class="redirect button2" type="button" value="确定" href="'+ url +'"><a href="javascript:;" onclick="popup.close();">取消</a></dd></div>'
  541. }
  542. $('body').append('<div id="ntcmsg" style="display:none;">'+ pop +'</div>');
  543. pop = $('#ntcmsg');
  544. }
  545. if(POPMENU[pop.attr('id')]) {
  546. $('#' + pop.attr('id') + '_popmenu').html(pop.html()).css({'height':pop.height()+'px', 'width':pop.width()+'px'});
  547. } else {
  548. pop.parent().append('<div class="dialogbox" id="'+ pop.attr('id') +'_popmenu" style="height:'+ pop.height() +'px;width:'+ pop.width() +'px;">'+ pop.html() +'</div>');
  549. }
  550. var popupobj = $('#' + pop.attr('id') + '_popmenu');
  551. var left = (window.innerWidth - popupobj.width()) / 2;
  552. var top = (document.documentElement.clientHeight - popupobj.height()) / 2;
  553. popupobj.css({'display':'block','position':'fixed','left':left,'top':top,'z-index':120,'opacity':1});
  554. $('#mask').css({'display':'block','width':'100%','height':'100%','position':'fixed','top':'0','left':'0','background':'black','opacity':'0.2','z-index':'100'});
  555. POPMENU[pop.attr('id')] = pop;
  556. },
  557. close : function() {
  558. $('#mask').css('display', 'none');
  559. $.each(POPMENU, function(index, obj) {
  560. $('#' + index + '_popmenu').css('display','none');
  561. });
  562. }
  563. };
  564. var dialog = {
  565. init : function() {
  566. $(document).on('click', '.dialog', function() {
  567. var obj = $(this);
  568. popup.open('<img src="' + IMGDIR + '/imageloading.gif">');
  569. $.ajax({
  570. type : 'GET',
  571. url : obj.attr('href') + '&inajax=1',
  572. dataType : 'xml'
  573. })
  574. .success(function(s) {
  575. popup.open(s.lastChild.firstChild.nodeValue);
  576. evalscript(s.lastChild.firstChild.nodeValue);
  577. })
  578. .error(function() {
  579. window.location.href = obj.attr('href');
  580. popup.close();
  581. });
  582. return false;
  583. });
  584. },
  585. };
  586. var formdialog = {
  587. init : function() {
  588. $(document).on('click', '.formdialog', function() {
  589. popup.open('<img src="' + IMGDIR + '/imageloading.gif">');
  590. var obj = $(this);
  591. var formobj = $(this.form);
  592. $.ajax({
  593. type:'POST',
  594. url:formobj.attr('action') + '&handlekey='+ formobj.attr('id') +'&inajax=1',
  595. data:formobj.serialize(),
  596. dataType:'xml'
  597. })
  598. .success(function(s) {
  599. popup.open(s.lastChild.firstChild.nodeValue);
  600. evalscript(s.lastChild.firstChild.nodeValue);
  601. })
  602. .error(function() {
  603. window.location.href = obj.attr('href');
  604. popup.close();
  605. });
  606. return false;
  607. });
  608. }
  609. };
  610. var redirect = {
  611. init : function() {
  612. $(document).on('click', '.redirect', function() {
  613. var obj = $(this);
  614. popup.close();
  615. window.location.href = obj.attr('href');
  616. });
  617. }
  618. };
  619. var DISMENU = new Object;
  620. var display = {
  621. init : function() {
  622. var $this = this;
  623. $('.display').each(function(index, obj) {
  624. obj = $(obj);
  625. var dis = $(obj.attr('href'));
  626. if(dis && dis.attr('display')) {
  627. dis.css({'display':'none'});
  628. dis.css({'z-index':'102'});
  629. DISMENU[dis.attr('id')] = dis;
  630. obj.on('click', function(e) {
  631. if(in_array(e.target.tagName, ['A', 'IMG', 'INPUT'])) return;
  632. $this.maskinit();
  633. if(dis.attr('display') == 'true') {
  634. dis.css('display', 'block');
  635. dis.attr('display', 'false');
  636. $('#mask').css({'display':'block','width':'100%','height':'100%','position':'fixed','top':'0','left':'0','background':'transparent','z-index':'100'});
  637. }
  638. return false;
  639. });
  640. }
  641. });
  642. },
  643. maskinit : function() {
  644. var $this = this;
  645. $('#mask').off().on('touchstart', function() {
  646. $this.hide();
  647. });
  648. },
  649. hide : function() {
  650. $('#mask').css('display', 'none');
  651. $.each(DISMENU, function(index, obj) {
  652. obj.css('display', 'none');
  653. obj.attr('display', 'true');
  654. });
  655. }
  656. };
  657. var geo = {
  658. latitude : null,
  659. longitude : null,
  660. loc : null,
  661. errmsg : null,
  662. timeout : 5000,
  663. getcurrentposition : function() {
  664. if(!!navigator.geolocation) {
  665. navigator.geolocation.getCurrentPosition(this.locationsuccess, this.locationerror, {
  666. enableHighAcuracy : true,
  667. timeout : this.timeout,
  668. maximumAge : 3000
  669. });
  670. }
  671. },
  672. locationerror : function(error) {
  673. geo.errmsg = 'error';
  674. switch(error.code) {
  675. case error.TIMEOUT:
  676. geo.errmsg = "获取位置超时,请重试";
  677. break;
  678. case error.POSITION_UNAVAILABLE:
  679. geo.errmsg = '无法检测到您的当前位置';
  680. break;
  681. case error.PERMISSION_DENIED:
  682. geo.errmsg = '请允许能够正常访问您的当前位置';
  683. break;
  684. case error.UNKNOWN_ERROR:
  685. geo.errmsg = '发生未知错误';
  686. break;
  687. }
  688. },
  689. locationsuccess : function(position) {
  690. geo.latitude = position.coords.latitude;
  691. geo.longitude = position.coords.longitude;
  692. geo.errmsg = '';
  693. $.ajax({
  694. type:'POST',
  695. url:'http://maps.google.com/maps/api/geocode/json?latlng=' + geo.latitude + ',' + geo.longitude + '&language=zh-CN&sensor=true',
  696. dataType:'json'
  697. })
  698. .success(function(s) {
  699. if(s.status == 'OK') {
  700. geo.loc = s.results[0].formatted_address;
  701. }
  702. })
  703. .error(function() {
  704. geo.loc = null;
  705. });
  706. }
  707. };
  708. var pullrefresh = {
  709. init : function() {
  710. var pos = {};
  711. var status = false;
  712. var divobj = null;
  713. var contentobj = null;
  714. var reloadflag = false;
  715. $('body').on('touchstart', function(e) {
  716. e = mygetnativeevent(e);
  717. pos.startx = e.touches[0].pageX;
  718. pos.starty = e.touches[0].pageY;
  719. })
  720. .on('touchmove', function(e) {
  721. e = mygetnativeevent(e);
  722. pos.curposx = e.touches[0].pageX;
  723. pos.curposy = e.touches[0].pageY;
  724. if(pos.curposy - pos.starty < 0 && !status) {
  725. return;
  726. }
  727. if(!status && $(window).scrollTop() <= 0) {
  728. status = true;
  729. divobj = document.createElement('div');
  730. divobj = $(divobj);
  731. divobj.css({'position':'relative', 'margin-left':'-85px'});
  732. $('body').prepend(divobj);
  733. contentobj = document.createElement('div');
  734. contentobj = $(contentobj);
  735. contentobj.css({'position':'absolute', 'height':'30px', 'top': '-30px', 'left':'50%'});
  736. contentobj.html('<img src="'+ STATICURL + 'image/mobile/images/icon_arrow.gif" style="vertical-align:middle;margin-right:5px;-moz-transform:rotate(180deg);-webkit-transform:rotate(180deg);-o-transform:rotate(180deg);transform:rotate(180deg);"><span id="refreshtxt">下拉可以刷新</span>');
  737. contentobj.find('img').css({'-webkit-transition':'all 0.5s ease-in-out'});
  738. divobj.prepend(contentobj);
  739. pos.topx = pos.curposx;
  740. pos.topy = pos.curposy;
  741. }
  742. if(!status) {
  743. return;
  744. }
  745. if(status == true) {
  746. var pullheight = pos.curposy - pos.topy;
  747. if(pullheight >= 0 && pullheight < 150) {
  748. divobj.css({'height': pullheight/2 + 'px'});
  749. contentobj.css({'top': (-30 + pullheight/2) + 'px'});
  750. if(reloadflag) {
  751. contentobj.find('img').css({'-webkit-transform':'rotate(180deg)', '-moz-transform':'rotate(180deg)', '-o-transform':'rotate(180deg)', 'transform':'rotate(180deg)'});
  752. contentobj.find('#refreshtxt').html('下拉可以刷新');
  753. }
  754. reloadflag = false;
  755. } else if(pullheight >= 150) {
  756. divobj.css({'height':pullheight/2 + 'px'});
  757. contentobj.css({'top': (-30 + pullheight/2) + 'px'});
  758. if(!reloadflag) {
  759. contentobj.find('img').css({'-webkit-transform':'rotate(360deg)', '-moz-transform':'rotate(360deg)', '-o-transform':'rotate(360deg)', 'transform':'rotate(360deg)'});
  760. contentobj.find('#refreshtxt').html('松开可以刷新');
  761. }
  762. reloadflag = true;
  763. }
  764. }
  765. e.preventDefault();
  766. })
  767. .on('touchend', function(e) {
  768. if(status == true) {
  769. if(reloadflag) {
  770. contentobj.html('<img src="'+ STATICURL + 'image/mobile/images/icon_load.gif" style="vertical-align:middle;margin-right:5px;">正在加载...');
  771. contentobj.animate({'top': (-30 + 75) + 'px'}, 618, 'linear');
  772. divobj.animate({'height': '75px'}, 618, 'linear', function() {
  773. window.location.reload();
  774. });
  775. return;
  776. }
  777. }
  778. divobj.remove();
  779. divobj = null;
  780. status = false;
  781. pos = {};
  782. });
  783. }
  784. };
  785. function mygetnativeevent(event) {
  786. while(event && typeof event.originalEvent !== "undefined") {
  787. event = event.originalEvent;
  788. }
  789. return event;
  790. }
  791. function evalscript(s) {
  792. if(s.indexOf('<script') == -1) return s;
  793. var p = /<script[^\>]*?>([^\x00]*?)<\/script>/ig;
  794. var arr = [];
  795. while(arr = p.exec(s)) {
  796. var p1 = /<script[^\>]*?src=\"([^\>]*?)\"[^\>]*?(reload=\"1\")?(?:charset=\"([\w\-]+?)\")?><\/script>/i;
  797. var arr1 = [];
  798. arr1 = p1.exec(arr[0]);
  799. if(arr1) {
  800. appendscript(arr1[1], '', arr1[2], arr1[3]);
  801. } else {
  802. p1 = /<script(.*?)>([^\x00]+?)<\/script>/i;
  803. arr1 = p1.exec(arr[0]);
  804. appendscript('', arr1[2], arr1[1].indexOf('reload=') != -1);
  805. }
  806. }
  807. return s;
  808. }
  809. var safescripts = {}, evalscripts = [];
  810. function appendscript(src, text, reload, charset) {
  811. var id = hash(src + text);
  812. if(!reload && in_array(id, evalscripts)) return;
  813. if(reload && $('#' + id)[0]) {
  814. $('#' + id)[0].parentNode.removeChild($('#' + id)[0]);
  815. }
  816. evalscripts.push(id);
  817. var scriptNode = document.createElement("script");
  818. scriptNode.type = "text/javascript";
  819. scriptNode.id = id;
  820. scriptNode.charset = charset ? charset : (!document.charset ? document.characterSet : document.charset);
  821. try {
  822. if(src) {
  823. scriptNode.src = src;
  824. scriptNode.onloadDone = false;
  825. scriptNode.onload = function () {
  826. scriptNode.onloadDone = true;
  827. JSLOADED[src] = 1;
  828. };
  829. scriptNode.onreadystatechange = function () {
  830. if((scriptNode.readyState == 'loaded' || scriptNode.readyState == 'complete') && !scriptNode.onloadDone) {
  831. scriptNode.onloadDone = true;
  832. JSLOADED[src] = 1;
  833. }
  834. };
  835. } else if(text){
  836. scriptNode.text = text;
  837. }
  838. document.getElementsByTagName('head')[0].appendChild(scriptNode);
  839. } catch(e) {}
  840. }
  841. function hash(string, length) {
  842. var length = length ? length : 32;
  843. var start = 0;
  844. var i = 0;
  845. var result = '';
  846. filllen = length - string.length % length;
  847. for(i = 0; i < filllen; i++){
  848. string += "0";
  849. }
  850. while(start < string.length) {
  851. result = stringxor(result, string.substr(start, length));
  852. start += length;
  853. }
  854. return result;
  855. }
  856. function stringxor(s1, s2) {
  857. var s = '';
  858. var hash = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  859. var max = Math.max(s1.length, s2.length);
  860. for(var i=0; i<max; i++) {
  861. var k = s1.charCodeAt(i) ^ s2.charCodeAt(i);
  862. s += hash.charAt(k % 52);
  863. }
  864. return s;
  865. }
  866. function in_array(needle, haystack) {
  867. if(typeof needle == 'string' || typeof needle == 'number') {
  868. for(var i in haystack) {
  869. if(haystack[i] == needle) {
  870. return true;
  871. }
  872. }
  873. }
  874. return false;
  875. }
  876. function isUndefined(variable) {
  877. return typeof variable == 'undefined' ? true : false;
  878. }
  879. function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
  880. if(cookieValue == '' || seconds < 0) {
  881. cookieValue = '';
  882. seconds = -2592000;
  883. }
  884. if(seconds) {
  885. var expires = new Date();
  886. expires.setTime(expires.getTime() + seconds * 1000);
  887. }
  888. domain = !domain ? cookiedomain : domain;
  889. path = !path ? cookiepath : path;
  890. document.cookie = escape(cookiepre + cookieName) + '=' + escape(cookieValue)
  891. + (expires ? '; expires=' + expires.toGMTString() : '')
  892. + (path ? '; path=' + path : '/')
  893. + (domain ? '; domain=' + domain : '')
  894. + (secure ? '; secure' : '');
  895. }
  896. function getcookie(name, nounescape) {
  897. name = cookiepre + name;
  898. var cookie_start = document.cookie.indexOf(name);
  899. var cookie_end = document.cookie.indexOf(";", cookie_start);
  900. if(cookie_start == -1) {
  901. return '';
  902. } else {
  903. var v = document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length));
  904. return !nounescape ? unescape(v) : v;
  905. }
  906. }
  907. $(document).ready(function() {
  908. if($('div.pg').length > 0) {
  909. page.converthtml();
  910. }
  911. if($('.scrolltop').length > 0) {
  912. scrolltop.init($('.scrolltop'));
  913. }
  914. if($('img').length > 0) {
  915. img.init(1);
  916. }
  917. if($('.popup').length > 0) {
  918. popup.init();
  919. }
  920. if($('.display').length > 0) {
  921. display.init();
  922. }
  923. if($('.atap').length > 0) {
  924. atap.init();
  925. }
  926. if($('.pullrefresh').length > 0) {
  927. pullrefresh.init();
  928. }
  929. dialog.init();
  930. formdialog.init();
  931. redirect.init();
  932. });