aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/URI.js/src/URI.fragmentURI.js
blob: 7fb85fe3bca3304fbd1525e8e1a2275575953bb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Extending URI.js for fragment abuse
 */

// --------------------------------------------------------------------------------
// EXAMPLE: storing a relative URL in the fragment ("FragmentURI")
// possibly helpful when working with backbone.js or sammy.js
// inspired by https://github.com/medialize/URI.js/pull/2
// --------------------------------------------------------------------------------

// Note: make sure this is the last file loaded!

// USAGE:
// var uri = URI("http://example.org/#!/foo/bar/baz.html");
// var furi = uri.fragment(true);
// furi.pathname() === '/foo/bar/baz.html';
// furi.pathname('/hello.html');
// uri.toString() === "http://example.org/#!/hello.html"

(function (root, factory) {
  'use strict';
  // https://github.com/umdjs/umd/blob/master/returnExports.js
  if (typeof exports === 'object') {
    // Node
    module.exports = factory(require('./URI'));
  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['./URI'], factory);
  } else {
    // Browser globals (root is window)
    factory(root.URI);
  }
}(this, function (URI) {
  'use strict';

  var p = URI.prototype;
  // old handlers we need to wrap
  var f = p.fragment;
  var b = p.build;

  // make fragmentPrefix configurable
  URI.fragmentPrefix = '!';
  var _parts = URI._parts;
  URI._parts = function() {
    var parts = _parts();
    parts.fragmentPrefix = URI.fragmentPrefix;
    return parts;
  };
  p.fragmentPrefix = function(v) {
    this._parts.fragmentPrefix = v;
    return this;
  };

  // add fragment(true) and fragment(URI) signatures  
  p.fragment = function(v, build) {
    var prefix = this._parts.fragmentPrefix;
    var fragment = this._parts.fragment || '';
    var furi;

    if (v === true) {
      if (fragment.substring(0, prefix.length) !== prefix) {
        furi = URI('');
      } else {
        furi = new URI(fragment.substring(prefix.length));
      }
    
      this._fragmentURI = furi;
      furi._parentURI = this;
      return furi;
    } else if (v !== undefined && typeof v !== 'string') {
      this._fragmentURI = v;
      v._parentURI = v;
      this._parts.fragment = prefix + v.toString();
      this.build(!build);
      return this;
    } else if (typeof v === 'string') {
      this._fragmentURI = undefined;
    }

    return f.call(this, v, build);
  };

  // make .build() of the actual URI aware of the FragmentURI
  p.build = function(deferBuild) {
    var t = b.call(this, deferBuild);
  
    if (deferBuild !== false && this._parentURI) {
      // update the parent
      this._parentURI.fragment(this);
    }

    return t;
  };

  // extending existing object rather than defining something new
  return URI;
}));