jquery.redirect.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. jQuery Redirect v1.1.3
  3. Copyright (c) 2013-2018 Miguel Galante
  4. Copyright (c) 2011-2013 Nemanja Avramovic, www.avramovic.info
  5. Licensed under CC BY-SA 4.0 License: http://creativecommons.org/licenses/by-sa/4.0/
  6. This means everyone is allowed to:
  7. Share - copy and redistribute the material in any medium or format
  8. Adapt - remix, transform, and build upon the material for any purpose, even commercially.
  9. Under following conditions:
  10. Attribution - You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  11. ShareAlike - If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
  12. */
  13. ;
  14. (function ($) {
  15. 'use strict';
  16. //Defaults configuration
  17. var defaults = {
  18. url: null,
  19. values: null,
  20. method: "POST",
  21. target: null,
  22. traditional: false,
  23. redirectTop: false
  24. };
  25. /**
  26. * jQuery Redirect
  27. * @param {string} url - Url of the redirection
  28. * @param {Object} values - (optional) An object with the data to send. If not present will look for values as QueryString in the target url.
  29. * @param {string} method - (optional) The HTTP verb can be GET or POST (defaults to POST)
  30. * @param {string} target - (optional) The target of the form. "_blank" will open the url in a new window.
  31. * @param {boolean} traditional - (optional) This provides the same function as jquery's ajax function. The brackets are omitted on the field name if its an array. This allows arrays to work with MVC.net among others.
  32. * @param {boolean} redirectTop - (optional) If its called from a iframe, force to navigate the top window.
  33. */
  34. /**
  35. * jQuery Redirect
  36. * @param {string} opts - Options object
  37. * @param {string} opts.url - Url of the redirection
  38. * @param {Object} opts.values - (optional) An object with the data to send. If not present will look for values as QueryString in the target url.
  39. * @param {string} opts.method - (optional) The HTTP verb can be GET or POST (defaults to POST)
  40. * @param {string} opts.target - (optional) The target of the form. "_blank" will open the url in a new window.
  41. * @param {boolean} opts.traditional - (optional) This provides the same function as jquery's ajax function. The brackets are omitted on the field name if its an array. This allows arrays to work with MVC.net among others.
  42. * @param {boolean} opts.redirectTop - (optional) If its called from a iframe, force to navigate the top window.
  43. */
  44. $.redirect = function (url, values, method, target, traditional, redirectTop) {
  45. var opts = url;
  46. if (typeof url !== "object") {
  47. var opts = {
  48. url: url,
  49. values: values,
  50. method: method,
  51. target: target,
  52. traditional: traditional,
  53. redirectTop: redirectTop
  54. };
  55. }
  56. var config = $.extend({}, defaults, opts);
  57. var generatedForm = $.redirect.getForm(config.url, config.values, config.method, config.target, config.traditional);
  58. $('body', config.redirectTop ? window.top.document : undefined).append(generatedForm.form);
  59. generatedForm.submit();
  60. generatedForm.form.remove();
  61. };
  62. $.redirect.getForm = function (url, values, method, target, traditional) {
  63. method = (method && ["GET", "POST", "PUT", "DELETE"].indexOf(method.toUpperCase()) !== -1) ? method.toUpperCase() : 'POST';
  64. url = url.split("#");
  65. var hash = url[1] ? ("#" + url[1]) : "";
  66. url = url[0];
  67. if (!values) {
  68. var obj = $.parseUrl(url);
  69. url = obj.url;
  70. values = obj.params;
  71. }
  72. values = removeNulls(values);
  73. var form = $('<form>')
  74. .attr("method", method)
  75. .attr("action", url + hash);
  76. if (target) {
  77. form.attr("target", target);
  78. }
  79. var submit = form[0].submit;
  80. iterateValues(values, [], form, null, traditional);
  81. return {
  82. form: form,
  83. submit: function () {
  84. submit.call(form[0]);
  85. }
  86. };
  87. }
  88. //Utility Functions
  89. /**
  90. * Url and QueryString Parser.
  91. * @param {string} url - a Url to parse.
  92. * @returns {object} an object with the parsed url with the following structure {url: URL, params:{ KEY: VALUE }}
  93. */
  94. $.parseUrl = function (url) {
  95. if (url.indexOf('?') === -1) {
  96. return {
  97. url: url,
  98. params: {}
  99. };
  100. }
  101. var parts = url.split('?'),
  102. query_string = parts[1],
  103. elems = query_string.split('&');
  104. url = parts[0];
  105. var i, pair, obj = {};
  106. for (i = 0; i < elems.length; i += 1) {
  107. pair = elems[i].split('=');
  108. obj[pair[0]] = pair[1];
  109. }
  110. return {
  111. url: url,
  112. params: obj
  113. };
  114. };
  115. //Private Functions
  116. var getInput = function (name, value, parent, array, traditional) {
  117. var parentString;
  118. if (parent.length > 0) {
  119. parentString = parent[0];
  120. var i;
  121. for (i = 1; i < parent.length; i += 1) {
  122. parentString += "[" + parent[i] + "]";
  123. }
  124. if (array) {
  125. if (traditional)
  126. name = parentString;
  127. else
  128. name = parentString + "[" + name + "]";
  129. } else {
  130. name = parentString + "[" + name + "]";
  131. }
  132. }
  133. return $("<input>").attr("type", "hidden")
  134. .attr("name", name)
  135. .attr("value", value);
  136. };
  137. var iterateValues = function (values, parent, form, isArray, traditional) {
  138. var i, iterateParent = [];
  139. Object.keys(values).forEach(function (i) {
  140. if (typeof values[i] === "object") {
  141. iterateParent = parent.slice();
  142. iterateParent.push(i);
  143. iterateValues(values[i], iterateParent, form, Array.isArray(values[i]), traditional);
  144. } else {
  145. form.append(getInput(i, values[i], parent, isArray, traditional));
  146. }
  147. });
  148. };
  149. var removeNulls = function (values) {
  150. var propNames = Object.getOwnPropertyNames(values);
  151. for (var i = 0; i < propNames.length; i++) {
  152. var propName = propNames[i];
  153. if (values[propName] === null || values[propName] === undefined) {
  154. delete values[propName];
  155. } else if (typeof values[propName] === 'object') {
  156. values[propName] = removeNulls(values[propName]);
  157. } else if (values[propName].length < 1) {
  158. delete values[propName];
  159. }
  160. }
  161. return values;
  162. };
  163. }(window.jQuery || window.Zepto || window.jqlite));