Orl 短網址,供三星、福斯使用

jquery.modalLink-1.0.0.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * version 1.1.0
  3. * upgrades
  4. * - fade animation on open and close
  5. * - open method. can open modal without binding link
  6. * - possible to open modal with "POST". nessecary when need to send large amount of data to modal
  7. */
  8. (function ($) {
  9. $.modalLinkDefaults = {
  10. height: 600,
  11. width: 900,
  12. showTitle: true,
  13. showClose: true,
  14. overlayOpacity: 0.6,
  15. method: "GET", // GET, POST, REF, CLONE
  16. disableScroll: true,
  17. onHideScroll: function () { },
  18. onShowScroll: function () { }
  19. };
  20. function hideBodyScroll(cb) {
  21. var w = $("body").outerWidth();
  22. $("body").css({ overflow: "hidden" });
  23. var w2 = $("body").outerWidth();
  24. $("body").css({ width: w });
  25. if (typeof cb == "function") {
  26. var scrollbarWidth = w2 - w;
  27. cb(scrollbarWidth);
  28. }
  29. }
  30. function showBodyScroll(cb) {
  31. var $body = $("body");
  32. var w = $body.outerWidth();
  33. $body.css({ width: '', overflow: '' });
  34. var w2 = $body.outerWidth();
  35. if (typeof cb == "function") {
  36. var scrollbarWidth = w - w2;
  37. cb(scrollbarWidth);
  38. }
  39. }
  40. /**
  41. * Helper method for appending parameter to url
  42. */
  43. function addUrlParam(url, name, value) {
  44. return appendUrl(url, name + "=" + value);
  45. }
  46. /**
  47. * Hepler method for appending querystring to url
  48. */
  49. function appendUrl(url, data) {
  50. return url + (url.indexOf("?") < 0 ? "?" : "&") + data;
  51. }
  52. function buildOptions(option) {
  53. }
  54. function resolveBooleanValue($link) {
  55. for (var i = 1; i < arguments.length; i++) {
  56. var val = arguments[i];
  57. if (typeof val == "boolean") {
  58. return val;
  59. }
  60. if (typeof val == "string") {
  61. var attrValue = $link.attr(val);
  62. if (attrValue) {
  63. if (attrValue.toLowerCase() === "true") {
  64. return true;
  65. }
  66. if (attrValue.toLowerCase() === "false") {
  67. return false;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. var methods = {
  74. init: function (options) {
  75. var settings = $.extend({}, $.modalLinkDefaults);
  76. $.extend(settings, options);
  77. return this.each(function () {
  78. var $link = $(this);
  79. // already bound
  80. if ($link.hasClass("sparkling-modal-link"))
  81. return;
  82. // mark as bound
  83. $link.addClass("sparkling-modal-link");
  84. $link.click(function (e) {
  85. e.preventDefault();
  86. methods.open($link, settings);
  87. return false;
  88. });
  89. });
  90. },
  91. close: function (cb) {
  92. var $container = $(".sparkling-modal-container");
  93. var $link = $container.data("modallink");
  94. if (!$link) {
  95. return;
  96. }
  97. $link.trigger("modallink.close");
  98. var $overlay = $container.find(".sparkling-modal-overlay");
  99. var $content = $container.find(".sparkling-modal-frame");
  100. $overlay.fadeTo("fast", 0);
  101. $content.fadeTo("fast", 0, function () {
  102. $container.remove();
  103. showBodyScroll(cb);
  104. if (typeof cb == "function") {
  105. cb();
  106. }
  107. });
  108. },
  109. open: function ($link, options) {
  110. options = options || {};
  111. var url, title, showTitle, showClose, disableScroll;
  112. url = options.url || $link.attr("href");
  113. title = options.title
  114. || $link.attr("data-ml-title")
  115. || $link.attr("title")
  116. || $link.text();
  117. showTitle = resolveBooleanValue($link,
  118. options.showTitle,
  119. "data-ml-show-title",
  120. $.modalLinkDefaults.showTitle);
  121. showClose = resolveBooleanValue($link,
  122. options.showClose,
  123. "data-ml-show-close",
  124. $.modalLinkDefaults.showClose);
  125. disableScroll = resolveBooleanValue($link,
  126. options.disableScroll,
  127. "data-ml-disable-scroll",
  128. $.modalLinkDefaults.disableScroll);
  129. var settings = $.extend({}, $.modalLinkDefaults);
  130. $.extend(settings, options);
  131. var dataWidth = $link.attr("data-ml-width");
  132. if (dataWidth) {
  133. settings.width = parseInt(dataWidth);
  134. }
  135. var dataHeight = $link.attr("data-ml-height");
  136. if (dataHeight) {
  137. settings.height = parseInt(dataHeight);
  138. }
  139. if (settings.method !== "CLONE" && url.length > 0 && url[0] === "#") {
  140. settings.method = "REF";
  141. }
  142. if (settings.method == "GET" || settings.method == "POST") {
  143. url = addUrlParam(url, "__inmodal", "true");
  144. }
  145. var data = {};
  146. if (typeof settings.data != 'undefined') {
  147. if (typeof settings.data == "function") {
  148. data = settings.data();
  149. }
  150. else {
  151. data = settings.data;
  152. }
  153. }
  154. var $container = $("<div class=\"sparkling-modal-container\"></div>");
  155. $container.data("modallink", $link);
  156. var $overlay = $("<div class=\"sparkling-modal-overlay\"></div>");
  157. $overlay.css({ position: 'fixed', top: 0, left: 0, opacity: 0, width: '100%', height: '100%', zIndex: 999 });
  158. $overlay.appendTo($container);
  159. var $content = $("<div class=\"sparkling-modal-frame\"></div>")
  160. .css("opacity", 0)
  161. .css({ zIndex: 1000, position: 'fixed', display: 'inline-block' })
  162. .css({ left: '50%', marginLeft: -settings.width / 2 })
  163. .css({ top: '50%', marginTop: -settings.height / 2 })
  164. .appendTo($container);
  165. $("body").append($container);
  166. if (showTitle) {
  167. var $title = $("<div class=\"sparkling-modal-title\"></div>");
  168. $title.appendTo($content);
  169. $title.append($("<span></span>").html(title));
  170. if (showClose) {
  171. var $closeButton = $("<div class=\"sparkling-modal-close\"><div class='i-close'><div class='i-close-h' /><div class='i-close-v' /></div></div>");
  172. $closeButton.appendTo($title);
  173. $closeButton.click(methods.close);
  174. }
  175. $title.append("<div style=\"clear: both;\"></div>");
  176. }
  177. var $iframeContainer = $("<div class=\"sparkling-modal-content\"></div>");
  178. $iframeContainer.appendTo($content);
  179. var $iframe;
  180. if (settings.method == "REF") {
  181. $iframe = $("<div />");
  182. $iframe.css("overflow", "auto");
  183. var $ref = $(url);
  184. var id = "ref_" + new Date().getTime();
  185. var $ph = $("<div id='" + id + "' />");
  186. $ph.insertAfter($ref);
  187. $ref.appendTo($iframe);
  188. $link.on("modallink.close", function() {
  189. $ph.replaceWith($ref);
  190. });
  191. } else {
  192. $iframe = $("<iframe frameborder=0 id='modal-frame' name='modal-frame'></iframe>");
  193. }
  194. $iframe.appendTo($iframeContainer);
  195. $iframe.css({ width: settings.width, height: settings.height });
  196. if (settings.method == "CLONE") {
  197. console.log(url);
  198. var $inlineContent = $(url);
  199. console.log($inlineContent);
  200. var iFrameDoc = $iframe[0].contentDocument || $iframe[0].contentWindow.document;
  201. iFrameDoc.write($inlineContent.html());
  202. iFrameDoc.close();
  203. }
  204. else if (settings.method == "GET") {
  205. if (typeof data == "object") {
  206. for (var i in data) {
  207. if (data.hasOwnProperty(i)) {
  208. url = addUrlParam(url, i, data[i]);
  209. }
  210. }
  211. } else if (typeof data != "undefined") {
  212. url = appendUrl(url, data);
  213. }
  214. $iframe.attr("src", url);
  215. }
  216. $content.css({ marginTop: -($content.outerHeight(false) / 2) });
  217. $overlay.fadeTo("fast", settings.overlayOpacity);
  218. $content.fadeTo("fast", 1);
  219. if (settings.method == "POST") {
  220. var $form = settings.$form;
  221. if ($form && $form instanceof jQuery)
  222. {
  223. var originalTarget = $form.attr("target");
  224. $form
  225. .attr("target", "modal-frame")
  226. .data("submitted-from-modallink", true)
  227. .submit()
  228. }
  229. else
  230. {
  231. $form = $("<form />")
  232. .attr("action", url)
  233. .attr("method", "POST")
  234. .attr("target", "modal-frame")
  235. .css({ display: 'none'});
  236. $("<input />").attr({ type: "hidden", name: "__sparklingModalInit", value: 1 }).appendTo($form);
  237. if ($.isArray(data)) {
  238. for (var i in data) {
  239. $("<input />").attr({ type: "hidden", name: data[i].name, value: data[i].value }).appendTo($form);
  240. }
  241. }
  242. else
  243. {
  244. for (var i in data) {
  245. $("<input />").attr({ type: "hidden", name: i, value: data[i] }).appendTo($form);
  246. }
  247. }
  248. $form
  249. .appendTo("body")
  250. .submit();
  251. $form.remove();
  252. }
  253. }
  254. if (disableScroll) {
  255. hideBodyScroll(settings.onHideScroll);
  256. }
  257. }
  258. };
  259. $.fn.modalLink = function(method) {
  260. // Method calling logic
  261. if (methods[method]) {
  262. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  263. } else if (typeof method === 'object' || !method) {
  264. return methods.init.apply(this, arguments);
  265. } else {
  266. $.error('Method ' + method + ' does not exist on jQuery.modalLink');
  267. return this;
  268. }
  269. };
  270. $.modalLink = function(method) {
  271. // Method calling logic
  272. if (methods[method]) {
  273. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  274. } else if (typeof method === 'object' || !method) {
  275. return methods.init.apply(this, arguments);
  276. } else {
  277. $.error('Method ' + method + ' does not exist on jQuery.modalLink');
  278. return this;
  279. }
  280. };
  281. $.modalLink.open = function(url, options) {
  282. options = $.extend({}, options);
  283. options.url = url;
  284. methods["open"].call(this, $("<a />"), options);
  285. };
  286. })(jQuery);
  287. $(document).keyup(function(e) {
  288. if (e.keyCode == 27) {
  289. $.modalLink("close");
  290. }
  291. });