all.js 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  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-delete").off("click touchend").on("click touchend", (function(_this) {
  1035. return function() {
  1036. _this.handleSiteDeleteClick();
  1037. return false;
  1038. };
  1039. })(this));
  1040. this.tag.find("#checkbox-owned").off("click touchend").on("click touchend", (function(_this) {
  1041. return function() {
  1042. var owned;
  1043. owned = _this.tag.find("#checkbox-owned").is(":checked");
  1044. return _this.wrapper.ws.cmd("siteSetOwned", [owned], function(res_set_owned) {
  1045. _this.log("Owned", owned);
  1046. if (owned) {
  1047. return _this.wrapper.ws.cmd("siteRecoverPrivatekey", [], function(res_recover) {
  1048. if (res_recover === "ok") {
  1049. return _this.wrapper.notifications.add("recover", "done", "Private key recovered from master seed", 5000);
  1050. } else {
  1051. return _this.log("Unable to recover private key: " + res_recover.error);
  1052. }
  1053. });
  1054. }
  1055. });
  1056. };
  1057. })(this));
  1058. this.tag.find("#checkbox-autodownloadoptional").off("click touchend").on("click touchend", (function(_this) {
  1059. return function() {
  1060. return _this.wrapper.ws.cmd("siteSetAutodownloadoptional", [_this.tag.find("#checkbox-autodownloadoptional").is(":checked")]);
  1061. };
  1062. })(this));
  1063. this.tag.find("#button-identity").off("click touchend").on("click touchend", (function(_this) {
  1064. return function() {
  1065. _this.wrapper.ws.cmd("certSelect");
  1066. return false;
  1067. };
  1068. })(this));
  1069. this.tag.find("#button-settings").off("click touchend").on("click touchend", (function(_this) {
  1070. return function() {
  1071. _this.wrapper.ws.cmd("fileGet", "content.json", function(res) {
  1072. var data, json_raw;
  1073. data = JSON.parse(res);
  1074. data["title"] = $("#settings-title").val();
  1075. data["description"] = $("#settings-description").val();
  1076. json_raw = unescape(encodeURIComponent(JSON.stringify(data, void 0, '\t')));
  1077. return _this.wrapper.ws.cmd("fileWrite", ["content.json", btoa(json_raw), true], function(res) {
  1078. if (res !== "ok") {
  1079. return _this.wrapper.notifications.add("file-write", "error", "File write error: " + res);
  1080. } else {
  1081. _this.wrapper.notifications.add("file-write", "done", "Site settings saved!", 5000);
  1082. if (_this.wrapper.site_info.privatekey) {
  1083. _this.wrapper.ws.cmd("siteSign", {
  1084. privatekey: "stored",
  1085. inner_path: "content.json",
  1086. update_changed_files: true
  1087. });
  1088. }
  1089. return _this.updateHtmlTag();
  1090. }
  1091. });
  1092. });
  1093. return false;
  1094. };
  1095. })(this));
  1096. this.tag.find("#link-directory").off("click touchend").on("click touchend", (function(_this) {
  1097. return function() {
  1098. _this.wrapper.ws.cmd("serverShowdirectory", ["site", _this.wrapper.site_info.address]);
  1099. return false;
  1100. };
  1101. })(this));
  1102. this.tag.find("#link-copypeers").off("click touchend").on("click touchend", (function(_this) {
  1103. return function(e) {
  1104. var copy_text, handler;
  1105. copy_text = e.currentTarget.href;
  1106. handler = function(e) {
  1107. e.clipboardData.setData('text/plain', copy_text);
  1108. e.preventDefault();
  1109. _this.wrapper.notifications.add("copy", "done", "Site address with peers copied to your clipboard", 5000);
  1110. return document.removeEventListener('copy', handler, true);
  1111. };
  1112. document.addEventListener('copy', handler, true);
  1113. document.execCommand('copy');
  1114. return false;
  1115. };
  1116. })(this));
  1117. $(document).on("click touchend", (function(_this) {
  1118. return function() {
  1119. var ref, ref1;
  1120. if ((ref = _this.tag) != null) {
  1121. ref.find("#button-sign-publish-menu").removeClass("visible");
  1122. }
  1123. return (ref1 = _this.tag) != null ? ref1.find(".contents + .flex").removeClass("sign-publish-flex") : void 0;
  1124. };
  1125. })(this));
  1126. this.tag.find(".contents-content").off("click touchend").on("click touchend", (function(_this) {
  1127. return function(e) {
  1128. $("#input-contents").val(e.currentTarget.innerText);
  1129. return false;
  1130. };
  1131. })(this));
  1132. menu = new Menu(this.tag.find("#menu-sign-publish"));
  1133. menu.elem.css("margin-top", "-130px");
  1134. menu.addItem("Sign", (function(_this) {
  1135. return function() {
  1136. var inner_path;
  1137. inner_path = _this.tag.find("#input-contents").val();
  1138. _this.wrapper.ws.cmd("fileRules", {
  1139. inner_path: inner_path
  1140. }, function(rules) {
  1141. var ref;
  1142. if (ref = _this.wrapper.site_info.auth_address, indexOf.call(rules.signers, ref) >= 0) {
  1143. return _this.sign(inner_path);
  1144. } else if (_this.wrapper.site_info.privatekey) {
  1145. return _this.sign(inner_path, "stored");
  1146. } else {
  1147. return _this.wrapper.displayPrompt("Enter your private key:", "password", "Sign", "", function(privatekey) {
  1148. return _this.sign(inner_path, privatekey);
  1149. });
  1150. }
  1151. });
  1152. _this.tag.find(".contents + .flex").removeClass("active");
  1153. return menu.hide();
  1154. };
  1155. })(this));
  1156. menu.addItem("Publish", (function(_this) {
  1157. return function() {
  1158. var inner_path;
  1159. inner_path = _this.tag.find("#input-contents").val();
  1160. _this.wrapper.ws.cmd("sitePublish", {
  1161. "inner_path": inner_path,
  1162. "sign": false
  1163. });
  1164. _this.tag.find(".contents + .flex").removeClass("active");
  1165. return menu.hide();
  1166. };
  1167. })(this));
  1168. this.tag.find("#menu-sign-publish").off("click touchend").on("click touchend", (function(_this) {
  1169. return function() {
  1170. if (window.visible_menu === menu) {
  1171. _this.tag.find(".contents + .flex").removeClass("active");
  1172. menu.hide();
  1173. } else {
  1174. _this.tag.find(".contents + .flex").addClass("active");
  1175. _this.tag.find(".content-wrapper").prop("scrollTop", 10000);
  1176. menu.show();
  1177. }
  1178. return false;
  1179. };
  1180. })(this));
  1181. $("body").on("click", (function(_this) {
  1182. return function() {
  1183. if (_this.tag) {
  1184. return _this.tag.find(".contents + .flex").removeClass("active");
  1185. }
  1186. };
  1187. })(this));
  1188. this.tag.find("#button-sign-publish").off("click touchend").on("click touchend", (function(_this) {
  1189. return function() {
  1190. var inner_path;
  1191. inner_path = _this.tag.find("#input-contents").val();
  1192. _this.wrapper.ws.cmd("fileRules", {
  1193. inner_path: inner_path
  1194. }, function(rules) {
  1195. var ref;
  1196. if (ref = _this.wrapper.site_info.auth_address, indexOf.call(rules.signers, ref) >= 0) {
  1197. return _this.publish(inner_path, null);
  1198. } else if (_this.wrapper.site_info.privatekey) {
  1199. return _this.publish(inner_path, "stored");
  1200. } else {
  1201. return _this.wrapper.displayPrompt("Enter your private key:", "password", "Sign", "", function(privatekey) {
  1202. return _this.publish(inner_path, privatekey);
  1203. });
  1204. }
  1205. });
  1206. return false;
  1207. };
  1208. })(this));
  1209. this.tag.find(".close").off("click touchend").on("click touchend", (function(_this) {
  1210. return function(e) {
  1211. _this.close();
  1212. return false;
  1213. };
  1214. })(this));
  1215. return this.loadGlobe();
  1216. };
  1217. Sidebar.prototype.close = function() {
  1218. this.move_lock = "x";
  1219. this.startDrag();
  1220. return this.stopDrag();
  1221. };
  1222. Sidebar.prototype.onClosed = function() {
  1223. $(window).off("resize");
  1224. $(window).on("resize", this.resized);
  1225. $(document.body).css("transition", "0.6s ease-in-out").removeClass("body-sidebar").on(transitionEnd, (function(_this) {
  1226. return function(e) {
  1227. if (e.target === document.body && !$(document.body).hasClass("body-sidebar") && !$(document.body).hasClass("body-console")) {
  1228. $(document.body).css("height", "auto").css("perspective", "").css("will-change", "").css("transition", "").off(transitionEnd);
  1229. return _this.unloadGlobe();
  1230. }
  1231. };
  1232. })(this));
  1233. return this.wrapper.setSiteInfo = this.original_set_site_info;
  1234. };
  1235. Sidebar.prototype.loadGlobe = function() {
  1236. if (this.tag.find(".globe").hasClass("loading")) {
  1237. return setTimeout(((function(_this) {
  1238. return function() {
  1239. var script_tag;
  1240. if (typeof DAT === "undefined") {
  1241. script_tag = $("<script>");
  1242. script_tag.attr("nonce", _this.wrapper.script_nonce);
  1243. script_tag.attr("src", "/uimedia/globe/all.js");
  1244. script_tag.on("load", _this.displayGlobe);
  1245. return document.head.appendChild(script_tag[0]);
  1246. } else {
  1247. return _this.displayGlobe();
  1248. }
  1249. };
  1250. })(this)), 600);
  1251. }
  1252. };
  1253. Sidebar.prototype.displayGlobe = function() {
  1254. var img;
  1255. img = new Image();
  1256. img.src = "/uimedia/globe/world.jpg";
  1257. return img.onload = (function(_this) {
  1258. return function() {
  1259. return _this.wrapper.ws.cmd("sidebarGetPeers", [], function(globe_data) {
  1260. var e, ref, ref1, ref2;
  1261. if (_this.globe) {
  1262. _this.globe.scene.remove(_this.globe.points);
  1263. _this.globe.addData(globe_data, {
  1264. format: 'magnitude',
  1265. name: "hello",
  1266. animated: false
  1267. });
  1268. _this.globe.createPoints();
  1269. return (ref = _this.tag) != null ? ref.find(".globe").removeClass("loading") : void 0;
  1270. } else if (typeof DAT !== "undefined") {
  1271. try {
  1272. _this.globe = new DAT.Globe(_this.tag.find(".globe")[0], {
  1273. "imgDir": "/uimedia/globe/"
  1274. });
  1275. _this.globe.addData(globe_data, {
  1276. format: 'magnitude',
  1277. name: "hello"
  1278. });
  1279. _this.globe.createPoints();
  1280. _this.globe.animate();
  1281. } catch (error) {
  1282. e = error;
  1283. console.log("WebGL error", e);
  1284. if ((ref1 = _this.tag) != null) {
  1285. ref1.find(".globe").addClass("error").text("WebGL not supported");
  1286. }
  1287. }
  1288. return (ref2 = _this.tag) != null ? ref2.find(".globe").removeClass("loading") : void 0;
  1289. }
  1290. });
  1291. };
  1292. })(this);
  1293. };
  1294. Sidebar.prototype.unloadGlobe = function() {
  1295. if (!this.globe) {
  1296. return false;
  1297. }
  1298. this.globe.unload();
  1299. return this.globe = null;
  1300. };
  1301. return Sidebar;
  1302. })(Class);
  1303. wrapper = window.wrapper;
  1304. setTimeout((function() {
  1305. return window.sidebar = new Sidebar(wrapper);
  1306. }), 500);
  1307. window.transitionEnd = 'transitionend webkitTransitionEnd oTransitionEnd otransitionend';
  1308. }).call(this);
  1309. /* ---- morphdom.js ---- */
  1310. (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){
  1311. var specialElHandlers = {
  1312. /**
  1313. * Needed for IE. Apparently IE doesn't think
  1314. * that "selected" is an attribute when reading
  1315. * over the attributes using selectEl.attributes
  1316. */
  1317. OPTION: function(fromEl, toEl) {
  1318. if ((fromEl.selected = toEl.selected)) {
  1319. fromEl.setAttribute('selected', '');
  1320. } else {
  1321. fromEl.removeAttribute('selected', '');
  1322. }
  1323. },
  1324. /**
  1325. * The "value" attribute is special for the <input> element
  1326. * since it sets the initial value. Changing the "value"
  1327. * attribute without changing the "value" property will have
  1328. * no effect since it is only used to the set the initial value.
  1329. * Similar for the "checked" attribute.
  1330. */
  1331. /*INPUT: function(fromEl, toEl) {
  1332. fromEl.checked = toEl.checked;
  1333. fromEl.value = toEl.value;
  1334. if (!toEl.hasAttribute('checked')) {
  1335. fromEl.removeAttribute('checked');
  1336. }
  1337. if (!toEl.hasAttribute('value')) {
  1338. fromEl.removeAttribute('value');
  1339. }
  1340. }*/
  1341. };
  1342. function noop() {}
  1343. /**
  1344. * Loop over all of the attributes on the target node and make sure the
  1345. * original DOM node has the same attributes. If an attribute
  1346. * found on the original node is not on the new node then remove it from
  1347. * the original node
  1348. * @param {HTMLElement} fromNode
  1349. * @param {HTMLElement} toNode
  1350. */
  1351. function morphAttrs(fromNode, toNode) {
  1352. var attrs = toNode.attributes;
  1353. var i;
  1354. var attr;
  1355. var attrName;
  1356. var attrValue;
  1357. var foundAttrs = {};
  1358. for (i=attrs.length-1; i>=0; i--) {
  1359. attr = attrs[i];
  1360. if (attr.specified !== false) {
  1361. attrName = attr.name;
  1362. attrValue = attr.value;
  1363. foundAttrs[attrName] = true;
  1364. if (fromNode.getAttribute(attrName) !== attrValue) {
  1365. fromNode.setAttribute(attrName, attrValue);
  1366. }
  1367. }
  1368. }
  1369. // Delete any extra attributes found on the original DOM element that weren't
  1370. // found on the target element.
  1371. attrs = fromNode.attributes;
  1372. for (i=attrs.length-1; i>=0; i--) {
  1373. attr = attrs[i];
  1374. if (attr.specified !== false) {
  1375. attrName = attr.name;
  1376. if (!foundAttrs.hasOwnProperty(attrName)) {
  1377. fromNode.removeAttribute(attrName);
  1378. }
  1379. }
  1380. }
  1381. }
  1382. /**
  1383. * Copies the children of one DOM element to another DOM element
  1384. */
  1385. function moveChildren(from, to) {
  1386. var curChild = from.firstChild;
  1387. while(curChild) {
  1388. var nextChild = curChild.nextSibling;
  1389. to.appendChild(curChild);
  1390. curChild = nextChild;
  1391. }
  1392. return to;
  1393. }
  1394. function morphdom(fromNode, toNode, options) {
  1395. if (!options) {
  1396. options = {};
  1397. }
  1398. if (typeof toNode === 'string') {
  1399. var newBodyEl = document.createElement('body');
  1400. newBodyEl.innerHTML = toNode;
  1401. toNode = newBodyEl.childNodes[0];
  1402. }
  1403. var savedEls = {}; // Used to save off DOM elements with IDs
  1404. var unmatchedEls = {};
  1405. var onNodeDiscarded = options.onNodeDiscarded || noop;
  1406. var onBeforeMorphEl = options.onBeforeMorphEl || noop;
  1407. var onBeforeMorphElChildren = options.onBeforeMorphElChildren || noop;
  1408. function removeNodeHelper(node, nestedInSavedEl) {
  1409. var id = node.id;
  1410. // If the node has an ID then save it off since we will want
  1411. // to reuse it in case the target DOM tree has a DOM element
  1412. // with the same ID
  1413. if (id) {
  1414. savedEls[id] = node;
  1415. } else if (!nestedInSavedEl) {
  1416. // If we are not nested in a saved element then we know that this node has been
  1417. // completely discarded and will not exist in the final DOM.
  1418. onNodeDiscarded(node);
  1419. }
  1420. if (node.nodeType === 1) {
  1421. var curChild = node.firstChild;
  1422. while(curChild) {
  1423. removeNodeHelper(curChild, nestedInSavedEl || id);
  1424. curChild = curChild.nextSibling;
  1425. }
  1426. }
  1427. }
  1428. function walkDiscardedChildNodes(node) {
  1429. if (node.nodeType === 1) {
  1430. var curChild = node.firstChild;
  1431. while(curChild) {
  1432. if (!curChild.id) {
  1433. // We only want to handle nodes that don't have an ID to avoid double
  1434. // walking the same saved element.
  1435. onNodeDiscarded(curChild);
  1436. // Walk recursively
  1437. walkDiscardedChildNodes(curChild);
  1438. }
  1439. curChild = curChild.nextSibling;
  1440. }
  1441. }
  1442. }
  1443. function removeNode(node, parentNode, alreadyVisited) {
  1444. parentNode.removeChild(node);
  1445. if (alreadyVisited) {
  1446. if (!node.id) {
  1447. onNodeDiscarded(node);
  1448. walkDiscardedChildNodes(node);
  1449. }
  1450. } else {
  1451. removeNodeHelper(node);
  1452. }
  1453. }
  1454. function morphEl(fromNode, toNode, alreadyVisited) {
  1455. if (toNode.id) {
  1456. // If an element with an ID is being morphed then it is will be in the final
  1457. // DOM so clear it out of the saved elements collection
  1458. delete savedEls[toNode.id];
  1459. }
  1460. if (onBeforeMorphEl(fromNode, toNode) === false) {
  1461. return;
  1462. }
  1463. morphAttrs(fromNode, toNode);
  1464. if (onBeforeMorphElChildren(fromNode, toNode) === false) {
  1465. return;
  1466. }
  1467. var curToNodeChild = toNode.firstChild;
  1468. var curFromNodeChild = fromNode.firstChild;
  1469. var curToNodeId;
  1470. var fromNextSibling;
  1471. var toNextSibling;
  1472. var savedEl;
  1473. var unmatchedEl;
  1474. outer: while(curToNodeChild) {
  1475. toNextSibling = curToNodeChild.nextSibling;
  1476. curToNodeId = curToNodeChild.id;
  1477. while(curFromNodeChild) {
  1478. var curFromNodeId = curFromNodeChild.id;
  1479. fromNextSibling = curFromNodeChild.nextSibling;
  1480. if (!alreadyVisited) {
  1481. if (curFromNodeId && (unmatchedEl = unmatchedEls[curFromNodeId])) {
  1482. unmatchedEl.parentNode.replaceChild(curFromNodeChild, unmatchedEl);
  1483. morphEl(curFromNodeChild, unmatchedEl, alreadyVisited);
  1484. curFromNodeChild = fromNextSibling;
  1485. continue;
  1486. }
  1487. }
  1488. var curFromNodeType = curFromNodeChild.nodeType;
  1489. if (curFromNodeType === curToNodeChild.nodeType) {
  1490. var isCompatible = false;
  1491. if (curFromNodeType === 1) { // Both nodes being compared are Element nodes
  1492. if (curFromNodeChild.tagName === curToNodeChild.tagName) {
  1493. // We have compatible DOM elements
  1494. if (curFromNodeId || curToNodeId) {
  1495. // If either DOM element has an ID then we handle
  1496. // those differently since we want to match up
  1497. // by ID
  1498. if (curToNodeId === curFromNodeId) {
  1499. isCompatible = true;
  1500. }
  1501. } else {
  1502. isCompatible = true;
  1503. }
  1504. }
  1505. if (isCompatible) {
  1506. // We found compatible DOM elements so add a
  1507. // task to morph the compatible DOM elements
  1508. morphEl(curFromNodeChild, curToNodeChild, alreadyVisited);
  1509. }
  1510. } else if (curFromNodeType === 3) { // Both nodes being compared are Text nodes
  1511. isCompatible = true;
  1512. curFromNodeChild.nodeValue = curToNodeChild.nodeValue;
  1513. }
  1514. if (isCompatible) {
  1515. curToNodeChild = toNextSibling;
  1516. curFromNodeChild = fromNextSibling;
  1517. continue outer;
  1518. }
  1519. }
  1520. // No compatible match so remove the old node from the DOM
  1521. removeNode(curFromNodeChild, fromNode, alreadyVisited);
  1522. curFromNodeChild = fromNextSibling;
  1523. }
  1524. if (curToNodeId) {
  1525. if ((savedEl = savedEls[curToNodeId])) {
  1526. morphEl(savedEl, curToNodeChild, true);
  1527. curToNodeChild = savedEl; // We want to append the saved element instead
  1528. } else {
  1529. // The current DOM element in the target tree has an ID
  1530. // but we did not find a match in any of the corresponding
  1531. // siblings. We just put the target element in the old DOM tree
  1532. // but if we later find an element in the old DOM tree that has
  1533. // a matching ID then we will replace the target element
  1534. // with the corresponding old element and morph the old element
  1535. unmatchedEls[curToNodeId] = curToNodeChild;
  1536. }
  1537. }
  1538. // If we got this far then we did not find a candidate match for our "to node"
  1539. // and we exhausted all of the children "from" nodes. Therefore, we will just
  1540. // append the current "to node" to the end
  1541. fromNode.appendChild(curToNodeChild);
  1542. curToNodeChild = toNextSibling;
  1543. curFromNodeChild = fromNextSibling;
  1544. }
  1545. // We have processed all of the "to nodes". If curFromNodeChild is non-null then
  1546. // we still have some from nodes left over that need to be removed
  1547. while(curFromNodeChild) {
  1548. fromNextSibling = curFromNodeChild.nextSibling;
  1549. removeNode(curFromNodeChild, fromNode, alreadyVisited);
  1550. curFromNodeChild = fromNextSibling;
  1551. }
  1552. var specialElHandler = specialElHandlers[fromNode.tagName];
  1553. if (specialElHandler) {
  1554. specialElHandler(fromNode, toNode);
  1555. }
  1556. }
  1557. var morphedNode = fromNode;
  1558. var morphedNodeType = morphedNode.nodeType;
  1559. var toNodeType = toNode.nodeType;
  1560. // Handle the case where we are given two DOM nodes that are not
  1561. // compatible (e.g. <div> --> <span> or <div> --> TEXT)
  1562. if (morphedNodeType === 1) {
  1563. if (toNodeType === 1) {
  1564. if (morphedNode.tagName !== toNode.tagName) {
  1565. onNodeDiscarded(fromNode);
  1566. morphedNode = moveChildren(morphedNode, document.createElement(toNode.tagName));
  1567. }
  1568. } else {
  1569. // Going from an element node to a text node
  1570. return toNode;
  1571. }
  1572. } else if (morphedNodeType === 3) { // Text node
  1573. if (toNodeType === 3) {
  1574. morphedNode.nodeValue = toNode.nodeValue;
  1575. return morphedNode;
  1576. } else {
  1577. onNodeDiscarded(fromNode);
  1578. // Text node to something else
  1579. return toNode;
  1580. }
  1581. }
  1582. morphEl(morphedNode, toNode, false);
  1583. // Fire the "onNodeDiscarded" event for any saved elements
  1584. // that never found a new home in the morphed DOM
  1585. for (var savedElId in savedEls) {
  1586. if (savedEls.hasOwnProperty(savedElId)) {
  1587. var savedEl = savedEls[savedElId];
  1588. onNodeDiscarded(savedEl);
  1589. walkDiscardedChildNodes(savedEl);
  1590. }
  1591. }
  1592. if (morphedNode !== fromNode && fromNode.parentNode) {
  1593. fromNode.parentNode.replaceChild(morphedNode, fromNode);
  1594. }
  1595. return morphedNode;
  1596. }
  1597. module.exports = morphdom;
  1598. },{}]},{},[1])(1)
  1599. });