URI.fragmentURI.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Extending URI.js for fragment abuse
  3. */
  4. // --------------------------------------------------------------------------------
  5. // EXAMPLE: storing a relative URL in the fragment ("FragmentURI")
  6. // possibly helpful when working with backbone.js or sammy.js
  7. // inspired by https://github.com/medialize/URI.js/pull/2
  8. // --------------------------------------------------------------------------------
  9. // Note: make sure this is the last file loaded!
  10. // USAGE:
  11. // var uri = URI("http://example.org/#!/foo/bar/baz.html");
  12. // var furi = uri.fragment(true);
  13. // furi.pathname() === '/foo/bar/baz.html';
  14. // furi.pathname('/hello.html');
  15. // uri.toString() === "http://example.org/#!/hello.html"
  16. (function (root, factory) {
  17. 'use strict';
  18. // https://github.com/umdjs/umd/blob/master/returnExports.js
  19. if (typeof module === 'object' && module.exports) {
  20. // Node
  21. module.exports = factory(require('./URI'));
  22. } else if (typeof define === 'function' && define.amd) {
  23. // AMD. Register as an anonymous module.
  24. define(['./URI'], factory);
  25. } else {
  26. // Browser globals (root is window)
  27. factory(root.URI);
  28. }
  29. }(this, function (URI) {
  30. 'use strict';
  31. var p = URI.prototype;
  32. // old handlers we need to wrap
  33. var f = p.fragment;
  34. var b = p.build;
  35. // make fragmentPrefix configurable
  36. URI.fragmentPrefix = '!';
  37. var _parts = URI._parts;
  38. URI._parts = function() {
  39. var parts = _parts();
  40. parts.fragmentPrefix = URI.fragmentPrefix;
  41. return parts;
  42. };
  43. p.fragmentPrefix = function(v) {
  44. this._parts.fragmentPrefix = v;
  45. return this;
  46. };
  47. // add fragment(true) and fragment(URI) signatures
  48. p.fragment = function(v, build) {
  49. var prefix = this._parts.fragmentPrefix;
  50. var fragment = this._parts.fragment || '';
  51. var furi;
  52. if (v === true) {
  53. if (fragment.substring(0, prefix.length) !== prefix) {
  54. furi = URI('');
  55. } else {
  56. furi = new URI(fragment.substring(prefix.length));
  57. }
  58. this._fragmentURI = furi;
  59. furi._parentURI = this;
  60. return furi;
  61. } else if (v !== undefined && typeof v !== 'string') {
  62. this._fragmentURI = v;
  63. v._parentURI = v;
  64. this._parts.fragment = prefix + v.toString();
  65. this.build(!build);
  66. return this;
  67. } else if (typeof v === 'string') {
  68. this._fragmentURI = undefined;
  69. }
  70. return f.call(this, v, build);
  71. };
  72. // make .build() of the actual URI aware of the FragmentURI
  73. p.build = function(deferBuild) {
  74. var t = b.call(this, deferBuild);
  75. if (deferBuild !== false && this._parentURI) {
  76. // update the parent
  77. this._parentURI.fragment(this);
  78. }
  79. return t;
  80. };
  81. // extending existing object rather than defining something new
  82. return URI;
  83. }));