PageRenderTime 33ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/static/estate/js/common.js

https://gitlab.com/colin.luo/shbs
JavaScript | 352 lines | 347 code | 0 blank | 5 comment | 87 complexity | b6a1b359e930a033f407080db16411cf MD5 | raw file
  1. var FCAPP = FCAPP || {
  2. Common: {
  3. RUNTIME: {
  4. loadImg: {},
  5. records: 0
  6. },
  7. init: function() {
  8. FCAPP.Common.setInfo();
  9. FCAPP.Common.hotReport();
  10. FCAPP.Common.initElements();
  11. },
  12. initElements: function() {
  13. var R = FCAPP.Common.RUNTIME;
  14. R.popTips = $('#popTips');
  15. R.tipsTitle = $('#tipsTitle');
  16. R.tipsMsg = $('#tipsMsg');
  17. R.tipsOK = $('#tipsOK');
  18. R.tipsCancel = $('#tipsCancel');
  19. R.popMask = $('#popMask');
  20. },
  21. format: function(tpl, data) {
  22. for (var i in data) {
  23. var key = i,
  24. val = data[i],
  25. reg = new RegExp('\\\{' + key + '\\\}', 'g');
  26. val = val.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  27. tpl = tpl.replace(reg, val);
  28. tpl = tpl.replace(/&lt;\/?br&gt;/gi, '<br>');
  29. }
  30. return tpl;
  31. },
  32. replaceAll: function(str, regexp, replacement) {
  33. var pattern = new RegExp(regexp, "gm");
  34. var tmp = str.replace(pattern, replacement);
  35. pattern = null;
  36. return tmp;
  37. },
  38. resizeLayout: function(floatTips) {
  39. var w = window.innerWidth,
  40. h = window.innerHeight;
  41. if (w > h) {
  42. floatTips.css('top', '20%');
  43. } else {
  44. floatTips.css('top', '30%');
  45. }
  46. },
  47. saveCookie: function(key, val, seconds) {
  48. var dt = new Date(),
  49. seconds = parseInt(seconds);
  50. seconds = isNaN(seconds) ? 180 : seconds;
  51. dt.setTime(dt.getTime() + seconds * 1000);
  52. document.cookie = [encodeURIComponent(key), '=', encodeURIComponent(val), '; expires=', dt.toGMTString(), '; domain=trade.qq.com; path=/fangchan/'].join('');
  53. },
  54. removeCookie: function(key) {
  55. document.cookie = encodeURIComponent(key) + '=; expires=Thu, 01 Jan 1970 16:00:00 GMT; domain=trade.qq.com; path=/fangchan/';
  56. },
  57. getCookie: function(key) {
  58. var cookies = document.cookie.split('; ');
  59. for (var i = 0,
  60. l = cookies.length; i < l; i++) {
  61. var parts = cookies[i].split('=');
  62. if (parts.shift() === decodeURIComponent(key)) {
  63. return decodeURIComponent(parts.shift());
  64. }
  65. }
  66. return '';
  67. },
  68. hideToolbar: function() {
  69. try {
  70. WeixinJSBridge.invoke('hideToolbar');
  71. } catch(e) {
  72. setTimeout(FCAPP.Common.hideToolbar, 30);
  73. }
  74. },
  75. hideLoading: function() {
  76. var R = FCAPP.Common.RUNTIME;
  77. if (!R.loading) {
  78. R.loading = $('#popFail');
  79. }
  80. if (R.loading) {
  81. R.loading.hide();
  82. }
  83. },
  84. showLoading: function() {
  85. var R = FCAPP.Common.RUNTIME;
  86. if (!R.loading) {
  87. R.loading = $('#popFail');
  88. }
  89. if (R.loading) {
  90. R.loading.show();
  91. }
  92. },
  93. loadImg: function(src, id, callback, force) {
  94. var R = FCAPP.Common.RUNTIME,
  95. loadImg = R.loadImg,
  96. chk = loadImg[id + src],
  97. img;
  98. if (!force && !!chk && (chk.loaded || chk.loading)) {
  99. return;
  100. }
  101. loadImg[id + src] = {
  102. id: id,
  103. loading: true,
  104. loaded: false,
  105. dom: false
  106. };
  107. img = new Image();
  108. img.idx = id;
  109. if (callback && typeof(callback) == 'function') {
  110. img.cb = callback;
  111. }
  112. img.onload = img.onerror = img.onreadystatechange = function() {
  113. if ( !! this.readyState && this.readyState != 4) {
  114. return;
  115. }
  116. var info = loadImg[this.idx + this.src],
  117. oimg,
  118. bw = document.documentElement.clientWidth,
  119. bh = document.documentElement.clientHeight;
  120. info.loaded = true;
  121. if ( !! info.dom) {
  122. oimg = info.dom;
  123. } else {
  124. oimg = document.getElementById(this.idx);
  125. info.dom = oimg;
  126. }
  127. if (!oimg.parentNode) {
  128. return;
  129. }
  130. if ( !! this.cb) {
  131. this.cb(this);
  132. delete this.cb;
  133. } else {
  134. this.width = bw;
  135. this.height = bh;
  136. }
  137. oimg.parentNode.replaceChild(this, oimg);
  138. this.onload = null;
  139. delete this.onload;
  140. };
  141. img.src = src;
  142. },
  143. updateShareData: function(data) {
  144. var ph = location.pathname.split('/'),
  145. page = ph[ph.length - 1].split('.')[0];
  146. if (page == '') {
  147. page = 'index';
  148. }
  149. window.shareData = window.shareData || {};
  150. for (var i in data) {
  151. if (typeof(data[i]) == 'object') {
  152. continue;
  153. }
  154. shareData[i] = data[i];
  155. }
  156. if (data[page]) {
  157. for (var i in data[page]) {
  158. shareData[i] = data[page][i];
  159. }
  160. }
  161. if (shareData.descKeep) {
  162. shareData.desc = shareData.descKeep;
  163. }
  164. if (shareData.linkKeep) {
  165. shareData.link = shareData.linkKeep;
  166. }
  167. if (window.gQuery && gQuery.qrcode && /^\w\d+$/i.test(gQuery.qrcode)) {
  168. shareData.qrcode = gQuery.qrcode;
  169. }
  170. },
  171. jumpTo: function(page, param, obj) {
  172. var arr = location.pathname.split('/'),
  173. hash = '',
  174. search = '';
  175. for (var i in param) {
  176. if (i.indexOf('#') == 0) {
  177. hash = i + '=' + param[i];
  178. } else {
  179. gQuery[i] = encodeURIComponent(param[i]);
  180. }
  181. }
  182. search = $.param(gQuery);
  183. if (obj && typeof(obj) != 'boolean') {
  184. obj.href = page + '?' + search + hash;
  185. } else {
  186. location.href = page + '?' + search + hash;
  187. }
  188. },
  189. setInfo: function() {
  190. var search = location.search ? location.search.substr(1) : '',
  191. hash = location.hash ? location.hash.substr(1) : '';
  192. window.gQuery = window.gHash = {};
  193. if (search) {
  194. window.gQuery = this.split(search);
  195. }
  196. if (hash) {
  197. window.gHash = this.split(hash);
  198. }
  199. },
  200. split: function(str) {
  201. var arr = str.split('&'),
  202. obj = {};
  203. if (arr.length < 1) {
  204. return obj;
  205. }
  206. for (var i = 0,
  207. il = arr.length; i < il; i++) {
  208. var pair = arr[i].split('=');
  209. if (pair.length == 2 && pair[0].length) {
  210. obj[pair[0]] = decodeURIComponent(pair[1]);
  211. }
  212. }
  213. return obj;
  214. },
  215. escapeHTML: function(str) {
  216. if (typeof(str) == 'string' || str instanceof String) {
  217. str = str.toString().replace(/<+/gi, '&lt;').replace(/>+/gi, '&gt;');
  218. str = str.replace(/&lt;strong&gt;/gi, '<strong>').replace(/&lt;\/strong&gt;/gi, '</strong>');
  219. str = str.replace(/&lt;br&gt;/gi, '<br/>').replace(/&lt;\/br&gt;/gi, '<br/>');
  220. if (str.indexOf('电话') != -1 && /[\d\-]{8,11}/.test(str)) {
  221. str = str.replace(/(\d[\d\-]+\d)/g, '<a style="color:#74a3a5" href="tel:$1">$1</a>');
  222. }
  223. return str;
  224. } else {
  225. return str;
  226. }
  227. },
  228. timer: function(seconds, id) {
  229. var totalSeconds = seconds,
  230. oneday = 3600 * 24,
  231. day = 0,
  232. hour = 0,
  233. min = 0,
  234. sec = 0,
  235. str = '',
  236. $con = $('#' + id),
  237. interval = setInterval(function() {
  238. totalSeconds--;
  239. if (totalSeconds > 0) {
  240. day = Math.floor(totalSeconds / oneday);
  241. hour = Math.floor((totalSeconds % oneday) / 3600);
  242. min = Math.floor((totalSeconds % 3600) / 60);
  243. sec = totalSeconds % 60;
  244. day = day < 10 ? '0' + day: day;
  245. hour = hour < 10 ? '0' + hour: hour;
  246. min = min < 10 ? '0' + min: min;
  247. sec = sec < 10 ? '0' + sec: sec;
  248. str = '<p><em>' + day + '</em>天<em>' + hour + '</em>小时<em>' + min + '</em>分<em>' + sec + '</em>秒</p>';
  249. $con.html(str);
  250. } else {
  251. clearInterval(interval);
  252. }
  253. },
  254. 1000);
  255. },
  256. addData2URL: function(url, data) {
  257. var param = $.param(data);
  258. url += url.indexOf('?') != -1 ? '&' + param: '?' + param;
  259. return url;
  260. },
  261. hotReport: function() {
  262. var fcount = 0;
  263. setTimeout(function() {
  264. if (typeof(pgvMain) == 'function') {
  265. pgvMain();
  266. } else {
  267. fcount++;
  268. if (fcount > 5) {
  269. return;
  270. }
  271. setTimeout(arguments.callee, 1000);
  272. }
  273. },
  274. 1000);
  275. },
  276. escTpl: function(str) {
  277. str = str.replace(/<%\s*=\s*([\w+\[\.\]]+)%>/gmi, "<%=FCAPP.Common.escapeHTML($1)%>");
  278. return str;
  279. },
  280. loadShareData: function(id) {
  281. //var dt = new Date();
  282. //$.ajax({
  283. //url: 'http://trade.qq.com/fangchan/static/' + (id.length ? id + '.': '') + 'sharedata.js?' + dt.getMonth() + dt.getDate(),
  284. //dataType: 'jsonp'
  285. //});
  286. },
  287. msg: function(boo, obj) {
  288. var R = FCAPP.Common.RUNTIME,
  289. title = '温馨提示',
  290. msg = '';
  291. if (!boo) {
  292. R.popTips.hide();
  293. if (R.popMask.length) {
  294. R.popMask.hide();
  295. }
  296. return;
  297. }
  298. if (!R.popTips.length || !obj.msg) {
  299. return;
  300. }
  301. if (obj.title) {
  302. title = FCAPP.Common.escapeHTML(obj.title);
  303. }
  304. R.tipsTitle.html(title);
  305. msg = FCAPP.Common.escapeHTML(obj.msg);
  306. R.tipsMsg.html(msg);
  307. if (obj.ok && typeof(obj.ok) == 'function') {
  308. R.tipsOK.one('click',
  309. function() {
  310. obj.ok.apply(null, obj.okParams || []);
  311. R.popTips.hide();
  312. if (R.popMask.length) {
  313. R.popMask.hide();
  314. }
  315. });
  316. } else {
  317. R.tipsOK.one('click',
  318. function() {
  319. R.popTips.hide();
  320. if (R.popMask.length) {
  321. R.popMask.hide();
  322. }
  323. });
  324. }
  325. if (obj.no && typeof(obj.no) == 'function') {
  326. R.tipsCancel.show();
  327. R.tipsCancel.one('click',
  328. function() {
  329. obj.no.apply(null, obj.noParams || []);
  330. R.popTips.hide();
  331. if (R.popMask.length) {
  332. R.popMask.hide();
  333. }
  334. });
  335. } else {
  336. R.tipsCancel.one('click',
  337. function() {
  338. R.popTips.hide();
  339. if (R.popMask.length) {
  340. R.popMask.hide();
  341. }
  342. });
  343. }
  344. if (R.popMask.length){
  345. R.popMask.show();
  346. }
  347. R.popTips.show();
  348. }
  349. }
  350. };
  351. window.updateShareData = FCAPP.Common.updateShareData;
  352. $(document).ready(FCAPP.Common.init);