1 /* utils.js - various utils that don't belong anywhere else */ |
1 /* utils.js - various utils that don't belong anywhere else */ |
|
2 import _ from "lodash"; |
2 |
3 |
3 IriSP.jqEscape = function(_text) { |
4 export default function(IriSP) { return { |
|
5 jqEscape: function(_text) { |
4 return _text.replace(/(:|\.)/g,'\\$1'); |
6 return _text.replace(/(:|\.)/g,'\\$1'); |
5 }; |
7 }, |
6 |
8 |
7 IriSP.getLib = function(lib) { |
9 textFieldHtml: function(_text, _regexp, _extend) { |
8 if (IriSP.libFiles.useCdn && typeof IriSP.libFiles.cdn[lib] == "string") { |
|
9 return IriSP.libFiles.cdn[lib]; |
|
10 } |
|
11 if (typeof IriSP.libFiles.locations[lib] == "string") { |
|
12 return IriSP.libFiles.locations[lib]; |
|
13 } |
|
14 if (typeof IriSP.libFiles.inDefaultDir[lib] == "string") { |
|
15 return IriSP.libFiles.defaultDir + '/' + IriSP.libFiles.inDefaultDir[lib]; |
|
16 } |
|
17 }; |
|
18 |
|
19 IriSP._cssCache = []; |
|
20 |
|
21 IriSP.loadCss = function(_cssFile) { |
|
22 if (IriSP._(IriSP._cssCache).indexOf(_cssFile) === -1) { |
|
23 IriSP.jQuery("<link>", { |
|
24 rel : "stylesheet", |
|
25 type : "text/css", |
|
26 href : _cssFile |
|
27 }).appendTo('head'); |
|
28 IriSP._cssCache.push(_cssFile); |
|
29 } |
|
30 }; |
|
31 |
|
32 IriSP.textFieldHtml = function(_text, _regexp, _extend) { |
|
33 var list = [], |
10 var list = [], |
34 positions = [], |
11 positions = [], |
35 text = _text.replace(/(^\s+|\s+$)/g,''); |
12 text = _text.replace(/(^\s+|\s+$)/g,''); |
36 |
13 |
37 function addToList(_rx, _startHtml, _endHtml) { |
14 function addToList(_rx, _startHtml, _endHtml) { |
64 return '<a href="http://twitter.com/' + matches[1] + '" target="_blank">'; |
41 return '<a href="http://twitter.com/' + matches[1] + '" target="_blank">'; |
65 }, '</a>'); |
42 }, '</a>'); |
66 addToList(/\*[^*]+\*/gm, '<b>', '</b>'); |
43 addToList(/\*[^*]+\*/gm, '<b>', '</b>'); |
67 addToList(/[\n\r]+/gm, '', '<br />'); |
44 addToList(/[\n\r]+/gm, '', '<br />'); |
68 |
45 |
69 IriSP._(_extend).each(function(x) { |
46 _(_extend).each(function(x) { |
70 addToList.apply(null, x); |
47 addToList.apply(null, x); |
71 }); |
48 }); |
72 |
49 |
73 positions = IriSP._(positions) |
50 positions = _(positions) |
74 .chain() |
51 .chain() |
75 .uniq() |
52 .uniq() |
76 .sortBy(function(p) { return parseInt(p); }) |
53 .sortBy(function(p) { return parseInt(p); }) |
77 .value(); |
54 .value(); |
78 |
55 |
98 |
75 |
99 res += text.substring(lastIndex); |
76 res += text.substring(lastIndex); |
100 |
77 |
101 return res; |
78 return res; |
102 |
79 |
103 }; |
80 }, |
104 |
81 |
105 IriSP.log = function() { |
82 log: function() { |
106 if (typeof console !== "undefined" && typeof IriSP.logging !== "undefined" && IriSP.logging) { |
83 if (typeof console !== "undefined" && typeof IriSP.logging !== "undefined" && IriSP.logging) { |
107 console.log.apply(console, arguments); |
84 console.log.apply(console, arguments); |
108 } |
85 } |
109 }; |
86 }, |
110 |
87 |
111 IriSP.attachDndData = function(jqSel, data) { |
88 attachDndData: function(jqSel, data) { |
112 jqSel.attr("draggable", "true").on("dragstart", function(_event) { |
89 jqSel.attr("draggable", "true").on("dragstart", function(_event) { |
113 var d = (typeof data === "function" ? data.call(this) : data); |
90 var d = (typeof data === "function" ? data.call(this) : data); |
114 try { |
91 try { |
115 if (d.html === undefined && d.uri && d.text) { |
92 if (d.html === undefined && d.uri && d.text) { |
116 d.html = '<a href="' + d.uri + '">' + d.text + '</a>'; |
93 d.html = '<a href="' + d.uri + '">' + d.text + '</a>'; |
117 } |
94 } |
118 IriSP._(d).each(function(v, k) { |
95 _(d).each(function(v, k) { |
119 if (v && k != 'text' && k != 'html') { |
96 if (v && k != 'text' && k != 'html') { |
120 _event.originalEvent.dataTransfer.setData("text/x-iri-" + k, v); |
97 _event.originalEvent.dataTransfer.setData("text/x-iri-" + k, v); |
121 } |
98 } |
122 }); |
99 }); |
123 if (d.uri && d.text) { |
100 if (d.uri && d.text) { |
135 } |
112 } |
136 } catch(err) { |
113 } catch(err) { |
137 _event.originalEvent.dataTransfer.setData("Text", JSON.stringify(d)); |
114 _event.originalEvent.dataTransfer.setData("Text", JSON.stringify(d)); |
138 } |
115 } |
139 }); |
116 }); |
140 }; |
117 }, |
141 |
118 |
142 IriSP.FakeClass = function(properties) { |
119 FakeClass: function(properties) { |
143 var _this = this, |
120 var _this = this, |
144 noop = (function() {}); |
121 noop = (function() {}); |
145 IriSP._(properties).each(function(p) { |
122 _(properties).each(function(p) { |
146 _this[p] = noop; |
123 _this[p] = noop; |
147 }); |
124 }); |
148 }; |
125 }, |
149 |
126 |
150 IriSP.timestamp2ms = function(t) { |
127 timestamp2ms: function(t) { |
151 // Convert timestamp to numeric value |
128 // Convert timestamp to numeric value |
152 // It accepts the following forms: |
129 // It accepts the following forms: |
153 // [h:mm:ss] [mm:ss] [ss] |
130 // [h:mm:ss] [mm:ss] [ss] |
154 var s = t.split(":").reverse(); |
131 var s = t.split(":").reverse(); |
155 while (s.length < 3) { |
132 while (s.length < 3) { |
156 s.push("0"); |
133 s.push("0"); |
157 } |
134 } |
158 return 1000 * (3600 * parseInt(s[2], 10) + 60 * parseInt(s[1], 10) + parseInt(s[0], 10)); |
135 return 1000 * (3600 * parseInt(s[2], 10) + 60 * parseInt(s[1], 10) + parseInt(s[0], 10)); |
159 }; |
136 }, |
160 |
137 |
161 IriSP.setFullScreen= function(elem, value) { |
138 setFullScreen: function(elem, value) { |
162 // Set fullscreen on or off |
139 // Set fullscreen on or off |
163 if (value) { |
140 if (value) { |
164 if (elem.requestFullscreen) { |
141 if (elem.requestFullscreen) { |
165 elem.requestFullscreen(); |
142 elem.requestFullscreen(); |
166 } else if (elem.mozRequestFullScreen) { |
143 } else if (elem.mozRequestFullScreen) { |
179 document.mozCancelFullScreen(); |
156 document.mozCancelFullScreen(); |
180 } else if (document.webkitExitFullscreen) { |
157 } else if (document.webkitExitFullscreen) { |
181 document.webkitExitFullscreen(); |
158 document.webkitExitFullscreen(); |
182 } |
159 } |
183 } |
160 } |
184 }; |
161 }, |
185 |
162 |
186 IriSP.isFullscreen = function() { |
163 isFullscreen: function() { |
187 return (document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement); |
164 return (document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement); |
188 }; |
165 }, |
189 |
166 |
190 IriSP.getFullscreenElement = function () { |
167 getFullscreenElement: function () { |
191 return (document.fullscreenElement |
168 return (document.fullscreenElement |
192 || document.webkitFullscreenElement |
169 || document.webkitFullscreenElement |
193 || document.mozFullScreenElement |
170 || document.mozFullScreenElement |
194 || document.msFullscreenElement |
171 || document.msFullscreenElement |
195 || undefined); |
172 || undefined); |
196 }; |
173 }, |
197 |
174 |
198 IriSP.getFullscreenEventname = function () { |
175 getFullscreenEventname: function () { |
199 return ((document.exitFullscreen && "fullscreenchange") |
176 return ((document.exitFullscreen && "fullscreenchange") |
200 || (document.webkitExitFullscreen && "webkitfullscreenchange") |
177 || (document.webkitExitFullscreen && "webkitfullscreenchange") |
201 || (document.mozExitFullScreen && "mozfullscreenchange") |
178 || (document.mozExitFullScreen && "mozfullscreenchange") |
202 || (document.msExitFullscreen && "msfullscreenchange") |
179 || (document.msExitFullscreen && "msfullscreenchange") |
203 || ""); |
180 || ""); |
204 }; |
181 }, |
|
182 }}; |