Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 46.11 KB | None | 0 0
  1. ;(function() {
  2. ////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // ReactJS / JSX-based code here //
  5. // //
  6. ////////////////////////////////////////////////////////////////////////////
  7. var converter = new showdown.Converter();
  8. var activities_logged_today = false;
  9. var listen = null;
  10. var one_shot = false;
  11. var scratchpad_is_initialized = false;
  12. var scratchpad_value = null;
  13. var todo_item_count = 0;
  14. var get_words = function(count) {
  15. var workbench = [];
  16. for(var index = 0; index < count; index += 1) {
  17. workbench.push(WORDS[Math.floor(Math.random() * WORDS.length)]);
  18. }
  19. return workbench.join('-');
  20. }
  21. var todo_item_names = [ 'Completed',
  22. 'Delete',
  23. 'Invisible',
  24. 'Background',
  25. 'You Decide',
  26. 'In Progress',
  27. 'Important',
  28. 'In Question',
  29. 'Problems'];
  30. var userid = 0;
  31. jQuery('#logout').click(function() {
  32. if (Modernizr.localstorage) {
  33. localStorage.removeItem('Calendar');
  34. localStorage.removeItem('Todo');
  35. localStorage.removeItem('Scratchpad');
  36. }
  37. });
  38. var populate_state = function(state, new_data) {
  39. for (field in new_data) {
  40. if (new_data.hasOwnProperty(field)) {
  41. state[field] = new_data[field];
  42. }
  43. }
  44. state.initialized = true;
  45. }
  46. var restore = function(identifier, default_value, state, callback) {
  47. populate_state(state, default_value);
  48. state.initialized = false;
  49. var complete = function(jqxhr) {
  50. if (jqxhr.responseText === 'undefined' || jqxhr.responseText.length &&
  51. jqxhr.responseText[0] === '<' || jqxhr.responseText === '') {
  52. // We ignore non-JSON a responses
  53. } else {
  54. populate_state(state, JSON.parse(jqxhr.responseText));
  55. }
  56. callback();
  57. state.initialized = true;
  58. }
  59. jQuery.ajax('/restore', {
  60. 'complete': complete,
  61. 'data': {
  62. 'identifier': identifier,
  63. 'userid': userid
  64. },
  65. 'method': 'POST',
  66. });
  67. if (Modernizr.localstorage) {
  68. if (localStorage[identifier] === null || localStorage[identifier]
  69. === undefined) {
  70. return default_value;
  71. } else {
  72. return JSON.parse(localStorage[identifier]);
  73. }
  74. } else {
  75. return default_value;
  76. }
  77. }
  78. var save = function(identifier, data) {
  79. if (Modernizr.localstorage) {
  80. localStorage[identifier] = JSON.stringify(data);
  81. }
  82. jQuery.ajax('/save', {
  83. 'data': {
  84. 'data': JSON.stringify(data),
  85. 'identifier': identifier,
  86. 'userid': userid
  87. },
  88. 'method': 'POST'
  89. });
  90. }
  91. var get_todo_item = function() {
  92. var result = {};
  93. for(var index = 0; index < todo_item_names.length; index += 1) {
  94. result[todo_item_names[index]] = false;
  95. }
  96. result.description = '';
  97. return result;
  98. }
  99. var Calendar = React.createClass({
  100. getInitialState: function() {
  101. default_value = {
  102. entries: [],
  103. entry_being_added: this.new_entry()
  104. };
  105. restore('Calendar', default_value,
  106. default_value, function() {
  107. jQuery('#submit-calendar').prop('disabled', false);
  108. });
  109. return default_value;
  110. },
  111. handle_submit: function(event)
  112. {
  113. event.preventDefault();
  114. (this.state.entry_being_added.month =
  115. parseInt(jQuery('#month').val()));
  116. (this.state.entry_being_added.date =
  117. parseInt(jQuery('#date').val()));
  118. (this.state.entry_being_added.year =
  119. parseInt(jQuery('#year').val()));
  120. if (jQuery('#all_day').checked) {
  121. this.state.entry_being_added.all_day = true;
  122. }
  123. (this.state.entry_being_added.description =
  124. jQuery('#description').val());
  125. if (this.state.entry_being_added.hasOwnProperty('repeats') &&
  126. this.state.entry_being_added.repeats) {
  127. if (jQuery('#yearly').is(':checked')) {
  128. this.state.entry_being_added.yearly = true;
  129. }
  130. (this.state.entry_being_added.start.time =
  131. this.state.entry_being_added.time);
  132. (this.state.entry_being_added.start.month =
  133. this.state.entry_being_added.month);
  134. (this.state.entry_being_added.start.date =
  135. this.state.entry_being_added.date);
  136. (this.state.entry_being_added.start.year =
  137. this.state.entry_being_added.year);
  138. (this.state.entry_being_added.frequency =
  139. jQuery('#month_based_frequency').val());
  140. if (jQuery('#sunday').is(':checked')) {
  141. this.state.entry_being_added.sunday = true;
  142. }
  143. if (jQuery('#monday').is(':checked')) {
  144. this.state.entry_being_added.monday = true;
  145. }
  146. if (jQuery('#tuesday').is(':checked')) {
  147. this.state.entry_being_added.tuesday = true;
  148. }
  149. if (jQuery('#wednesday').is('checked')) {
  150. this.state.entry_being_added.wednesday = true;
  151. }
  152. if (jQuery('#thursday').is('checked')) {
  153. this.state.entry_being_added.thursday = true;
  154. }
  155. if (jQuery('#friday').is('checked')) {
  156. this.state.entry_being_added.friday = true;
  157. }
  158. if (jQuery('#saturday').is('checked')) {
  159. this.state.entry_being_added.saturday = true;
  160. }
  161. (this.state.entry_being_added.month_occurrence =
  162. parseInt(jQuery('#month_occurrence').val()));
  163. var days;
  164. (days =
  165. parseInt(jQuery('#days_frequency').val()));
  166. if (isNaN(days)) {
  167. this.state.entry_being_added.days_frequency = null;
  168. } else {
  169. this.state.entry_being_added.days_frequency = days;
  170. }
  171. if (jQuery('#series_ends').is(':checked')) {
  172. (this.state.entry_being_added.end.month =
  173. parseInt(jQuery('#end_month').val()));
  174. (this.state.entry_being_added.end.date =
  175. parseInt(jQuery('#end_date').val()));
  176. (this.state.entry_being_added.end.year =
  177. parseInt(jQuery('#end_year').val()));
  178. }
  179. }
  180. var old_entry = this.state.entry_being_added;
  181. this.state.entries.push(this.state.entry_being_added);
  182. this.state.entry_being_added = this.new_entry();
  183. save('Calendar', this.state);
  184. var entry = this.new_entry();
  185. jQuery('#month').val(entry.month.toString());
  186. jQuery('#date').val(entry['date'].toString());
  187. jQuery('#year').val(entry.year.toString());
  188. jQuery('#all_day').prop('checked', false);
  189. jQuery('#description').val('');
  190. jQuery('#advanced').prop('checked', false);
  191. if (old_entry.hasOwnProperty('repeats') && old_entry.repeats) {
  192. jQuery('#month_based_frequency').val('Every');
  193. jQuery('#month_occurrence').val('-1');
  194. jQuery('#series_ends').prop('checked', false);
  195. jQuery('#end_month').val('' + new Date().getMonth());
  196. jQuery('#end_date').val('' + new Date().getDate());
  197. jQuery('#end_year').val('' + new Date().getFullYear() + 1);
  198. }
  199. },
  200. new_entry: function() {
  201. var result = {};
  202. result.hours = 12;
  203. result.minutes = 0;
  204. result.month = new Date().getMonth();
  205. result.date = new Date().getDate();
  206. result.year = new Date().getFullYear();
  207. result.weekday = new Date().getDay();
  208. result.description = '';
  209. return result;
  210. },
  211. new_series_entry: function() {
  212. var result = this.new_entry();
  213. result.repeats = true;
  214. result.start = {};
  215. result.yearly = false;
  216. result.start.hours = null;
  217. result.start.minutes = null;
  218. result.start.month = new Date().getMonth();
  219. result.start.date = new Date().getDate();
  220. result.start.year = new Date().getFullYear();
  221. result.frequency = null;
  222. result.sunday = false;
  223. result.monday = false;
  224. result.tuesday = false;
  225. result.wednesday = false;
  226. result.thursday = false;
  227. result.friday = false;
  228. result.saturday = false;
  229. result.month_occurrence = -1;
  230. result.end = {};
  231. result.end.time = null;
  232. result.end.month = null;
  233. result.end.date = null;
  234. result.end.year = null;
  235. return result;
  236. },
  237. on_change: function() {
  238. if (this.state.entry_being_added.hasOwnProperty('repeats')) {
  239. (this.state.entry_being_added.repeats =
  240. !this.state.entry_being_added.repeats);
  241. } else {
  242. var new_entry = this.new_series_entry();
  243. new_entry.time = this.state.entry_being_added.time;
  244. new_entry.month = this.state.entry_being_added.month;
  245. new_entry.date = this.state.entry_being_added.date;
  246. new_entry.year = this.state.entry_being_added.year;
  247. this.state.entry_being_added = new_entry;
  248. }
  249. },
  250. render: function() {
  251. var result = [this.render_basic_entry(
  252. this.state.entry_being_added)];
  253. if (this.state.entry_being_added &&
  254. this.state.entry_being_added.hasOwnProperty('repeats') &&
  255. this.state.entry_being_added.repeats) {
  256. result.push(this.render_entry_additionals(
  257. this.state.entry_being_added));
  258. }
  259. return (<div id="Calendar">
  260. <h1>Calendar</h1>
  261. {this.render_upcoming()}<form onSubmit={
  262. this.handle_submit}>{result}
  263. <input type="submit" value="Save" id="submit-calendar"
  264. disabled="disabled" /></form></div>);
  265. },
  266. render_basic_entry: function(entry) {
  267. var result = [];
  268. var all_day = false;
  269. var hour_options = [[0, '12AM'],
  270. [1, '1AM'],
  271. [2, '2AM'],
  272. [3, '3AM'],
  273. [4, '4AM'],
  274. [5, '5AM'],
  275. [6, '6AM'],
  276. [7, '7AM'],
  277. [8, '8AM'],
  278. [9, '9AM'],
  279. [10, '10AM'],
  280. [11, '11AM'],
  281. [12, '12PM'],
  282. [13, '1PM'],
  283. [14, '2PM'],
  284. [15, '3PM'],
  285. [16, '4PM'],
  286. [17, '5PM'],
  287. [18, '6PM'],
  288. [19, '7PM'],
  289. [20, '8PM'],
  290. [21, '9PM'],
  291. [22, '10PM'],
  292. [23, '11PM']];
  293. var hours = [];
  294. for(var index = 0; index < hour_options.length; index += 1) {
  295. hours.push(<option
  296. value={hour_options[index][0]}
  297. >{hour_options[index][1]}</option>);
  298. }
  299. var minute_options = [[0, '00'],
  300. [1, '01'],
  301. [2, '02'],
  302. [3, '03'],
  303. [4, '04'],
  304. [5, '05'],
  305. [6, '06'],
  306. [7, '07'],
  307. [8, '08'],
  308. [9, '09'],
  309. [10, '10'],
  310. [11, '11'],
  311. [12, '12'],
  312. [13, '13'],
  313. [14, '14'],
  314. [15, '15'],
  315. [16, '16'],
  316. [17, '17'],
  317. [18, '18'],
  318. [19, '19'],
  319. [20, '20'],
  320. [21, '21'],
  321. [22, '22'],
  322. [23, '23'],
  323. [24, '24'],
  324. [25, '25'],
  325. [26, '26'],
  326. [27, '27'],
  327. [28, '28'],
  328. [29, '29'],
  329. [30, '30'],
  330. [31, '31'],
  331. [32, '32'],
  332. [33, '33'],
  333. [34, '34'],
  334. [35, '35'],
  335. [36, '36'],
  336. [37, '37'],
  337. [38, '38'],
  338. [39, '39'],
  339. [40, '40'],
  340. [41, '41'],
  341. [42, '42'],
  342. [43, '43'],
  343. [44, '44'],
  344. [45, '45'],
  345. [46, '46'],
  346. [47, '47'],
  347. [48, '48'],
  348. [49, '49'],
  349. [50, '50'],
  350. [51, '51'],
  351. [52, '52'],
  352. [53, '53'],
  353. [54, '54'],
  354. [55, '55'],
  355. [56, '56'],
  356. [57, '57'],
  357. [58, '58'],
  358. [59, '59']];
  359. var minutes = [];
  360. for(var index = 0; index < minute_options.length; index += 1) {
  361. minutes.push(<option value={minute_options[index][0]}
  362. >{minute_options[index][1]}</option>);
  363. }
  364. result.push(<li><input type="checkbox" name="all_day"
  365. id="all_day" />All day event.
  366. &nbsp;<strong>&mdash;or&mdash;</strong>&nbsp;
  367. <select id="hours" id="hours"
  368. defaultValue="12">{hours}</select>:
  369. <select id="minutes" id="minutes"
  370. defaultValue="0">{minutes}</select></li>);
  371. var month_options = [[0, 'January'],
  372. [1, 'February'],
  373. [2, 'March'],
  374. [3, 'April'],
  375. [4, 'May'],
  376. [5, 'June'],
  377. [6, 'July'],
  378. [7, 'August'],
  379. [8, 'September'],
  380. [9, 'October'],
  381. [10, 'November'],
  382. [11, 'December']];
  383. var months = [];
  384. for(var index = 0; index < month_options.length; index += 1) {
  385. months.push(<option value={month_options[index][0]}
  386. >{month_options[index][1]}</option>);
  387. }
  388. result.push(<li><select id="month"
  389. name="month" defaultValue={entry.month}
  390. >{months}</select></li>);
  391. var days_in_month = null;
  392. if (entry && entry.hasOwnProperty('month')) {
  393. var month = entry.month;
  394. if (jQuery('#month').val()) {
  395. month = parseInt(jQuery('#month').val());
  396. }
  397. if (month === 0 || month === 2 || month === 4 || month
  398. === 6 || month === 7 || month === 9 || month === 11) {
  399. days_in_month = 31;
  400. }
  401. else if (month === 1) {
  402. if (entry && entry.hasOwnProperty('year') && entry.year
  403. % 4 === 0) {
  404. days_in_month = 29;
  405. } else {
  406. days_in_month = 28;
  407. }
  408. } else {
  409. days_in_month = 30;
  410. }
  411. }
  412. var date_options = [];
  413. for(var index = 1; index <= days_in_month; index += 1) {
  414. date_options.push([index, index.toString()]);
  415. }
  416. var dates = [];
  417. for(var index = 0; index < date_options.length; index += 1) {
  418. dates.push(<option value={date_options[index][0]}
  419. >{date_options[index][1]}</option>);
  420. }
  421. result.push(<li>Date: <select id="date" name="date"
  422. defaultValue={entry.date}>{dates}</select></li>);
  423. var year_options = [];
  424. for(var index = new Date().getFullYear(); index < new
  425. Date().getFullYear() + 100; index += 1) {
  426. year_options.push([index, index.toString()]);
  427. }
  428. var years = [];
  429. for(var index = 0; index < year_options.length; index += 1) {
  430. years.push(<option value={year_options[index][0]}
  431. >{year_options[index][1]}</option>);
  432. }
  433. result.push(<li>Year: <select id="year" name="year"
  434. defaultValue={entry.years}>{years}</select></li>);
  435. result.push(<li>Description: <input type="text"
  436. name="description" id="description" /></li>);
  437. result.push(<li><input type="checkbox" name="advanced"
  438. id="advanced" onChange={this.on_change} />
  439. Recurring event</li>);
  440. return <ul>{result}</ul>;
  441. },
  442. render_entry_additionals: function(entry) {
  443. var result = [];
  444. result.push(<li><input type="checkbox"
  445. name="yearly" id="yearly" /> This date, every year.</li>);
  446. var frequency = [];
  447. var frequency_options = ['Every',
  448. 'Every First',
  449. 'Every Second',
  450. 'Every Third',
  451. 'Every Fourth',
  452. 'Every Last',
  453. 'Every First and Third',
  454. 'Every Second and Fourth'];
  455. for(var index = 0; index < frequency_options.length;
  456. index += 1) {
  457. frequency.push(<option>{frequency_options[index]}</option>);
  458. }
  459. result.push(<li><select name="month_based_frequency"
  460. id="month_based_frequency" defaultValue="0"
  461. >{frequency}</select></li>);
  462. var weekdays = [];
  463. var weekday_options = ['Sunday', 'Monday', 'Tuesday',
  464. 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  465. for(var index = 0; index < weekday_options.length; index) {
  466. if (entry && entry.hasOwnProperty(
  467. weekday_options[index].toLowerCase()) &&
  468. entry[weekday_options[index].toLowerCase()]) {
  469. weekdays.push(<span><input type="checkbox"
  470. name={weekday_options[index].toLowerCase()}
  471. id={weekday_options[index].toLowerCase()} />
  472. {weekday_options[index]}</span>);
  473. } else {
  474. weekdays.push(<span><input type="checkbox"
  475. name={weekday_options[index].toLowerCase()}
  476. id={weekday_options[index].toLowerCase()} />
  477. {weekday_options[index]}</span>);
  478. }
  479. }
  480. result.push(<li>{weekdays}</li>);
  481. var month_occurrences = [[0, 'January'],
  482. [1, 'February'],
  483. [2, 'March'],
  484. [3, 'April'],
  485. [4, 'May'],
  486. [5, 'June'],
  487. [6, 'July'],
  488. [7, 'August'],
  489. [8, 'September'],
  490. [9, 'October'],
  491. [10, 'November'],
  492. [11, 'December']];
  493. var month_occurrences_with_all = [[-1, 'Every Month'],
  494. [0, 'January'],
  495. [1, 'February'],
  496. [2, 'March'],
  497. [3, 'April'],
  498. [4, 'May'],
  499. [5, 'June'],
  500. [6, 'July'],
  501. [7, 'August'],
  502. [8, 'September'],
  503. [9, 'October'],
  504. [10, 'November'],
  505. [11, 'December']];
  506. var months = [];
  507. for(var index = 0; index < month_occurrences.length; index += 1) {
  508. months.push(<option
  509. name={month_occurrences[index][0]}
  510. id={month_occurrences[index][0]}
  511. >{month_occurrences[index][1]}</option>);
  512. }
  513. var months_with_all = [];
  514. for(var index = 0; index <
  515. month_occurrences_with_all.length; index += 1) {
  516. months_with_all.push(<option
  517. name={month_occurrences_with_all[index][0]}
  518. id={month_occurrences_with_all[index][0]}
  519. value={month_occurrences_with_all[index][0]}
  520. >{month_occurrences_with_all[index][1]}</option>);
  521. }
  522. result.push(<li><select id="month_occurrence"
  523. name="month_occurrence" defaultValue="-1"
  524. >{months_with_all}</select></li>);
  525. var month = entry.month;
  526. result.push(<li>Every <input type="text" size="2"
  527. name="days_frequency" id="days_frequency" /> days.</li>);
  528. if (jQuery('#end_month').val()) {
  529. month = parseInt(jQuery('#end_month').val());
  530. }
  531. if (month === 0 || month === 2 || month === 4 || month ===
  532. 6 || month === 7 || month === 9 || month === 11) {
  533. days_in_month = 31;
  534. } else if (month === 1) {
  535. if (entry && entry.hasOwnProperty('year') &&
  536. entry.year % 4 === 0) {
  537. days_in_month = 29;
  538. } else {
  539. days_in_month = 28;
  540. }
  541. } else {
  542. days_in_month = 30;
  543. }
  544. var date_options = [];
  545. for(var index = 1; index <= days_in_month; index += 1) {
  546. date_options.push([index, index.toString()]);
  547. }
  548. var dates = [];
  549. for(var index = 0; index < date_options.length; index += 1) {
  550. dates.push(<option value={date_options[index][0]}
  551. >{date_options[index][1]}</option>);
  552. }
  553. var year_options = [];
  554. for(var index = new Date().getFullYear(); index < new
  555. Date().getFullYear() + 100; index += 1) {
  556. year_options.push([index + 1, (index + 1).toString()]);
  557. }
  558. var years = [];
  559. for(var index = 0; index < year_options.length; index += 1) {
  560. years.push(<option value={year_options[index][0]}
  561. >{year_options[index][1]}</option>);
  562. }
  563. result.push(<li>Ends on (optional): <input type="checkbox"
  564. name="series_ends" id="series_ends" /><ul><li>Month:
  565. <select id="end_month" name="end_month"
  566. defaultValue={month}>{months}</select></li>
  567. <li>End date:<select id="end_date"
  568. name="end_date" defaultValue={entry.date}
  569. >{dates}</select></li>
  570. <li>End year:<select id="end_year"
  571. name="end_year" defaultValue={entry.end_year + 1}
  572. >{years}</select></li></ul></li>);
  573. return <ul>{result}</ul>;
  574. },
  575. render_upcoming: function() {
  576. var that = this;
  577. var result = [];
  578. var entry_displayed = false;
  579. var compare = function(first, second) {
  580. if (first.year > second.year) {
  581. return 1;
  582. } else if (first.year === second.year && first.month >
  583. second.month) {
  584. return 1;
  585. } else if (first.year === second.year && first.month ===
  586. second.month && first.date > second.date) {
  587. return 1;
  588. } else if (first.year === second.year && first.month ===
  589. second.month && first.date === second.date) {
  590. return 0;
  591. } else {
  592. return -1;
  593. }
  594. }
  595. var successor = function(entry) {
  596. var result = that.new_entry();
  597. var days_in_month = null;
  598. if (entry.month === 0 || entry.month === 2 ||
  599. entry.month === 4 || entry.month === 6 ||
  600. entry.month === 7 || entry.month === 9 ||
  601. entry.month === 11) {
  602. days_in_month = 31;
  603. } else if (entry.month === 1) {
  604. if (entry && entry.hasOwnProperty('year') &&
  605. entry.year % 4 === 0) {
  606. days_in_month = 29;
  607. } else {
  608. days_in_month = 28;
  609. }
  610. } else {
  611. days_in_month = 30;
  612. }
  613. if (entry.date === days_in_month) {
  614. if (entry.month === 11) {
  615. result.year = entry.year + 1;
  616. result.month = 0;
  617. } else {
  618. result.year = entry.year;
  619. result.month = entry.month + 1;
  620. }
  621. result.date = 1;
  622. } else {
  623. result.year = entry.year;
  624. result.month = entry.month;
  625. result.date = entry.date + 1;
  626. }
  627. result.days_ahead = entry.days_ahead + 1;
  628. result.weekday = (entry.weekday + 1) % 7;
  629. return result;
  630. }
  631. var greatest = this.new_entry();
  632. for(var index = 0; index < this.state.entries.length;
  633. index += 1) {
  634. var entry = this.state.entries[index];
  635. if (!entry.hasOwnProperty('repeats') &&
  636. entry.repeats) {
  637. if (compare(entry, greatest) === 1) {
  638. greatest = this.new_entry();
  639. greatest.year = entry.year;
  640. greatest.month = entry.month;
  641. greatest.date = entry.date;
  642. }
  643. }
  644. }
  645. var once = {};
  646. var repeating = [];
  647. for(var index = 0; index < this.state.entries.length;
  648. index += 1) {
  649. var entry = this.state.entries[index];
  650. if (entry.hasOwnProperty('repeats') && entry.repeats) {
  651. repeating.push(entry);
  652. } else {
  653. var identifier = (entry.date + '/' + entry.month + '/' +
  654. entry.year);
  655. if (once.hasOwnProperty(identifier)) {
  656. once[identifier].push(entry);
  657. } else {
  658. once[identifier] = [entry];
  659. }
  660. }
  661. }
  662. greatest.year += 1;
  663. var first_day = this.new_entry();
  664. first_day.days_ahead = 0;
  665. first_day.weekday = new Date().getDay();
  666. for(var day = first_day; compare(day, greatest)
  667. === -1; day = successor(day)) {
  668. var activities_today = [];
  669. if (once.hasOwnProperty(day.date + '/' + day.month + '/' +
  670. day.year)) {
  671. activities_today = activities_today.concat(
  672. once[day.date + '/' + day.month + '/' + day.year]);
  673. }
  674. for(var index = 0; index < repeating.length;
  675. index += 1) {
  676. var entry = repeating[index];
  677. var accepts_this_date = true;
  678. if (entry.yearly) {
  679. if (!(day.date === entry.start.date &&
  680. day.month === entry.start.month)) {
  681. accepts_this_date = false;
  682. }
  683. }
  684. if (entry.date === day.date && entry.month ===
  685. day.month && entry.year === day.year) {
  686. entry.days_ahead = day.days_ahead;
  687. }
  688. if (entry.frequency === 'Every First') {
  689. if (!day.date < 8) {
  690. accepts_this_date = false;
  691. }
  692. } else if (entry.frequency === 'Every Second') {
  693. if (!(day.date > 7 && day.date < 15)) {
  694. accepts_this_date = false;
  695. }
  696. } else if (entry.frequency === 'Every Third') {
  697. if (!(day.date > 14 && day.date < 22)) {
  698. accepts_this_date = false;
  699. }
  700. } else if (entry.frequency === 'Every Fourth') {
  701. if (!(day.date > 21 && day.date < 29)) {
  702. accepts_this_date = false;
  703. }
  704. } else if (entry.frequency === 'Every Last') {
  705. var last = null;
  706. if (day.month === 0 || day.month === 2 ||
  707. day.month === 4 || day.month === 6 ||
  708. day.month === 7 || day.month === 9 ||
  709. day.month === 11) {
  710. last = 31;
  711. } else if (day.month === 1) {
  712. if (day.year % 4 === 0) {
  713. last = 29;
  714. } else {
  715. last = 28;
  716. }
  717. } else {
  718. last = 30;
  719. }
  720. if (day.date <= last - 7) {
  721. accepts_this_date = false;
  722. }
  723. } else if (entry.frequency === 'Every First and Third') {
  724. if (!(day.date < 8 || day.date > 14 && day.date <
  725. 22)) {
  726. accepts_this_date = false;
  727. }
  728. } else if (entry.frequency === 'Every Second and Fourth') {
  729. if (!(day.date > 7 && day.date < 15 ||
  730. day.date > 21 && day.date < 29)) {
  731. accepts_this_date = false;
  732. }
  733. }
  734. if (entry.sunday || entry.monday || entry.tuesday ||
  735. entry.wednesday || entry.thursday || entry.friday
  736. || entry.saturday) {
  737. if (day.weekday === 0 && !entry.sunday) {
  738. accepts_this_date = false;
  739. }
  740. if (day.weekday === 1 && !entry.monday) {
  741. accepts_this_date = false;
  742. }
  743. if (day.weekday === 2 && !entry.tuesday) {
  744. accepts_this_date = false;
  745. }
  746. if (day.weekday === 3 && !entry.wednesday) {
  747. accepts_this_date = false;
  748. }
  749. if (day.weekday === 4 && !entry.thursday) {
  750. accepts_this_date = false;
  751. }
  752. if (day.weekday === 5 && !entry.friday) {
  753. accepts_this_date = false;
  754. }
  755. if (day.weekday === 6 && !entry.saturday) {
  756. accepts_this_date = false;
  757. }
  758. }
  759. if (entry.month_occurrence !== -1) {
  760. if (day.month !== entry.month_occurrence) {
  761. accepts_this_date = false;
  762. }
  763. }
  764. if (entry.days_frequency) {
  765. if (entry.hasOwnProperty('days_ahead')) {
  766. if ((day.days_ahead - entry.days_ahead)
  767. % entry.days_frequency !== 0) {
  768. accepts_this_date = false;
  769. }
  770. }
  771. }
  772. if (entry.ends_on) {
  773. if (entry.end.year < day.year) {
  774. accepts_this_date = false;
  775. } else if (entry.end.year === day.year &&
  776. entry.end.month < day.month) {
  777. accepts_this_date = false;
  778. } else if (entry.end.year === day.year &&
  779. entry.end.month === day.month &&
  780. entry.end.date < day.date) {
  781. accepts_this_date = false;
  782. }
  783. }
  784. if (entry.hasOwnProperty('start')) {
  785. if (entry.start.year > day.year) {
  786. accepts_this_date = false;
  787. } else if (entry.start.year === day.year &&
  788. entry.start.month > day.month) {
  789. accepts_this_date = false;
  790. } else if (entry.start.year === day.year &&
  791. entry.end.month === day.month &&
  792. entry.end_date >= day.date) {
  793. accepts_this_date = false;
  794. }
  795. }
  796. if (accepts_this_date) {
  797. activities_today.push(entry)
  798. }
  799. }
  800. if (activities_today.length && !activities_logged_today) {
  801. activities_today_global = activities_today;
  802. activities_logged_today = true;
  803. }
  804. if (activities_today.length) {
  805. var comparator = function(first, second) {
  806. if (first.all_day && second.all_day) {
  807. if (first.description < second.description) {
  808. return -1;
  809. } else if (first.description === second.description) {
  810. return 0;
  811. } else {
  812. return 1;
  813. }
  814. } else if (first.all_day && !second.all_day) {
  815. return -1;
  816. } else if (!first.all_day && second.all_day) {
  817. return 1;
  818. } else {
  819. if (first.hour < second.hour) {
  820. return -1;
  821. } else if (first.hour > second.hour) {
  822. return 1;
  823. } else if (first.hour === second.hour) {
  824. if (first.minute < second.minute) {
  825. return -1;
  826. } else if (first.minute > second.minute) {
  827. return -1;
  828. } else {
  829. if (first.description < second.description) {
  830. return -1;
  831. } else if (first.description === second.description) {
  832. return 0;
  833. } else {
  834. return 1;
  835. }
  836. }
  837. }
  838. }
  839. }
  840. activities_today.sort(comparator);
  841. if (activities_today.length) {
  842. entry_displayed = true;
  843. var weekday = null;
  844. if (day.weekday === 0) {
  845. weekday = 'Sunday';
  846. }
  847. if (day.weekday === 1) {
  848. weekday = 'Monday';
  849. }
  850. if (day.weekday === 2) {
  851. weekday = 'Tuesday';
  852. }
  853. if (day.weekday === 3) {
  854. weekday = 'Wednesday';
  855. }
  856. if (day.weekday === 4) {
  857. weekday = 'Thursday';
  858. }
  859. if (day.weekday === 5) {
  860. weekday = 'Friday';
  861. }
  862. if (day.weekday === 6) {
  863. weekday = 'Saturday';
  864. }
  865. var month = null;
  866. if (day.month === 0) {
  867. month = 'January';
  868. }
  869. if (day.month === 1) {
  870. month = 'February';
  871. }
  872. if (day.month === 2) {
  873. month = 'March';
  874. }
  875. if (day.month === 3) {
  876. month = 'April';
  877. }
  878. if (day.month === 4) {
  879. month = 'May';
  880. }
  881. if (day.month === 5) {
  882. month = 'June';
  883. }
  884. if (day.month === 6) {
  885. month = 'July';
  886. }
  887. if (day.month === 7) {
  888. month = 'August';
  889. }
  890. if (day.month === 8) {
  891. month = 'September';
  892. }
  893. if (day.month === 9) {
  894. month = 'October';
  895. }
  896. if (day.month === 10) {
  897. month = 'November';
  898. }
  899. if (day.month === 11) {
  900. month = 'December';
  901. }
  902. result.push(<h2 className="day">{weekday},&nbsp;
  903. {day.date} {month} {day.year}</h2>);
  904. var rendered_activities = [];
  905. for(var index = 0; index < activities_today.length; index += 1) {
  906. var activity = activities_today[index];
  907. var hour_options = [[0, '12AM'],
  908. [1, '1AM'],
  909. [2, '2AM'],
  910. [3, '3AM'],
  911. [4, '4AM'],
  912. [5, '5AM'],
  913. [6, '6AM'],
  914. [7, '7AM'],
  915. [8, '8AM'],
  916. [9, '9AM'],
  917. [10, '10AM'],
  918. [11, '11AM'],
  919. [12, '12PM'],
  920. [13, '1PM'],
  921. [14, '2PM'],
  922. [15, '3PM'],
  923. [16, '4PM'],
  924. [17, '5PM'],
  925. [18, '6PM'],
  926. [19, '7PM'],
  927. [20, '8PM'],
  928. [21, '9PM'],
  929. [22, '10PM'],
  930. [23, '11PM']];
  931. var minute_options = [[0, '00'],
  932. [1, '01'],
  933. [2, '02'],
  934. [3, '03'],
  935. [4, '04'],
  936. [5, '05'],
  937. [6, '06'],
  938. [7, '07'],
  939. [8, '08'],
  940. [9, '09'],
  941. [10, '10'],
  942. [11, '11'],
  943. [12, '12'],
  944. [13, '13'],
  945. [14, '14'],
  946. [15, '15'],
  947. [16, '16'],
  948. [17, '17'],
  949. [18, '18'],
  950. [19, '19'],
  951. [20, '20'],
  952. [21, '21'],
  953. [22, '22'],
  954. [23, '23'],
  955. [24, '24'],
  956. [25, '25'],
  957. [26, '26'],
  958. [27, '27'],
  959. [28, '28'],
  960. [29, '29'],
  961. [30, '30'],
  962. [31, '31'],
  963. [32, '32'],
  964. [33, '33'],
  965. [34, '34'],
  966. [35, '35'],
  967. [36, '36'],
  968. [37, '37'],
  969. [38, '38'],
  970. [39, '39'],
  971. [40, '40'],
  972. [41, '41'],
  973. [42, '42'],
  974. [43, '43'],
  975. [44, '44'],
  976. [45, '45'],
  977. [46, '46'],
  978. [47, '47'],
  979. [48, '48'],
  980. [49, '49'],
  981. [50, '50'],
  982. [51, '51'],
  983. [52, '52'],
  984. [53, '53'],
  985. [54, '54'],
  986. [55, '55'],
  987. [56, '56'],
  988. [57, '57'],
  989. [58, '58'],
  990. [59, '59']];
  991. if (activity.all_day) {
  992. rendered_activities.push(<li
  993. dangerouslySetInnerHTML={{__html:
  994. converter.makeHtml(activity.description)
  995. .replace('<p>', '').replace('</p>', '')}}
  996. />);
  997. } else if (activity.minutes) {
  998. rendered_activities.push(<li
  999. dangerouslySetInnerHTML={{__html:
  1000. hour_options[activity.hours][1] + ':' +
  1001. minute_options[activity.minutes][1] + ' ' +
  1002. converter.makeHtml(activity.description)
  1003. .replace('<p>', '').replace('</p>', '')}}
  1004. />);
  1005. } else {
  1006. if (activity.description) {
  1007. var description = converter.makeHtml(activity.description
  1008. ).replace('<p>', '').replace('</p>', '');
  1009. } else {
  1010. var description = '';
  1011. }
  1012. rendered_activities.push(<li
  1013. dangerouslySetInnerHTML={{__html:
  1014. hour_options[activity.hours][1] + ' ' +
  1015. description}} />);
  1016. }
  1017. }
  1018. result.push(<ul className="activities">
  1019. {rendered_activities}</ul>);
  1020. }
  1021. }
  1022. }
  1023. if (entry_displayed) {
  1024. result.push(<hr />);
  1025. }
  1026. return result;
  1027. }
  1028. });
  1029. var Pragmatometer = React.createClass({
  1030. render: function() {
  1031. return (
  1032. <div className="Pragmatometer">
  1033. <Calendar />
  1034. <Todo />
  1035. <Scratchpad />
  1036. <Talk />
  1037. </div>
  1038. );
  1039. }
  1040. });
  1041. var Scratchpad = React.createClass({
  1042. getInitialState: function() {
  1043. var that = this;
  1044. result = {'text': ''};
  1045. setTimeout(function() {
  1046. restore('Scratchpad', '', result, function() {
  1047. scratchpad_is_initialized = true;
  1048. var text = '';
  1049. for(var index = 0; that.state.hasOwnProperty(index); index += 1) {
  1050. text += that.state[index];
  1051. }
  1052. jQuery('#scratchpad').val(text);
  1053. if (typeof CKEDITOR.instances['scratchpad'] === 'undefined') {
  1054. CKEDITOR.replace('scratchpad');
  1055. }
  1056. });
  1057. }, 1000);
  1058. return result;
  1059. },
  1060. render: function() {
  1061. return (
  1062. <div id="Scratchpad">
  1063. <h1>Scratchpad</h1>
  1064. <textarea name="scratchpad" id="scratchpad"
  1065. value={this.state.text}></textarea>
  1066. </div>
  1067. );
  1068. },
  1069. shouldComponentUpdate: function() {
  1070. return false;
  1071. }
  1072. });
  1073. var Todo = React.createClass({
  1074. mixins: [React.addons.LinkedStateMixin],
  1075. getInitialState: function() {
  1076. return restore('Todo', {
  1077. 'items': [],
  1078. 'text': ''
  1079. }, {
  1080. 'items': [],
  1081. 'text': ''
  1082. },
  1083. function() {
  1084. jQuery('#add-activity-button').prop('disabled', false);
  1085. });
  1086. },
  1087. handle_checkbox_change: function(event) {
  1088. var address = event.target.id.split('.', 2);
  1089. var index = this.state.items.length - 1 - parseInt(address[0]);
  1090. (this.state.items[parseInt(index)][address[1]] =
  1091. !this.state.items[parseInt(index)][address[1]]);
  1092. save('Todo', this.state);
  1093. },
  1094. handleSubmit: function(event) {
  1095. event.preventDefault();
  1096. var new_item = get_todo_item();
  1097. new_item.description = this.state.text;
  1098. new_item.id = this.state.items.length;
  1099. this.state.items.unshift(new_item);
  1100. var next_text = '';
  1101. this.setState({text: next_text});
  1102. save('Todo', this.state);
  1103. },
  1104. onChange: function(event) {
  1105. this.setState({text: event.target.value});
  1106. },
  1107. render: function() {
  1108. var that = this;
  1109. var table_rows = [];
  1110. var display_todo_item_checkbox = function(label, item) {
  1111. var html_id = item.id + '.' + label;
  1112. return (
  1113. <td className={label} title={label}>
  1114. <input onChange={that.handle_checkbox_change} id={html_id}
  1115. className={label} type="checkbox"
  1116. defaultChecked={item[label]} />
  1117. </td>
  1118. );
  1119. };
  1120. var display_todo_item = function(item) {
  1121. var rendered_nodes = [];
  1122. if (!item.Completed && !item.Delete && !item.Invisible) {
  1123. for(var index = 0; index < todo_item_names.length;
  1124. index += 1) {
  1125. rendered_nodes.push(
  1126. display_todo_item_checkbox(todo_item_names[index], item)
  1127. );
  1128. }
  1129. rendered_nodes.push(<td dangerouslySetInnerHTML={{__html:
  1130. converter.makeHtml(item.description)}} />);
  1131. }
  1132. if (rendered_nodes) {
  1133. return (<tr>{rendered_nodes}</tr>);
  1134. } else {
  1135. return null;
  1136. }
  1137. };
  1138. table_rows.push(this.state.items.map(display_todo_item));
  1139. return (
  1140. <div id="Todo">
  1141. <h1>To do</h1>
  1142. <form onSubmit={this.handleSubmit}>
  1143. <table>
  1144. {table_rows}
  1145. <tfoot>
  1146. <textarea onChange={this.onChange} id="todo-new-entries"
  1147. value={this.state.text}></textarea>
  1148. <br />
  1149. <button id="add-activity-button"
  1150. disabled="disabled">{'Add activity'}</button>
  1151. </tfoot>
  1152. </table>
  1153. </form>
  1154. </div>
  1155. );
  1156. }
  1157. });
  1158. var Talk = React.createClass( {
  1159. listen: function() {
  1160. var that = this;
  1161. jQuery.ajax('/listen', {
  1162. 'complete': function(jqxhr) {
  1163. if (jqxhr.responseText && jqxhr.responseText[0] != '[')
  1164. {
  1165. // We ignore non-JSON a responses
  1166. } else {
  1167. that.state.hearing = JSON.parse(jqxhr.responseText);
  1168. }
  1169. },
  1170. 'data': {
  1171. 'identifier': jQuery('#room').val()
  1172. },
  1173. 'method': 'POST'
  1174. });
  1175. },
  1176. componentDidMount: function() {
  1177. document.body.addEventListener('keypress', this.onKeyPress);
  1178. document.body.addEventListener('keydown', this.onKeyDown);
  1179. listen = this.listen;
  1180. },
  1181. getInitialState: function() {
  1182. return {
  1183. hearing: [],
  1184. room_identifier: get_words(3)
  1185. }
  1186. },
  1187. onKeyDown: function(eventObject) {
  1188. this.onKeyPress(eventObject);
  1189. },
  1190. onKeyPress: function(eventObject) {
  1191. var payload = {};
  1192. payload.statement = jQuery('#say').val();
  1193. payload.finished = false;
  1194. if (eventObject.which === 13 ||
  1195. eventObject.which === 10) {
  1196. payload.finished = true;
  1197. }
  1198. setTimeout(function() {
  1199. jQuery.ajax('/say', {
  1200. 'data': {
  1201. 'data': JSON.stringify(payload),
  1202. 'identifier': jQuery('#room').val(),
  1203. 'statement': jQuery('#say').val()
  1204. },
  1205. 'method': 'POST'
  1206. });
  1207. if (eventObject.which === 13 ||
  1208. eventObject.which === 10) {
  1209. jQuery('#say').val('');
  1210. }
  1211. }, 1000);
  1212. },
  1213. render: function() {
  1214. var that = this;
  1215. var workbench = [];
  1216. that.state.hearing.sort(function (first, second) {
  1217. if (first.startstamp > second.startstamp) {
  1218. return 1;
  1219. } else if (first.startstamp < second.startstamp) {
  1220. return -1;
  1221. } else {
  1222. return 0;
  1223. }
  1224. });
  1225. var display_conversations = function() {
  1226. var result = [];
  1227. for(var index = 0; index < that.state.hearing.length; index += 1) {
  1228. if (that.state.hearing[index].statement) {
  1229. console.log(that.state.hearing[index].endstamp);
  1230. if (that.state.hearing[index].endstamp) {
  1231. result.push(<p><strong>{that.state.hearing[index].name}:
  1232. </strong> {that.state.hearing[index].statement}
  1233. <span className="endstamp"> {new
  1234. Date(that.state.hearing[index].endstamp).toLocaleString()}</span></p>);
  1235. } else {
  1236. result.push(<p><strong>{that.state.hearing[index].name}:
  1237. </strong> {that.state.hearing[index].statement}</p>);
  1238. }
  1239. }
  1240. }
  1241. return result;
  1242. }
  1243. return (
  1244. <div id="Talk">
  1245. <h1>Instant Chat</h1>
  1246. <input type="text" id="room"
  1247. defaultValue={this.state.room_identifier} />
  1248. <div id="conversations">{display_conversations()}</div>
  1249. <input type="text" id="say" />
  1250. </div>
  1251. );
  1252. }
  1253. });
  1254. var pragmatometer = React.createElement(<Pragmatometer />, null);
  1255. React.render(pragmatometer, jQuery('#main')[0]);
  1256. var update_on_event = function() {
  1257. React.render(pragmatometer, jQuery('#main')[0]);
  1258. };
  1259. jQuery(document).keydown(update_on_event);
  1260. jQuery(document).keypress(update_on_event);
  1261. jQuery(document).keyup(update_on_event);
  1262. jQuery(document).click(update_on_event);
  1263. jQuery(document).dblclick(update_on_event);
  1264. jQuery(document).focus(update_on_event);
  1265. var update = function() {
  1266. React.render(pragmatometer, jQuery('#main')[0]);
  1267. if (scratchpad_is_initialized) {
  1268. for(var instance in CKEDITOR.instances) {
  1269. CKEDITOR.instances[instance].updateElement();
  1270. }
  1271. if (scratchpad_value === null ||
  1272. jQuery('#scratchpad').val() !== scratchpad_value) {
  1273. scratchpad_value = jQuery('#scratchpad').val();
  1274. save('Scratchpad', jQuery('#scratchpad').val());
  1275. }
  1276. listen();
  1277. jQuery("#conversation").scrollTop(jQuery("#conversation")[0].scrollHeight);
  1278. }
  1279. };
  1280. setInterval(update, 1000);
  1281. update();
  1282. ////////////////////////////////////////////////////////////////////////////
  1283. // //
  1284. // jQuery, combination, or general JavaScript code here. //
  1285. // //
  1286. ////////////////////////////////////////////////////////////////////////////
  1287. var set_element_sizes = function() {
  1288. var header_element = jQuery('h1#header')[0];
  1289. var header_height = jQuery(header_element).height();
  1290. header_height += parseInt(jQuery(header_element).css('margin-top'
  1291. ).replace('px', ''));
  1292. header_height += parseInt(jQuery(header_element).css('margin-bottom'
  1293. ).replace('px', ''));
  1294. var window_height = jQuery(window).height() - header_height;
  1295. var window_width = jQuery(window).width();
  1296. if (window_width >= 513) {
  1297. jQuery('#Calendar').css('height', window_height * .46);
  1298. jQuery('#Calendar').css('left', window_width * .02);
  1299. jQuery('#Calendar').css('top', header_height + window_height * .02);
  1300. jQuery('#Calendar').css('width', window_width * .46);
  1301. jQuery('#Scratchpad').css('height', window_height * .46);
  1302. jQuery('#Scratchpad').css('left', window_width * .02);
  1303. jQuery('#Scratchpad').css('top', header_height + window_height * .46);
  1304. jQuery('#Scratchpad').css('width', window_width * .46);
  1305. jQuery('#Todo').css('height', window_height * .46);
  1306. jQuery('#Todo').css('left', window_width * .52);
  1307. jQuery('#Todo').css('top', header_height + window_height * .02);
  1308. jQuery('#Todo').css('width', window_width * .46);
  1309. jQuery('#Talk').css('height', window_height * .46);
  1310. jQuery('#Talk').css('top', header_height + window_height * .46);
  1311. jQuery('#Talk').css('left', window_width * .52);
  1312. jQuery('#Talk').css('width', window_width * .46);
  1313. jQuery('#conversations').css('width', window_width * .46);
  1314. jQuery('#conversations').css('top', header_height + window_height * .46 +
  1315. jQuery('#room').height());
  1316. jQuery('#conversations').css('left', window_width * .52);
  1317. jQuery('#conversations').css('height', window_height * .46 -
  1318. jQuery('#room').height() - jQuery('#say').height() -
  1319. jQuery('#Talk h1').height() -
  1320. parseInt(jQuery(jQuery('#Talk h1')[0]).css('margin-top').replace(
  1321. 'px', '')) -
  1322. parseInt(jQuery(jQuery('#Talk h1')[0]).css('margin-bottom').replace(
  1323. 'px', '')) - 40);
  1324. }
  1325. };
  1326. set_element_sizes();
  1327. jQuery(window).resize(set_element_sizes);
  1328. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement