Blame view

bower_components/prism/plugins/jsonp-highlight/prism-jsonp-highlight.js 4.1 KB
eb240478   Luigi Serra   public room cards...
1
  (function() {
f748e9cf   Luigi Serra   new controllet an...
2
  	if ( !self.Prism || !self.document || !document.querySelectorAll || ![].filter) return;
eb240478   Luigi Serra   public room cards...
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  
  	var adapters = [];
  	function registerAdapter(adapter) {
  		if (typeof adapter === "function" && !getAdapter(adapter)) {
  			adapters.push(adapter);
  		}
  	}
  	function getAdapter(adapter) {
  		if (typeof adapter === "function") {
  			return adapters.filter(function(fn) { return fn.valueOf() === adapter.valueOf()})[0];
  		}
  		else if (typeof adapter === "string" && adapter.length > 0) {
  			return adapters.filter(function(fn) { return fn.name === adapter})[0];
  		}
  		return null;
  	}
  	function removeAdapter(adapter) {
  		if (typeof adapter === "string")
  			adapter = getAdapter(adapter);
  		if (typeof adapter === "function") {
  			var index = adapters.indexOf(adapter);
  			if (index >=0) {
  				adapters.splice(index,1);
  			}
  		}
  	}
  
  	Prism.plugins.jsonphighlight = {
  		registerAdapter: registerAdapter,
  		removeAdapter: removeAdapter,
  		highlight: highlight
  	};
  	registerAdapter(function github(rsp, el) {
  		if ( rsp && rsp.meta && rsp.data ) {
  			if ( rsp.meta.status && rsp.meta.status >= 400 ) {
  				return "Error: " + ( rsp.data.message || rsp.meta.status );
  			}
  			else if ( typeof(rsp.data.content) === "string" ) {
  				return typeof(atob) === "function"
  					? atob(rsp.data.content.replace(/\s/g, ""))
  					: "Your browser cannot decode base64";
  			}
  		}
  		return null;
  	});
  	registerAdapter(function gist(rsp, el) {
  		if ( rsp && rsp.meta && rsp.data && rsp.data.files ) {
  			if ( rsp.meta.status && rsp.meta.status >= 400 ) {
  				return "Error: " + ( rsp.data.message || rsp.meta.status );
  			}
  			else {
  				var filename = el.getAttribute("data-filename");
  				if (filename == null) {
  					// Maybe in the future we can somehow render all files
  					// But the standard <script> include for gists does that nicely already,
  					// so that might be getting beyond the scope of this plugin
  					for (var key in rsp.data.files) {
  						if (rsp.data.files.hasOwnProperty(key)) {
  							filename = key;
  							break;
  						}
  					}
  				}
  				if (rsp.data.files[filename] !== undefined) {
  					return rsp.data.files[filename].content;
  				}
  				else {
  					return "Error: unknown or missing gist file " + filename;
  				}
  			}
  		}
  		return null;
  	});
  	registerAdapter(function bitbucket(rsp, el) {
  		return rsp && rsp.node && typeof(rsp.data) === "string"
  			? rsp.data
  			: null;
  	});
  
  	var jsonpcb = 0,
  	    loadstr = "Loading…";
  
  	function highlight() {
  		Array.prototype.slice.call(document.querySelectorAll("pre[data-jsonp]")).forEach(function(pre) {
  			pre.textContent = "";
  
  			var code = document.createElement("code");
  			code.textContent = loadstr;
  			pre.appendChild(code);
  
  			var adapterfn = pre.getAttribute("data-adapter");
  			var adapter = null;
  			if ( adapterfn ) {
  				if ( typeof(window[adapterfn]) === "function" ) {
  					adapter = window[adapterfn];
  				}
  				else {
  					code.textContent = "JSONP adapter function '" + adapterfn + "' doesn't exist";
  					return;
  				}
  			}
  
  			var cb = "prismjsonp" + ( jsonpcb++ );
  			
  			var uri = document.createElement("a");
  			var src = uri.href = pre.getAttribute("data-jsonp");
  			uri.href += ( uri.search ? "&" : "?" ) + ( pre.getAttribute("data-callback") || "callback" ) + "=" + cb;
  
  			var timeout = setTimeout(function() {
  				// we could clean up window[cb], but if the request finally succeeds, keeping it around is a good thing
  				if ( code.textContent === loadstr )
  					code.textContent = "Timeout loading '" + src + "'";
  			}, 5000);
  			
  			var script = document.createElement("script");
  			script.src = uri.href;
  
  			window[cb] = function(rsp) {
  				document.head.removeChild(script);
  				clearTimeout(timeout);
  				delete window[cb];
  
  				var data = "";
  				
  				if ( adapter ) {
  					data = adapter(rsp, pre);
  				}
  				else {
  					for ( var p in adapters ) {
  						data = adapters[p](rsp, pre);
  						if ( data !== null ) break;
  					}
  				}
  
  				if (data === null) {
  					code.textContent = "Cannot parse response (perhaps you need an adapter function?)";
  				}
  				else {
  					code.textContent = data;
  					Prism.highlightElement(code);
  				}
  			};
  
  			document.head.appendChild(script);
  		});
  	}
  
  	highlight();
  })();