all.js 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  1. /* ---- Class.coffee ---- */
  2. (function() {
  3. var Class,
  4. slice = [].slice;
  5. Class = (function() {
  6. function Class() {}
  7. Class.prototype.trace = true;
  8. Class.prototype.log = function() {
  9. var args;
  10. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  11. if (!this.trace) {
  12. return;
  13. }
  14. if (typeof console === 'undefined') {
  15. return;
  16. }
  17. args.unshift("[" + this.constructor.name + "]");
  18. console.log.apply(console, args);
  19. return this;
  20. };
  21. Class.prototype.logStart = function() {
  22. var args, name;
  23. name = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
  24. if (!this.trace) {
  25. return;
  26. }
  27. this.logtimers || (this.logtimers = {});
  28. this.logtimers[name] = +(new Date);
  29. if (args.length > 0) {
  30. this.log.apply(this, ["" + name].concat(slice.call(args), ["(started)"]));
  31. }
  32. return this;
  33. };
  34. Class.prototype.logEnd = function() {
  35. var args, ms, name;
  36. name = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
  37. ms = +(new Date) - this.logtimers[name];
  38. this.log.apply(this, ["" + name].concat(slice.call(args), ["(Done in " + ms + "ms)"]));
  39. return this;
  40. };
  41. return Class;
  42. })();
  43. window.Class = Class;
  44. }).call(this);
  45. /* ---- Console.coffee ---- */
  46. (function() {
  47. var Console,
  48. bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  49. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  50. hasProp = {}.hasOwnProperty;
  51. Console = (function(superClass) {
  52. extend(Console, superClass);
  53. function Console(sidebar) {
  54. var handleMessageWebsocket_original;
  55. this.sidebar = sidebar;
  56. this.handleTabClick = bind(this.handleTabClick, this);
  57. this.changeFilter = bind(this.changeFilter, this);
  58. this.stopDragY = bind(this.stopDragY, this);
  59. this.cleanup = bind(this.cleanup, this);
  60. this.onClosed = bind(this.onClosed, this);
  61. this.onOpened = bind(this.onOpened, this);
  62. this.open = bind(this.open, this);
  63. this.close = bind(this.close, this);
  64. this.loadConsoleText = bind(this.loadConsoleText, this);
  65. this.addLines = bind(this.addLines, this);
  66. this.formatLine = bind(this.formatLine, this);
  67. this.checkTextIsBottom = bind(this.checkTextIsBottom, this);
  68. this.tag = null;
  69. this.opened = false;
  70. this.filter = null;
  71. this.tab_types = [
  72. {
  73. title: "All",
  74. filter: ""
  75. }, {
  76. title: "Info",
  77. filter: "INFO"
  78. }, {
  79. title: "Warning",
  80. filter: "WARNING"
  81. }, {
  82. title: "Error",
  83. filter: "ERROR"
  84. }
  85. ];
  86. this.read_size = 32 * 1024;
  87. this.tab_active = "";
  88. handleMessageWebsocket_original = this.sidebar.wrapper.handleMessageWebsocket;
  89. this.sidebar.wrapper.handleMessageWebsocket = (function(_this) {
  90. return function(message) {
  91. if (message.cmd === "logLineAdd" && message.params.stream_id === _this.stream_id) {
  92. return _this.addLines(message.params.lines);
  93. } else {
  94. return handleMessageWebsocket_original(message);
  95. }
  96. };
  97. })(this);
  98. $(window).on("hashchange", (function(_this) {
  99. return function() {
  100. if (window.top.location.hash.startsWith("#ZeroNet:Console")) {
  101. return _this.open();
  102. }
  103. };
  104. })(this));
  105. if (window.top.location.hash.startsWith("#ZeroNet:Console")) {
  106. setTimeout(((function(_this) {
  107. return function() {
  108. return _this.open();
  109. };
  110. })(this)), 10);
  111. }
  112. }
  113. Console.prototype.createHtmltag = function() {
  114. var j, len, ref, tab, tab_type;
  115. if (!this.container) {
  116. this.container = $("<div class=\"console-container\">\n <div class=\"console\">\n <div class=\"console-top\">\n <div class=\"console-tabs\"></div>\n <div class=\"console-text\">Loading...</div>\n </div>\n <div class=\"console-middle\">\n <div class=\"mynode\"></div>\n <div class=\"peers\">\n <div class=\"peer\"><div class=\"line\"></div><a href=\"#\" class=\"icon\">\u25BD</div></div>\n </div>\n </div>\n </div>\n</div>");
  117. this.text = this.container.find(".console-text");
  118. this.text_elem = this.text[0];
  119. this.tabs = this.container.find(".console-tabs");
  120. this.text.on("mousewheel", (function(_this) {
  121. return function(e) {
  122. if (e.originalEvent.deltaY < 0) {
  123. _this.text.stop();
  124. }
  125. return RateLimit(300, _this.checkTextIsBottom);
  126. };
  127. })(this));
  128. this.text.is_bottom = true;
  129. this.container.appendTo(document.body);
  130. this.tag = this.container.find(".console");
  131. ref = this.tab_types;
  132. for (j = 0, len = ref.length; j < len; j++) {
  133. tab_type = ref[j];
  134. tab = $("<a></a>", {
  135. href: "#",
  136. "data-filter": tab_type.filter,
  137. "data-title": tab_type.title
  138. }).text(tab_type.title);
  139. if (tab_type.filter === this.tab_active) {
  140. tab.addClass("active");
  141. }
  142. tab.on("click", this.handleTabClick);
  143. if (window.top.location.hash.endsWith(tab_type.title)) {
  144. this.log("Triggering click on", tab);
  145. tab.trigger("click");
  146. }
  147. this.tabs.append(tab);
  148. }
  149. this.container.on("mousedown touchend touchcancel", (function(_this) {
  150. return function(e) {
  151. if (e.target !== e.currentTarget) {
  152. return true;
  153. }
  154. _this.log("closing");
  155. if ($(document.body).hasClass("body-console")) {
  156. _this.close();
  157. return true;
  158. }
  159. };
  160. })(this));
  161. return this.loadConsoleText();
  162. }
  163. };
  164. Console.prototype.checkTextIsBottom = function() {
  165. return this.text.is_bottom = Math.round(this.text_elem.scrollTop + this.text_elem.clientHeight) >= this.text_elem.scrollHeight - 15;
  166. };
  167. Console.prototype.toColor = function(text, saturation, lightness) {
  168. var hash, i, j, ref;
  169. if (saturation == null) {
  170. saturation = 60;
  171. }
  172. if (lightness == null) {
  173. lightness = 70;
  174. }
  175. hash = 0;
  176. for (i = j = 0, ref = text.length - 1; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
  177. hash += text.charCodeAt(i) * i;
  178. hash = hash % 1777;
  179. }
  180. return "hsl(" + (hash % 360) + ("," + saturation + "%," + lightness + "%)");
  181. };
  182. Console.prototype.formatLine = function(line) {
  183. var added, level, match, module, ref, text;
  184. match = line.match(/(\[.*?\])[ ]+(.*?)[ ]+(.*?)[ ]+(.*)/);
  185. if (!match) {
  186. return line.replace(/\</g, "&lt;").replace(/\>/g, "&gt;");
  187. }
  188. ref = line.match(/(\[.*?\])[ ]+(.*?)[ ]+(.*?)[ ]+(.*)/), line = ref[0], added = ref[1], level = ref[2], module = ref[3], text = ref[4];
  189. added = "<span style='color: #dfd0fa'>" + added + "</span>";
  190. level = "<span style='color: " + (this.toColor(level, 100)) + ";'>" + level + "</span>";
  191. module = "<span style='color: " + (this.toColor(module, 60)) + "; font-weight: bold;'>" + module + "</span>";
  192. text = text.replace(/(Site:[A-Za-z0-9\.]+)/g, "<span style='color: #AAAAFF'>$1</span>");
  193. text = text.replace(/\</g, "&lt;").replace(/\>/g, "&gt;");
  194. return added + " " + level + " " + module + " " + text;
  195. };
  196. Console.prototype.addLines = function(lines, animate) {
  197. var html_lines, j, len, line;
  198. if (animate == null) {
  199. animate = true;
  200. }
  201. html_lines = [];
  202. this.logStart("formatting");
  203. for (j = 0, len = lines.length; j < len; j++) {
  204. line = lines[j];
  205. html_lines.push(this.formatLine(line));
  206. }
  207. this.logEnd("formatting");
  208. this.logStart("adding");
  209. this.text.append(html_lines.join("<br>") + "<br>");
  210. this.logEnd("adding");
  211. if (this.text.is_bottom && animate) {
  212. return this.text.stop().animate({
  213. scrollTop: this.text_elem.scrollHeight - this.text_elem.clientHeight + 1
  214. }, 600, 'easeInOutCubic');
  215. }
  216. };
  217. Console.prototype.loadConsoleText = function() {
  218. this.sidebar.wrapper.ws.cmd("consoleLogRead", {
  219. filter: this.filter,
  220. read_size: this.read_size
  221. }, (function(_this) {
  222. return function(res) {
  223. var pos_diff, size_read, size_total;
  224. _this.text.html("");
  225. pos_diff = res["pos_end"] - res["pos_start"];
  226. size_read = Math.round(pos_diff / 1024);
  227. size_total = Math.round(res['pos_end'] / 1024);
  228. _this.text.append("<br><br>");
  229. _this.text.append("Displaying " + res.lines.length + " of " + res.num_found + " lines found in the last " + size_read + "kB of the log file. (" + size_total + "kB total)<br>");
  230. _this.addLines(res.lines, false);
  231. return _this.text_elem.scrollTop = _this.text_elem.scrollHeight;
  232. };
  233. })(this));
  234. if (this.stream_id) {
  235. this.sidebar.wrapper.ws.cmd("consoleLogStreamRemove", {
  236. stream_id: this.stream_id
  237. });
  238. }
  239. return this.sidebar.wrapper.ws.cmd("consoleLogStream", {
  240. filter: this.filter
  241. }, (function(_this) {
  242. return function(res) {
  243. return _this.stream_id = res.stream_id;
  244. };
  245. })(this));
  246. };
  247. Console.prototype.close = function() {
  248. window.top.location.hash = "";
  249. this.sidebar.move_lock = "y";
  250. this.sidebar.startDrag();
  251. return this.sidebar.stopDrag();
  252. };
  253. Console.prototype.open = function() {
  254. this.sidebar.startDrag();
  255. this.sidebar.moved("y");
  256. this.sidebar.fixbutton_targety = this.sidebar.page_height - this.sidebar.fixbutton_inity - 50;
  257. return this.sidebar.stopDrag();
  258. };
  259. Console.prototype.onOpened = function() {
  260. this.sidebar.onClosed();
  261. return this.log("onOpened");
  262. };
  263. Console.prototype.onClosed = function() {
  264. $(document.body).removeClass("body-console");
  265. if (this.stream_id) {
  266. return this.sidebar.wrapper.ws.cmd("consoleLogStreamRemove", {
  267. stream_id: this.stream_id
  268. });
  269. }
  270. };
  271. Console.prototype.cleanup = function() {
  272. if (this.container) {
  273. this.container.remove();
  274. return this.container = null;
  275. }
  276. };
  277. Console.prototype.stopDragY = function() {
  278. var targety;
  279. if (this.sidebar.fixbutton_targety === this.sidebar.fixbutton_inity) {
  280. targety = 0;
  281. this.opened = false;
  282. } else {
  283. targety = this.sidebar.fixbutton_targety - this.sidebar.fixbutton_inity;
  284. this.onOpened();
  285. this.opened = true;
  286. }
  287. if (this.tag) {
  288. this.tag.css("transition", "0.5s ease-out");
  289. this.tag.css("transform", "translateY(" + targety + "px)").one(transitionEnd, (function(_this) {
  290. return function() {
  291. _this.tag.css("transition", "");
  292. if (!_this.opened) {
  293. return _this.cleanup();
  294. }
  295. };
  296. })(this));
  297. }
  298. this.log("stopDragY", "opened:", this.opened, targety);
  299. if (!this.opened) {
  300. return this.onClosed();
  301. }
  302. };
  303. Console.prototype.changeFilter = function(filter) {
  304. this.filter = filter;
  305. if (this.filter === "") {
  306. this.read_size = 32 * 1024;
  307. } else {
  308. this.read_size = 5 * 1024 * 1024;
  309. }
  310. return this.loadConsoleText();
  311. };
  312. Console.prototype.handleTabClick = function(e) {
  313. var elem;
  314. elem = $(e.currentTarget);
  315. this.tab_active = elem.data("filter");
  316. $("a", this.tabs).removeClass("active");
  317. elem.addClass("active");
  318. this.changeFilter(this.tab_active);
  319. window.top.location.hash = "#ZeroNet:Console:" + elem.data("title");
  320. return false;
  321. };
  322. return Console;
  323. })(Class);
  324. window.Console = Console;
  325. }).call(this);
  326. /* ---- Menu.coffee ---- */
  327. (function() {
  328. var Menu,
  329. slice = [].slice;
  330. Menu = (function() {
  331. function Menu(button) {
  332. this.button = button;
  333. this.elem = $(".menu.template").clone().removeClass("template");
  334. this.elem.appendTo("body");
  335. this.items = [];
  336. }
  337. Menu.prototype.show = function() {
  338. var button_pos, left;
  339. if (window.visible_menu && window.visible_menu.button[0] === this.button[0]) {
  340. window.visible_menu.hide();
  341. return this.hide();
  342. } else {
  343. button_pos = this.button.offset();
  344. left = button_pos.left;
  345. this.elem.css({
  346. "top": button_pos.top + this.button.outerHeight(),
  347. "left": left
  348. });
  349. this.button.addClass("menu-active");
  350. this.elem.addClass("visible");
  351. if (this.elem.position().left + this.elem.width() + 20 > window.innerWidth) {
  352. this.elem.css("left", window.innerWidth - this.elem.width() - 20);
  353. }
  354. if (window.visible_menu) {
  355. window.visible_menu.hide();
  356. }
  357. return window.visible_menu = this;
  358. }
  359. };
  360. Menu.prototype.hide = function() {
  361. this.elem.removeClass("visible");
  362. this.button.removeClass("menu-active");
  363. return window.visible_menu = null;
  364. };
  365. Menu.prototype.addItem = function(title, cb) {
  366. var item;
  367. item = $(".menu-item.template", this.elem).clone().removeClass("template");
  368. item.html(title);
  369. item.on("click", (function(_this) {
  370. return function() {
  371. if (!cb(item)) {
  372. _this.hide();
  373. }
  374. return false;
  375. };
  376. })(this));
  377. item.appendTo(this.elem);
  378. this.items.push(item);
  379. return item;
  380. };
  381. Menu.prototype.log = function() {
  382. var args;
  383. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  384. return console.log.apply(console, ["[Menu]"].concat(slice.call(args)));
  385. };
  386. return Menu;
  387. })();
  388. window.Menu = Menu;
  389. $("body").on("click", function(e) {
  390. if (window.visible_menu && e.target !== window.visible_menu.button[0] && $(e.target).parent()[0] !== window.visible_menu.elem[0]) {
  391. return window.visible_menu.hide();
  392. }
  393. });
  394. }).call(this);
  395. /* ---- Prototypes.coffee ---- */
  396. (function() {
  397. String.prototype.startsWith = function(s) {
  398. return this.slice(0, s.length) === s;
  399. };
  400. String.prototype.endsWith = function(s) {
  401. return s === '' || this.slice(-s.length) === s;
  402. };
  403. String.prototype.capitalize = function() {
  404. if (this.length) {
  405. return this[0].toUpperCase() + this.slice(1);
  406. } else {
  407. return "";
  408. }
  409. };
  410. String.prototype.repeat = function(count) {
  411. return new Array(count + 1).join(this);
  412. };
  413. window.isEmpty = function(obj) {
  414. var key;
  415. for (key in obj) {
  416. return false;
  417. }
  418. return true;
  419. };
  420. }).call(this);
  421. /* ---- RateLimit.coffee ---- */
  422. (function() {
  423. var call_after_interval, limits;
  424. limits = {};
  425. call_after_interval = {};
  426. window.RateLimit = function(interval, fn) {
  427. if (!limits[fn]) {
  428. call_after_interval[fn] = false;
  429. fn();
  430. return limits[fn] = setTimeout((function() {
  431. if (call_after_interval[fn]) {
  432. fn();
  433. }
  434. delete limits[fn];
  435. return delete call_after_interval[fn];
  436. }), interval);
  437. } else {
  438. return call_after_interval[fn] = true;
  439. }
  440. };
  441. }).call(this);
  442. /* ---- Scrollable.js ---- */
  443. /* via http://jsfiddle.net/elGrecode/00dgurnn/ */
  444. window.initScrollable = function () {
  445. var scrollContainer = document.querySelector('.scrollable'),
  446. scrollContentWrapper = document.querySelector('.scrollable .content-wrapper'),
  447. scrollContent = document.querySelector('.scrollable .content'),
  448. contentPosition = 0,
  449. scrollerBeingDragged = false,
  450. scroller,
  451. topPosition,
  452. scrollerHeight;
  453. function calculateScrollerHeight() {
  454. // *Calculation of how tall scroller should be
  455. var visibleRatio = scrollContainer.offsetHeight / scrollContentWrapper.scrollHeight;
  456. if (visibleRatio == 1)
  457. scroller.style.display = "none";
  458. else
  459. scroller.style.display = "block";
  460. return visibleRatio * scrollContainer.offsetHeight;
  461. }
  462. function moveScroller(evt) {
  463. // Move Scroll bar to top offset
  464. var scrollPercentage = evt.target.scrollTop / scrollContentWrapper.scrollHeight;
  465. topPosition = scrollPercentage * (scrollContainer.offsetHeight - 5); // 5px arbitrary offset so scroll bar doesn't move too far beyond content wrapper bounding box
  466. scroller.style.top = topPosition + 'px';
  467. }
  468. function startDrag(evt) {
  469. normalizedPosition = evt.pageY;
  470. contentPosition = scrollContentWrapper.scrollTop;
  471. scrollerBeingDragged = true;
  472. window.addEventListener('mousemove', scrollBarScroll);
  473. return false;
  474. }
  475. function stopDrag(evt) {
  476. scrollerBeingDragged = false;
  477. window.removeEventListener('mousemove', scrollBarScroll);
  478. }
  479. function scrollBarScroll(evt) {
  480. if (scrollerBeingDragged === true) {
  481. evt.preventDefault();
  482. var mouseDifferential = evt.pageY - normalizedPosition;
  483. var scrollEquivalent = mouseDifferential * (scrollContentWrapper.scrollHeight / scrollContainer.offsetHeight);
  484. scrollContentWrapper.scrollTop = contentPosition + scrollEquivalent;
  485. }
  486. }
  487. function updateHeight() {
  488. scrollerHeight = calculateScrollerHeight() - 10;
  489. scroller.style.height = scrollerHeight + 'px';
  490. }
  491. function createScroller() {
  492. // *Creates scroller element and appends to '.scrollable' div
  493. // create scroller element
  494. scroller = document.createElement("div");
  495. scroller.className = 'scroller';
  496. // determine how big scroller should be based on content
  497. scrollerHeight = calculateScrollerHeight() - 10;
  498. if (scrollerHeight / scrollContainer.offsetHeight < 1) {
  499. // *If there is a need to have scroll bar based on content size
  500. scroller.style.height = scrollerHeight + 'px';
  501. // append scroller to scrollContainer div
  502. scrollContainer.appendChild(scroller);
  503. // show scroll path divot
  504. scrollContainer.className += ' showScroll';
  505. // attach related draggable listeners
  506. scroller.addEventListener('mousedown', startDrag);
  507. window.addEventListener('mouseup', stopDrag);
  508. }
  509. }
  510. createScroller();
  511. // *** Listeners ***
  512. scrollContentWrapper.addEventListener('scroll', moveScroller);
  513. return updateHeight;
  514. };
  515. /* ---- Sidebar.coffee ---- */
  516. (function() {
  517. var Sidebar, wrapper,
  518. bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  519. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  520. hasProp = {}.hasOwnProperty,
  521. indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  522. Sidebar = (function(superClass) {
  523. extend(Sidebar, superClass);
  524. function Sidebar(wrapper1) {
  525. this.wrapper = wrapper1;
  526. this.unloadGlobe = bind(this.unloadGlobe, this);
  527. this.displayGlobe = bind(this.displayGlobe, this);
  528. this.loadGlobe = bind(this.loadGlobe, this);
  529. this.animDrag = bind(this.animDrag, this);
  530. this.setHtmlTag = bind(this.setHtmlTag, this);
  531. this.waitMove = bind(this.waitMove, this);
  532. this.resized = bind(this.resized, this);
  533. this.tag = null;
  534. this.container = null;
  535. this.opened = false;
  536. this.width = 410;
  537. this.console = new Console(this);
  538. this.fixbutton = $(".fixbutton");
  539. this.fixbutton_addx = 0;
  540. this.fixbutton_addy = 0;
  541. this.fixbutton_initx = 0;
  542. this.fixbutton_inity = 15;
  543. this.fixbutton_targetx = 0;
  544. this.move_lock = null;
  545. this.page_width = $(window).width();
  546. this.page_height = $(window).height();
  547. this.frame = $("#inner-iframe");
  548. this.initFixbutton();
  549. this.dragStarted = 0;
  550. this.globe = null;
  551. this.preload_html = null;
  552. this.original_set_site_info = this.wrapper.setSiteInfo;
  553. if (window.top.location.hash === "#ZeroNet:OpenSidebar") {
  554. this.startDrag();
  555. this.moved("x");
  556. this.fixbutton_targetx = this.fixbutton_initx - this.width;
  557. this.stopDrag();
  558. }
  559. }
  560. Sidebar.prototype.initFixbutton = function() {
  561. this.fixbutton.on("mousedown touchstart", (function(_this) {
  562. return function(e) {
  563. if (e.button > 0) {
  564. return;
  565. }
  566. e.preventDefault();
  567. _this.fixbutton.off("click touchend touchcancel");
  568. _this.dragStarted = +(new Date);
  569. $(".drag-bg").remove();
  570. $("<div class='drag-bg'></div>").appendTo(document.body);
  571. return $("body").one("mousemove touchmove", function(e) {
  572. var mousex, mousey;
  573. mousex = e.pageX;
  574. mousey = e.pageY;
  575. if (!mousex) {
  576. mousex = e.originalEvent.touches[0].pageX;
  577. mousey = e.originalEvent.touches[0].pageY;
  578. }
  579. _this.fixbutton_addx = _this.fixbutton.offset().left - mousex;
  580. _this.fixbutton_addy = _this.fixbutton.offset().top - mousey;
  581. return _this.startDrag();
  582. });
  583. };
  584. })(this));
  585. this.fixbutton.parent().on("click touchend touchcancel", (function(_this) {
  586. return function(e) {
  587. if ((+(new Date)) - _this.dragStarted < 100) {
  588. window.top.location = _this.fixbutton.find(".fixbutton-bg").attr("href");
  589. }
  590. return _this.stopDrag();
  591. };
  592. })(this));
  593. this.resized();
  594. return $(window).on("resize", this.resized);
  595. };
  596. Sidebar.prototype.resized = function() {
  597. this.page_width = $(window).width();
  598. this.page_height = $(window).height();
  599. this.fixbutton_initx = this.page_width - 75;
  600. if (this.opened) {
  601. return this.fixbutton.css({
  602. left: this.fixbutton_initx - this.width
  603. });
  604. } else {
  605. return this.fixbutton.css({
  606. left: this.fixbutton_initx
  607. });
  608. }
  609. };
  610. Sidebar.prototype.startDrag = function() {
  611. this.log("startDrag", this.fixbutton_initx, this.fixbutton_inity);
  612. this.fixbutton_targetx = this.fixbutton_initx;
  613. this.fixbutton_targety = this.fixbutton_inity;
  614. this.fixbutton.addClass("dragging");
  615. if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
  616. this.fixbutton.css("pointer-events", "none");
  617. }
  618. this.fixbutton.one("click", (function(_this) {
  619. return function(e) {
  620. var moved_x, moved_y;
  621. _this.stopDrag();
  622. _this.fixbutton.removeClass("dragging");
  623. moved_x = Math.abs(_this.fixbutton.offset().left - _this.fixbutton_initx);
  624. moved_y = Math.abs(_this.fixbutton.offset().top - _this.fixbutton_inity);
  625. if (moved_x > 5 || moved_y > 10) {
  626. return e.preventDefault();
  627. }
  628. };
  629. })(this));
  630. this.fixbutton.parents().on("mousemove touchmove", this.animDrag);
  631. this.fixbutton.parents().on("mousemove touchmove", this.waitMove);
  632. return this.fixbutton.parents().one("mouseup touchend touchcancel", (function(_this) {
  633. return function(e) {
  634. e.preventDefault();
  635. return _this.stopDrag();
  636. };
  637. })(this));
  638. };
  639. Sidebar.prototype.waitMove = function(e) {
  640. var moved_x, moved_y;
  641. document.body.style.perspective = "1000px";
  642. document.body.style.height = "100%";
  643. document.body.style.willChange = "perspective";
  644. document.documentElement.style.height = "100%";
  645. moved_x = Math.abs(parseInt(this.fixbutton[0].style.left) - this.fixbutton_targetx);
  646. moved_y = Math.abs(parseInt(this.fixbutton[0].style.top) - this.fixbutton_targety);
  647. if (moved_x > 5 && (+(new Date)) - this.dragStarted + moved_x > 50) {
  648. this.moved("x");
  649. this.fixbutton.stop().animate({
  650. "top": this.fixbutton_inity
  651. }, 1000);
  652. return this.fixbutton.parents().off("mousemove touchmove", this.waitMove);
  653. } else if (moved_y > 5 && (+(new Date)) - this.dragStarted + moved_y > 50) {
  654. this.moved("y");
  655. return this.fixbutton.parents().off("mousemove touchmove", this.waitMove);
  656. }
  657. };
  658. Sidebar.prototype.moved = function(direction) {
  659. var img;
  660. this.log("Moved", direction);
  661. this.move_lock = direction;
  662. if (direction === "y") {
  663. $(document.body).addClass("body-console");
  664. return this.console.createHtmltag();
  665. }
  666. this.createHtmltag();
  667. $(document.body).addClass("body-sidebar");
  668. this.container.on("mousedown touchend touchcancel", (function(_this) {
  669. return function(e) {
  670. if (e.target !== e.currentTarget) {
  671. return true;
  672. }
  673. _this.log("closing");
  674. if ($(document.body).hasClass("body-sidebar")) {
  675. _this.close();
  676. return true;
  677. }
  678. };
  679. })(this));
  680. $(window).off("resize");
  681. $(window).on("resize", (function(_this) {
  682. return function() {
  683. $(document.body).css("height", $(window).height());
  684. _this.scrollable();
  685. return _this.resized();
  686. };
  687. })(this));
  688. this.wrapper.setSiteInfo = (function(_this) {
  689. return function(site_info) {
  690. _this.setSiteInfo(site_info);
  691. return _this.original_set_site_info.apply(_this.wrapper, arguments);
  692. };
  693. })(this);
  694. img = new Image();
  695. return img.src = "/uimedia/globe/world.jpg";
  696. };
  697. Sidebar.prototype.setSiteInfo = function(site_info) {
  698. RateLimit(1500, (function(_this) {
  699. return function() {
  700. return _this.updateHtmlTag();
  701. };
  702. })(this));
  703. return RateLimit(30000, (function(_this) {
  704. return function() {
  705. return _this.displayGlobe();
  706. };
  707. })(this));
  708. };
  709. Sidebar.prototype.createHtmltag = function() {
  710. this.when_loaded = $.Deferred();
  711. if (!this.container) {
  712. this.container = $("<div class=\"sidebar-container\"><div class=\"sidebar scrollable\"><div class=\"content-wrapper\"><div class=\"content\">\n</div></div></div></div>");
  713. this.container.appendTo(document.body);
  714. this.tag = this.container.find(".sidebar");
  715. this.updateHtmlTag();
  716. return this.scrollable = window.initScrollable();
  717. }
  718. };
  719. Sidebar.prototype.updateHtmlTag = function() {
  720. if (this.preload_html) {
  721. this.setHtmlTag(this.preload_html);
  722. return this.preload_html = null;
  723. } else {
  724. return this.wrapper.ws.cmd("sidebarGetHtmlTag", {}, this.setHtmlTag);
  725. }
  726. };
  727. Sidebar.prototype.setHtmlTag = function(res) {
  728. if (this.tag.find(".content").children().length === 0) {
  729. this.log("Creating content");
  730. this.container.addClass("loaded");
  731. morphdom(this.tag.find(".content")[0], '<div class="content">' + res + '</div>');
  732. this.when_loaded.resolve();
  733. } else {
  734. morphdom(this.tag.find(".content")[0], '<div class="content">' + res + '</div>', {
  735. onBeforeMorphEl: function(from_el, to_el) {
  736. if (from_el.className === "globe" || from_el.className.indexOf("noupdate") >= 0) {
  737. return false;
  738. } else {
  739. return true;
  740. }
  741. }
  742. });
  743. }
  744. this.tag.find("#privatekey-add").off("click, touchend").on("click touchend", (function(_this) {
  745. return function(e) {
  746. _this.wrapper.displayPrompt("Enter your private key:", "password", "Save", "", function(privatekey) {
  747. return _this.wrapper.ws.cmd("userSetSitePrivatekey", [privatekey], function(res) {
  748. return _this.wrapper.notifications.add("privatekey", "done", "Private key saved for site signing", 5000);
  749. });
  750. });
  751. return false;
  752. };
  753. })(this));
  754. this.tag.find("#privatekey-forget").off("click, touchend").on("click touchend", (function(_this) {
  755. return function(e) {
  756. _this.wrapper.displayConfirm("Remove saved private key for this site?", "Forget", function(res) {
  757. if (!res) {
  758. return false;
  759. }
  760. return _this.wrapper.ws.cmd("userSetSitePrivatekey", [""], function(res) {
  761. return _this.wrapper.notifications.add("privatekey", "done", "Saved private key removed", 5000);
  762. });
  763. });
  764. return false;
  765. };
  766. })(this));
  767. return this.tag.find("#browse-files").attr("href", document.location.pathname.replace(/(\/.*?(\/|$)).*$/, "/list$1"));
  768. };
  769. Sidebar.prototype.animDrag = function(e) {
  770. var mousex, mousey, overdrag, overdrag_percent, targetx, targety;
  771. mousex = e.pageX;
  772. mousey = e.pageY;
  773. if (!mousex && e.originalEvent.touches) {
  774. mousex = e.originalEvent.touches[0].pageX;
  775. mousey = e.originalEvent.touches[0].pageY;
  776. }
  777. overdrag = this.fixbutton_initx - this.width - mousex;
  778. if (overdrag > 0) {
  779. overdrag_percent = 1 + overdrag / 300;
  780. mousex = (mousex + (this.fixbutton_initx - this.width) * overdrag_percent) / (1 + overdrag_percent);
  781. }
  782. targetx = this.fixbutton_initx - mousex - this.fixbutton_addx;
  783. targety = this.fixbutton_inity - mousey - this.fixbutton_addy;
  784. if (this.move_lock === "x") {
  785. targety = this.fixbutton_inity;
  786. } else if (this.move_lock === "y") {
  787. targetx = this.fixbutton_initx;
  788. }
  789. if (!this.move_lock || this.move_lock === "x") {
  790. this.fixbutton[0].style.left = (mousex + this.fixbutton_addx) + "px";
  791. if (this.tag) {
  792. this.tag[0].style.transform = "translateX(" + (0 - targetx) + "px)";
  793. }
  794. }
  795. if (!this.move_lock || this.move_lock === "y") {
  796. this.fixbutton[0].style.top = (mousey + this.fixbutton_addy) + "px";
  797. if (this.console.tag) {
  798. this.console.tag[0].style.transform = "translateY(" + (0 - targety) + "px)";
  799. }
  800. }
  801. if ((!this.opened && targetx > this.width / 3) || (this.opened && targetx > this.width * 0.9)) {
  802. this.fixbutton_targetx = this.fixbutton_initx - this.width;
  803. } else {
  804. this.fixbutton_targetx = this.fixbutton_initx;
  805. }
  806. if ((!this.console.opened && 0 - targety > this.page_height / 10) || (this.console.opened && 0 - targety > this.page_height * 0.8)) {
  807. return this.fixbutton_targety = this.page_height - this.fixbutton_inity - 50;
  808. } else {
  809. return this.fixbutton_targety = this.fixbutton_inity;
  810. }
  811. };
  812. Sidebar.prototype.stopDrag = function() {
  813. var left, top;
  814. this.fixbutton.parents().off("mousemove touchmove");
  815. this.fixbutton.off("mousemove touchmove");
  816. this.fixbutton.css("pointer-events", "");
  817. $(".drag-bg").remove();
  818. if (!this.fixbutton.hasClass("dragging")) {
  819. return;
  820. }
  821. this.fixbutton.removeClass("dragging");
  822. if (this.fixbutton_targetx !== this.fixbutton.offset().left || this.fixbutton_targety !== this.fixbutton.offset().top) {
  823. if (this.move_lock === "y") {
  824. top = this.fixbutton_targety;
  825. left = this.fixbutton_initx;
  826. }
  827. if (this.move_lock === "x") {
  828. top = this.fixbutton_inity;
  829. left = this.fixbutton_targetx;
  830. }
  831. this.fixbutton.stop().animate({
  832. "left": left,
  833. "top": top
  834. }, 500, "easeOutBack", (function(_this) {
  835. return function() {
  836. if (_this.fixbutton_targetx === _this.fixbutton_initx) {
  837. _this.fixbutton.css("left", "auto");
  838. } else {
  839. _this.fixbutton.css("left", left);
  840. }
  841. return $(".fixbutton-bg").trigger("mouseout");
  842. };
  843. })(this));
  844. this.stopDragX();
  845. this.console.stopDragY();
  846. }
  847. return this.move_lock = null;
  848. };
  849. Sidebar.prototype.stopDragX = function() {
  850. var targetx;
  851. if (this.fixbutton_targetx === this.fixbutton_initx || this.move_lock === "y") {
  852. targetx = 0;
  853. this.opened = false;
  854. } else {
  855. targetx = this.width;
  856. if (this.opened) {
  857. this.onOpened();
  858. } else {
  859. this.when_loaded.done((function(_this) {
  860. return function() {
  861. return _this.onOpened();
  862. };
  863. })(this));
  864. }
  865. this.opened = true;
  866. }
  867. if (this.tag) {
  868. this.tag.css("transition", "0.4s ease-out");
  869. this.tag.css("transform", "translateX(-" + targetx + "px)").one(transitionEnd, (function(_this) {
  870. return function() {
  871. _this.tag.css("transition", "");
  872. if (!_this.opened) {
  873. _this.container.remove();
  874. _this.container = null;
  875. if (_this.tag) {
  876. _this.tag.remove();
  877. return _this.tag = null;
  878. }
  879. }
  880. };
  881. })(this));
  882. }
  883. this.log("stopdrag", "opened:", this.opened);
  884. if (!this.opened) {
  885. return this.onClosed();
  886. }
  887. };
  888. Sidebar.prototype.sign = function(inner_path, privatekey) {
  889. this.wrapper.displayProgress("sign", "Signing: " + inner_path + "...", 0);
  890. return this.wrapper.ws.cmd("siteSign", {
  891. privatekey: privatekey,
  892. inner_path: inner_path,
  893. update_changed_files: true
  894. }, (function(_this) {
  895. return function(res) {
  896. if (res === "ok") {
  897. return _this.wrapper.displayProgress("sign", inner_path + " signed!", 100);
  898. } else {
  899. return _this.wrapper.displayProgress("sign", "Error signing " + inner_path, -1);
  900. }
  901. };
  902. })(this));
  903. };
  904. Sidebar.prototype.publish = function(inner_path, privatekey) {
  905. return this.wrapper.ws.cmd("sitePublish", {
  906. privatekey: privatekey,
  907. inner_path: inner_path,
  908. sign: true,
  909. update_changed_files: true
  910. }, (function(_this) {
  911. return function(res) {
  912. if (res === "ok") {
  913. return _this.wrapper.notifications.add("sign", "done", inner_path + " Signed and published!", 5000);
  914. }
  915. };
  916. })(this));
  917. };
  918. Sidebar.prototype.handleSiteDeleteClick = function() {
  919. var options, question;
  920. if (this.wrapper.site_info.privatekey) {
  921. question = "Are you sure?<br>This site has a saved private key";
  922. options = ["Forget private key and delete site"];
  923. } else {
  924. question = "Are you sure?";
  925. options = ["Delete this site", "Blacklist"];
  926. }
  927. return this.wrapper.displayConfirm(question, options, (function(_this) {
  928. return function(confirmed) {
  929. if (confirmed === 1) {
  930. _this.tag.find("#button-delete").addClass("loading");
  931. return _this.wrapper.ws.cmd("siteDelete", _this.wrapper.site_info.address, function() {
  932. return document.location = $(".fixbutton-bg").attr("href");
  933. });
  934. } else if (confirmed === 2) {
  935. return _this.wrapper.displayPrompt("Blacklist this site", "text", "Delete and Blacklist", "Reason", function(reason) {
  936. _this.tag.find("#button-delete").addClass("loading");
  937. _this.wrapper.ws.cmd("siteblockAdd", [_this.wrapper.site_info.address, reason]);
  938. return _this.wrapper.ws.cmd("siteDelete", _this.wrapper.site_info.address, function() {
  939. return document.location = $(".fixbutton-bg").attr("href");
  940. });
  941. });
  942. }
  943. };
  944. })(this));
  945. };
  946. Sidebar.prototype.onOpened = function() {
  947. var menu;
  948. this.log("Opened");
  949. this.scrollable();
  950. this.tag.find("#checkbox-owned, #checkbox-autodownloadoptional").off("click touchend").on("click touchend", (function(_this) {
  951. return function() {
  952. return setTimeout((function() {
  953. return _this.scrollable();
  954. }), 300);
  955. };
  956. })(this));
  957. this.tag.find("#button-sitelimit").off("click touchend").on("click touchend", (function(_this) {
  958. return function() {
  959. _this.wrapper.ws.cmd("siteSetLimit", $("#input-sitelimit").val(), function(res) {
  960. if (res === "ok") {
  961. _this.wrapper.notifications.add("done-sitelimit", "done", "Site storage limit modified!", 5000);
  962. }
  963. return _this.updateHtmlTag();
  964. });
  965. return false;
  966. };
  967. })(this));
  968. this.tag.find("#button-autodownload_bigfile_size_limit").off("click touchend").on("click touchend", (function(_this) {
  969. return function() {
  970. _this.wrapper.ws.cmd("siteSetAutodownloadBigfileLimit", $("#input-autodownload_bigfile_size_limit").val(), function(res) {
  971. if (res === "ok") {
  972. _this.wrapper.notifications.add("done-bigfilelimit", "done", "Site bigfile auto download limit modified!", 5000);
  973. }
  974. return _this.updateHtmlTag();
  975. });
  976. return false;
  977. };
  978. })(this));
  979. this.tag.find("#button-autodownload_previous").off("click touchend").on("click touchend", (function(_this) {
  980. return function() {
  981. _this.wrapper.ws.cmd("siteUpdate", {
  982. "address": _this.wrapper.site_info.address,
  983. "check_files": true
  984. }, function() {
  985. return _this.wrapper.notifications.add("done-download_optional", "done", "Optional files downloaded", 5000);
  986. });
  987. _this.wrapper.notifications.add("start-download_optional", "info", "Optional files download started", 5000);
  988. return false;
  989. };
  990. })(this));
  991. this.tag.find("#button-dbreload").off("click touchend").on("click touchend", (function(_this) {
  992. return function() {
  993. _this.wrapper.ws.cmd("dbReload", [], function() {
  994. _this.wrapper.notifications.add("done-dbreload", "done", "Database schema reloaded!", 5000);
  995. return _this.updateHtmlTag();
  996. });
  997. return false;
  998. };
  999. })(this));
  1000. this.tag.find("#button-dbrebuild").off("click touchend").on("click touchend", (function(_this) {
  1001. return function() {
  1002. _this.wrapper.notifications.add("done-dbrebuild", "info", "Database rebuilding....");
  1003. _this.wrapper.ws.cmd("dbRebuild", [], function() {
  1004. _this.wrapper.notifications.add("done-dbrebuild", "done", "Database rebuilt!", 5000);
  1005. return _this.updateHtmlTag();
  1006. });
  1007. return false;
  1008. };
  1009. })(this));
  1010. this.tag.find("#button-update").off("click touchend").on("click touchend", (function(_this) {
  1011. return function() {
  1012. _this.tag.find("#button-update").addClass("loading");
  1013. _this.wrapper.ws.cmd("siteUpdate", _this.wrapper.site_info.address, function() {
  1014. _this.wrapper.notifications.add("done-updated", "done", "Site updated!", 5000);
  1015. return _this.tag.find("#button-update").removeClass("loading");
  1016. });
  1017. return false;
  1018. };
  1019. })(this));
  1020. this.tag.find("#button-pause").off("click touchend").on("click touchend", (function(_this) {
  1021. return function() {
  1022. _this.tag.find("#button-pause").addClass("hidden");
  1023. _this.wrapper.ws.cmd("sitePause", _this.wrapper.site_info.address);
  1024. return false;
  1025. };
  1026. })(this));
  1027. this.tag.find("#button-resume").off("click touchend").on("click touchend", (function(_this) {
  1028. return function() {
  1029. _this.tag.find("#button-resume").addClass("hidden");
  1030. _this.wrapper.ws.cmd("siteResume", _this.wrapper.site_info.address);
  1031. return false;
  1032. };
  1033. })(this));
  1034. this.tag.find("#button-favourite").off("click touched").on("click touched", (function(_this) {
  1035. return function() {
  1036. _this.tag.find("#button-favourite").addClass("hidden");
  1037. _this.tag.find("#button-unfavourite").removeClass("hidden");
  1038. _this.wrapper.ws.cmd("siteFavourite", _this.wrapper.site_info.address);
  1039. return false;
  1040. };
  1041. })(this));
  1042. this.tag.find("#button-unfavourite").off("click touched").on("click touched", (function(_this) {
  1043. return function() {
  1044. _this.tag.find("#button-favourite").removeClass("hidden");
  1045. _this.tag.find("#button-unfavourite").addClass("hidden");
  1046. _this.wrapper.ws.cmd("siteUnfavourite", _this.wrapper.site_info.address);
  1047. return false;
  1048. };
  1049. })(this));
  1050. this.tag.find("#button-delete").off("click touchend").on("click touchend", (function(_this) {
  1051. return function() {
  1052. _this.handleSiteDeleteClick();
  1053. return false;
  1054. };
  1055. })(this));
  1056. this.tag.find("#checkbox-owned").off("click touchend").on("click touchend", (function(_this) {
  1057. return function() {
  1058. var owned;
  1059. owned = _this.tag.find("#checkbox-owned").is(":checked");
  1060. return _this.wrapper.ws.cmd("siteSetOwned", [owned], function(res_set_owned) {
  1061. _this.log("Owned", owned);
  1062. if (owned) {
  1063. return _this.wrapper.ws.cmd("siteRecoverPrivatekey", [], function(res_recover) {
  1064. if (res_recover === "ok") {
  1065. return _this.wrapper.notifications.add("recover", "done", "Private key recovered from master seed", 5000);
  1066. } else {
  1067. return _this.log("Unable to recover private key: " + res_recover.error);
  1068. }
  1069. });
  1070. }
  1071. });
  1072. };
  1073. })(this));
  1074. this.tag.find("#checkbox-autodownloadoptional").off("click touchend").on("click touchend", (function(_this) {
  1075. return function() {
  1076. return _this.wrapper.ws.cmd("siteSetAutodownloadoptional", [_this.tag.find("#checkbox-autodownloadoptional").is(":checked")]);
  1077. };
  1078. })(this));
  1079. this.tag.find("#button-identity").off("click touchend").on("click touchend", (function(_this) {
  1080. return function() {
  1081. _this.wrapper.ws.cmd("certSelect");
  1082. return false;
  1083. };
  1084. })(this));
  1085. this.tag.find("#button-settings").off("click touchend").on("click touchend", (function(_this) {
  1086. return function() {
  1087. _this.wrapper.ws.cmd("fileGet", "content.json", function(res) {
  1088. var data, json_raw;
  1089. data = JSON.parse(res);
  1090. data["title"] = $("#settings-title").val();
  1091. data["description"] = $("#settings-description").val();
  1092. json_raw = unescape(encodeURIComponent(JSON.stringify(data, void 0, '\t')));
  1093. return _this.wrapper.ws.cmd("fileWrite", ["content.json", btoa(json_raw), true], function(res) {
  1094. if (res !== "ok") {
  1095. return _this.wrapper.notifications.add("file-write", "error", "File write error: " + res);
  1096. } else {
  1097. _this.wrapper.notifications.add("file-write", "done", "Site settings saved!", 5000);
  1098. if (_this.wrapper.site_info.privatekey) {
  1099. _this.wrapper.ws.cmd("siteSign", {
  1100. privatekey: "stored",
  1101. inner_path: "content.json",
  1102. update_changed_files: true
  1103. });
  1104. }
  1105. return _this.updateHtmlTag();
  1106. }
  1107. });
  1108. });
  1109. return false;
  1110. };
  1111. })(this));
  1112. this.tag.find("#link-directory").off("click touchend").on("click touchend", (function(_this) {
  1113. return function() {
  1114. _this.wrapper.ws.cmd("serverShowdirectory", ["site", _this.wrapper.site_info.address]);
  1115. return false;
  1116. };
  1117. })(this));
  1118. this.tag.find("#link-copypeers").off("click touchend").on("click touchend", (function(_this) {
  1119. return function(e) {
  1120. var copy_text, handler;
  1121. copy_text = e.currentTarget.href;
  1122. handler = function(e) {
  1123. e.clipboardData.setData('text/plain', copy_text);
  1124. e.preventDefault();
  1125. _this.wrapper.notifications.add("copy", "done", "Site address with peers copied to your clipboard", 5000);
  1126. return document.removeEventListener('copy', handler, true);
  1127. };
  1128. document.addEventListener('copy', handler, true);
  1129. document.execCommand('copy');
  1130. return false;
  1131. };
  1132. })(this));
  1133. $(document).on("click touchend", (function(_this) {
  1134. return function() {
  1135. var ref, ref1;
  1136. if ((ref = _this.tag) != null) {
  1137. ref.find("#button-sign-publish-menu").removeClass("visible");
  1138. }
  1139. return (ref1 = _this.tag) != null ? ref1.find(".contents + .flex").removeClass("sign-publish-flex") : void 0;
  1140. };
  1141. })(this));
  1142. this.tag.find(".contents-content").off("click touchend").on("click touchend", (function(_this) {
  1143. return function(e) {
  1144. $("#input-contents").val(e.currentTarget.innerText);
  1145. return false;
  1146. };
  1147. })(this));
  1148. menu = new Menu(this.tag.find("#menu-sign-publish"));
  1149. menu.elem.css("margin-top", "-130px");
  1150. menu.addItem("Sign", (function(_this) {
  1151. return function() {
  1152. var inner_path;
  1153. inner_path = _this.tag.find("#input-contents").val();
  1154. _this.wrapper.ws.cmd("fileRules", {
  1155. inner_path: inner_path
  1156. }, function(rules) {
  1157. var ref;
  1158. if (ref = _this.wrapper.site_info.auth_address, indexOf.call(rules.signers, ref) >= 0) {
  1159. return _this.sign(inner_path);
  1160. } else if (_this.wrapper.site_info.privatekey) {
  1161. return _this.sign(inner_path, "stored");
  1162. } else {
  1163. return _this.wrapper.displayPrompt("Enter your private key:", "password", "Sign", "", function(privatekey) {
  1164. return _this.sign(inner_path, privatekey);
  1165. });
  1166. }
  1167. });
  1168. _this.tag.find(".contents + .flex").removeClass("active");
  1169. return menu.hide();
  1170. };
  1171. })(this));
  1172. menu.addItem("Publish", (function(_this) {
  1173. return function() {
  1174. var inner_path;
  1175. inner_path = _this.tag.find("#input-contents").val();
  1176. _this.wrapper.ws.cmd("sitePublish", {
  1177. "inner_path": inner_path,
  1178. "sign": false
  1179. });
  1180. _this.tag.find(".contents + .flex").removeClass("active");
  1181. return menu.hide();
  1182. };
  1183. })(this));
  1184. this.tag.find("#menu-sign-publish").off("click touchend").on("click touchend", (function(_this) {
  1185. return function() {
  1186. if (window.visible_menu === menu) {
  1187. _this.tag.find(".contents + .flex").removeClass("active");
  1188. menu.hide();
  1189. } else {
  1190. _this.tag.find(".contents + .flex").addClass("active");
  1191. _this.tag.find(".content-wrapper").prop("scrollTop", 10000);
  1192. menu.show();
  1193. }
  1194. return false;
  1195. };
  1196. })(this));
  1197. $("body").on("click", (function(_this) {
  1198. return function() {
  1199. if (_this.tag) {
  1200. return _this.tag.find(".contents + .flex").removeClass("active");
  1201. }
  1202. };
  1203. })(this));
  1204. this.tag.find("#button-sign-publish").off("click touchend").on("click touchend", (function(_this) {
  1205. return function() {
  1206. var inner_path;
  1207. inner_path = _this.tag.find("#input-contents").val();
  1208. _this.wrapper.ws.cmd("fileRules", {
  1209. inner_path: inner_path
  1210. }, function(rules) {
  1211. var ref;
  1212. if (ref = _this.wrapper.site_info.auth_address, indexOf.call(rules.signers, ref) >= 0) {
  1213. return _this.publish(inner_path, null);
  1214. } else if (_this.wrapper.site_info.privatekey) {
  1215. return _this.publish(inner_path, "stored");
  1216. } else {
  1217. return _this.wrapper.displayPrompt("Enter your private key:", "password", "Sign", "", function(privatekey) {
  1218. return _this.publish(inner_path, privatekey);
  1219. });
  1220. }
  1221. });
  1222. return false;
  1223. };
  1224. })(this));
  1225. this.tag.find(".close").off("click touchend").on("click touchend", (function(_this) {
  1226. return function(e) {
  1227. _this.close();
  1228. return false;
  1229. };
  1230. })(this));
  1231. return this.loadGlobe();
  1232. };
  1233. Sidebar.prototype.close = function() {
  1234. this.move_lock = "x";
  1235. this.startDrag();
  1236. return this.stopDrag();
  1237. };
  1238. Sidebar.prototype.onClosed = function() {
  1239. $(window).off("resize");
  1240. $(window).on("resize", this.resized);
  1241. $(document.body).css("transition", "0.6s ease-in-out").removeClass("body-sidebar").on(transitionEnd, (function(_this) {
  1242. return function(e) {
  1243. if (e.target === document.body && !$(document.body).hasClass("body-sidebar") && !$(document.body).hasClass("body-console")) {
  1244. $(document.body).css("height", "auto").css("perspective", "").css("will-change", "").css("transition", "").off(transitionEnd);
  1245. return _this.unloadGlobe();
  1246. }
  1247. };
  1248. })(this));
  1249. return this.wrapper.setSiteInfo = this.original_set_site_info;
  1250. };
  1251. Sidebar.prototype.loadGlobe = function() {
  1252. if (this.tag.find(".globe").hasClass("loading")) {
  1253. return setTimeout(((function(_this) {
  1254. return function() {
  1255. var script_tag;
  1256. if (typeof DAT === "undefined") {
  1257. script_tag = $("<script>");
  1258. script_tag.attr("nonce", _this.wrapper.script_nonce);
  1259. script_tag.attr("src", "/uimedia/globe/all.js");
  1260. script_tag.on("load", _this.displayGlobe);
  1261. return document.head.appendChild(script_tag[0]);
  1262. } else {
  1263. return _this.displayGlobe();
  1264. }
  1265. };
  1266. })(this)), 600);
  1267. }
  1268. };
  1269. Sidebar.prototype.displayGlobe = function() {
  1270. var img;
  1271. img = new Image();
  1272. img.src = "/uimedia/globe/world.jpg";
  1273. return img.onload = (function(_this) {
  1274. return function() {
  1275. return _this.wrapper.ws.cmd("sidebarGetPeers", [], function(globe_data) {
  1276. var e, ref, ref1, ref2;
  1277. if (_this.globe) {
  1278. _this.globe.scene.remove(_this.globe.points);
  1279. _this.globe.addData(globe_data, {
  1280. format: 'magnitude',
  1281. name: "hello",
  1282. animated: false
  1283. });
  1284. _this.globe.createPoints();
  1285. return (ref = _this.tag) != null ? ref.find(".globe").removeClass("loading") : void 0;
  1286. } else if (typeof DAT !== "undefined") {
  1287. try {
  1288. _this.globe = new DAT.Globe(_this.tag.find(".globe")[0], {
  1289. "imgDir": "/uimedia/globe/"
  1290. });
  1291. _this.globe.addData(globe_data, {
  1292. format: 'magnitude',
  1293. name: "hello"
  1294. });
  1295. _this.globe.createPoints();
  1296. _this.globe.animate();
  1297. } catch (error) {
  1298. e = error;
  1299. console.log("WebGL error", e);
  1300. if ((ref1 = _this.tag) != null) {
  1301. ref1.find(".globe").addClass("error").text("WebGL not supported");
  1302. }
  1303. }
  1304. return (ref2 = _this.tag) != null ? ref2.find(".globe").removeClass("loading") : void 0;
  1305. }
  1306. });
  1307. };
  1308. })(this);
  1309. };
  1310. Sidebar.prototype.unloadGlobe = function() {
  1311. if (!this.globe) {
  1312. return false;
  1313. }
  1314. this.globe.unload();
  1315. return this.globe = null;
  1316. };
  1317. return Sidebar;
  1318. })(Class);
  1319. wrapper = window.wrapper;
  1320. setTimeout((function() {
  1321. return window.sidebar = new Sidebar(wrapper);
  1322. }), 500);
  1323. window.transitionEnd = 'transitionend webkitTransitionEnd oTransitionEnd otransitionend';
  1324. }).call(this);
  1325. /* ---- morphdom.js ---- */
  1326. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.morphdom = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  1327. var specialElHandlers = {
  1328. /**
  1329. * Needed for IE. Apparently IE doesn't think
  1330. * that "selected" is an attribute when reading
  1331. * over the attributes using selectEl.attributes
  1332. */
  1333. OPTION: function(fromEl, toEl) {
  1334. if ((fromEl.selected = toEl.selected)) {
  1335. fromEl.setAttribute('selected', '');
  1336. } else {
  1337. fromEl.removeAttribute('selected', '');
  1338. }
  1339. },
  1340. /**
  1341. * The "value" attribute is special for the <input> element
  1342. * since it sets the initial value. Changing the "value"
  1343. * attribute without changing the "value" property will have
  1344. * no effect since it is only used to the set the initial value.
  1345. * Similar for the "checked" attribute.
  1346. */
  1347. /*INPUT: function(fromEl, toEl) {
  1348. fromEl.checked = toEl.checked;
  1349. fromEl.value = toEl.value;
  1350. if (!toEl.hasAttribute('checked')) {
  1351. fromEl.removeAttribute('checked');
  1352. }
  1353. if (!toEl.hasAttribute('value')) {
  1354. fromEl.removeAttribute('value');
  1355. }
  1356. }*/
  1357. };
  1358. function noop() {}
  1359. /**
  1360. * Loop over all of the attributes on the target node and make sure the
  1361. * original DOM node has the same attributes. If an attribute
  1362. * found on the original node is not on the new node then remove it from
  1363. * the original node
  1364. * @param {HTMLElement} fromNode
  1365. * @param {HTMLElement} toNode
  1366. */
  1367. function morphAttrs(fromNode, toNode) {
  1368. var attrs = toNode.attributes;
  1369. var i;
  1370. var attr;
  1371. var attrName;
  1372. var attrValue;
  1373. var foundAttrs = {};
  1374. for (i=attrs.length-1; i>=0; i--) {
  1375. attr = attrs[i];
  1376. if (attr.specified !== false) {
  1377. attrName = attr.name;
  1378. attrValue = attr.value;
  1379. foundAttrs[attrName] = true;
  1380. if (fromNode.getAttribute(attrName) !== attrValue) {
  1381. fromNode.setAttribute(attrName, attrValue);
  1382. }
  1383. }
  1384. }
  1385. // Delete any extra attributes found on the original DOM element that weren't
  1386. // found on the target element.
  1387. attrs = fromNode.attributes;
  1388. for (i=attrs.length-1; i>=0; i--) {
  1389. attr = attrs[i];
  1390. if (attr.specified !== false) {
  1391. attrName = attr.name;
  1392. if (!foundAttrs.hasOwnProperty(attrName)) {
  1393. fromNode.removeAttribute(attrName);
  1394. }
  1395. }
  1396. }
  1397. }
  1398. /**
  1399. * Copies the children of one DOM element to another DOM element
  1400. */
  1401. function moveChildren(from, to) {
  1402. var curChild = from.firstChild;
  1403. while(curChild) {
  1404. var nextChild = curChild.nextSibling;
  1405. to.appendChild(curChild);
  1406. curChild = nextChild;
  1407. }
  1408. return to;
  1409. }
  1410. function morphdom(fromNode, toNode, options) {
  1411. if (!options) {
  1412. options = {};
  1413. }
  1414. if (typeof toNode === 'string') {
  1415. var newBodyEl = document.createElement('body');
  1416. newBodyEl.innerHTML = toNode;
  1417. toNode = newBodyEl.childNodes[0];
  1418. }
  1419. var savedEls = {}; // Used to save off DOM elements with IDs
  1420. var unmatchedEls = {};
  1421. var onNodeDiscarded = options.onNodeDiscarded || noop;
  1422. var onBeforeMorphEl = options.onBeforeMorphEl || noop;
  1423. var onBeforeMorphElChildren = options.onBeforeMorphElChildren || noop;
  1424. function removeNodeHelper(node, nestedInSavedEl) {
  1425. var id = node.id;
  1426. // If the node has an ID then save it off since we will want
  1427. // to reuse it in case the target DOM tree has a DOM element
  1428. // with the same ID
  1429. if (id) {
  1430. savedEls[id] = node;
  1431. } else if (!nestedInSavedEl) {
  1432. // If we are not nested in a saved element then we know that this node has been
  1433. // completely discarded and will not exist in the final DOM.
  1434. onNodeDiscarded(node);
  1435. }
  1436. if (node.nodeType === 1) {
  1437. var curChild = node.firstChild;
  1438. while(curChild) {
  1439. removeNodeHelper(curChild, nestedInSavedEl || id);
  1440. curChild = curChild.nextSibling;
  1441. }
  1442. }
  1443. }
  1444. function walkDiscardedChildNodes(node) {
  1445. if (node.nodeType === 1) {
  1446. var curChild = node.firstChild;
  1447. while(curChild) {
  1448. if (!curChild.id) {
  1449. // We only want to handle nodes that don't have an ID to avoid double
  1450. // walking the same saved element.
  1451. onNodeDiscarded(curChild);
  1452. // Walk recursively
  1453. walkDiscardedChildNodes(curChild);
  1454. }
  1455. curChild = curChild.nextSibling;
  1456. }
  1457. }
  1458. }
  1459. function removeNode(node, parentNode, alreadyVisited) {
  1460. parentNode.removeChild(node);
  1461. if (alreadyVisited) {
  1462. if (!node.id) {
  1463. onNodeDiscarded(node);
  1464. walkDiscardedChildNodes(node);
  1465. }
  1466. } else {
  1467. removeNodeHelper(node);
  1468. }
  1469. }
  1470. function morphEl(fromNode, toNode, alreadyVisited) {
  1471. if (toNode.id) {
  1472. // If an element with an ID is being morphed then it is will be in the final
  1473. // DOM so clear it out of the saved elements collection
  1474. delete savedEls[toNode.id];
  1475. }
  1476. if (onBeforeMorphEl(fromNode, toNode) === false) {
  1477. return;
  1478. }
  1479. morphAttrs(fromNode, toNode);
  1480. if (onBeforeMorphElChildren(fromNode, toNode) === false) {
  1481. return;
  1482. }
  1483. var curToNodeChild = toNode.firstChild;
  1484. var curFromNodeChild = fromNode.firstChild;
  1485. var curToNodeId;
  1486. var fromNextSibling;
  1487. var toNextSibling;
  1488. var savedEl;
  1489. var unmatchedEl;
  1490. outer: while(curToNodeChild) {
  1491. toNextSibling = curToNodeChild.nextSibling;
  1492. curToNodeId = curToNodeChild.id;
  1493. while(curFromNodeChild) {
  1494. var curFromNodeId = curFromNodeChild.id;
  1495. fromNextSibling = curFromNodeChild.nextSibling;
  1496. if (!alreadyVisited) {
  1497. if (curFromNodeId && (unmatchedEl = unmatchedEls[curFromNodeId])) {
  1498. unmatchedEl.parentNode.replaceChild(curFromNodeChild, unmatchedEl);
  1499. morphEl(curFromNodeChild, unmatchedEl, alreadyVisited);
  1500. curFromNodeChild = fromNextSibling;
  1501. continue;
  1502. }
  1503. }
  1504. var curFromNodeType = curFromNodeChild.nodeType;
  1505. if (curFromNodeType === curToNodeChild.nodeType) {
  1506. var isCompatible = false;
  1507. if (curFromNodeType === 1) { // Both nodes being compared are Element nodes
  1508. if (curFromNodeChild.tagName === curToNodeChild.tagName) {
  1509. // We have compatible DOM elements
  1510. if (curFromNodeId || curToNodeId) {
  1511. // If either DOM element has an ID then we handle
  1512. // those differently since we want to match up
  1513. // by ID
  1514. if (curToNodeId === curFromNodeId) {
  1515. isCompatible = true;
  1516. }
  1517. } else {
  1518. isCompatible = true;
  1519. }
  1520. }
  1521. if (isCompatible) {
  1522. // We found compatible DOM elements so add a
  1523. // task to morph the compatible DOM elements
  1524. morphEl(curFromNodeChild, curToNodeChild, alreadyVisited);
  1525. }
  1526. } else if (curFromNodeType === 3) { // Both nodes being compared are Text nodes
  1527. isCompatible = true;
  1528. curFromNodeChild.nodeValue = curToNodeChild.nodeValue;
  1529. }
  1530. if (isCompatible) {
  1531. curToNodeChild = toNextSibling;
  1532. curFromNodeChild = fromNextSibling;
  1533. continue outer;
  1534. }
  1535. }
  1536. // No compatible match so remove the old node from the DOM
  1537. removeNode(curFromNodeChild, fromNode, alreadyVisited);
  1538. curFromNodeChild = fromNextSibling;
  1539. }
  1540. if (curToNodeId) {
  1541. if ((savedEl = savedEls[curToNodeId])) {
  1542. morphEl(savedEl, curToNodeChild, true);
  1543. curToNodeChild = savedEl; // We want to append the saved element instead
  1544. } else {
  1545. // The current DOM element in the target tree has an ID
  1546. // but we did not find a match in any of the corresponding
  1547. // siblings. We just put the target element in the old DOM tree
  1548. // but if we later find an element in the old DOM tree that has
  1549. // a matching ID then we will replace the target element
  1550. // with the corresponding old element and morph the old element
  1551. unmatchedEls[curToNodeId] = curToNodeChild;
  1552. }
  1553. }
  1554. // If we got this far then we did not find a candidate match for our "to node"
  1555. // and we exhausted all of the children "from" nodes. Therefore, we will just
  1556. // append the current "to node" to the end
  1557. fromNode.appendChild(curToNodeChild);
  1558. curToNodeChild = toNextSibling;
  1559. curFromNodeChild = fromNextSibling;
  1560. }
  1561. // We have processed all of the "to nodes". If curFromNodeChild is non-null then
  1562. // we still have some from nodes left over that need to be removed
  1563. while(curFromNodeChild) {
  1564. fromNextSibling = curFromNodeChild.nextSibling;
  1565. removeNode(curFromNodeChild, fromNode, alreadyVisited);
  1566. curFromNodeChild = fromNextSibling;
  1567. }
  1568. var specialElHandler = specialElHandlers[fromNode.tagName];
  1569. if (specialElHandler) {
  1570. specialElHandler(fromNode, toNode);
  1571. }
  1572. }
  1573. var morphedNode = fromNode;
  1574. var morphedNodeType = morphedNode.nodeType;
  1575. var toNodeType = toNode.nodeType;
  1576. // Handle the case where we are given two DOM nodes that are not
  1577. // compatible (e.g. <div> --> <span> or <div> --> TEXT)
  1578. if (morphedNodeType === 1) {
  1579. if (toNodeType === 1) {
  1580. if (morphedNode.tagName !== toNode.tagName) {
  1581. onNodeDiscarded(fromNode);
  1582. morphedNode = moveChildren(morphedNode, document.createElement(toNode.tagName));
  1583. }
  1584. } else {
  1585. // Going from an element node to a text node
  1586. return toNode;
  1587. }
  1588. } else if (morphedNodeType === 3) { // Text node
  1589. if (toNodeType === 3) {
  1590. morphedNode.nodeValue = toNode.nodeValue;
  1591. return morphedNode;
  1592. } else {
  1593. onNodeDiscarded(fromNode);
  1594. // Text node to something else
  1595. return toNode;
  1596. }
  1597. }
  1598. morphEl(morphedNode, toNode, false);
  1599. // Fire the "onNodeDiscarded" event for any saved elements
  1600. // that never found a new home in the morphed DOM
  1601. for (var savedElId in savedEls) {
  1602. if (savedEls.hasOwnProperty(savedElId)) {
  1603. var savedEl = savedEls[savedElId];
  1604. onNodeDiscarded(savedEl);
  1605. walkDiscardedChildNodes(savedEl);
  1606. }
  1607. }
  1608. if (morphedNode !== fromNode && fromNode.parentNode) {
  1609. fromNode.parentNode.replaceChild(morphedNode, fromNode);
  1610. }
  1611. return morphedNode;
  1612. }
  1613. module.exports = morphdom;
  1614. },{}]},{},[1])(1)
  1615. });