canvas.js 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /* Contains all the Javascript logic of the canvas and its main features: save, export and share */
  2. $(document).ready(function() {
  3. /* ================================================
  4. Miscellaneous
  5. ================================================= */
  6. // Declare the currently loaded tags belonging to the user
  7. var tags;
  8. // This piece of code is for making the column-count and column-gap CSS to work in Firefox
  9. /*
  10. document.getElementById("7-5-col-layout").style.MozColumnCount = "7";
  11. document.getElementById("2-col-layout").style.MozColumnCount = "2";
  12. */
  13. // Prevent pressing ENTER on Project Title from submitting the form
  14. $('.proj_title').keydown(function(event){
  15. if(event.keyCode == 13) {
  16. event.preventDefault();
  17. return false;
  18. }
  19. });
  20. /* ================================================
  21. Rearrange fields numerically if 1 column is displayed
  22. ================================================= */
  23. // Declarations
  24. var groupOneLayout = $("#7-5-col-layout");
  25. var groupTwoLayout = $("#4-col-layout");
  26. var field01 = $("#panel_01");
  27. var field03 = $("#panel_03");
  28. var field04 = $("#panel_04");
  29. var field09 = $("#panel_09");
  30. var field05 = $("#panel_05");
  31. var field06 = $("#panel_06");
  32. var field02 = $("#panel_02");
  33. var field07 = $("#panel_07");
  34. var field08 = $("#panel_08");
  35. var isRearranged = false;
  36. // Rearrange the fields to suit mobile
  37. function rearrangeFields() {
  38. field01.detach();
  39. field03.detach();
  40. field04.detach();
  41. field09.detach();
  42. field05.detach();
  43. field06.detach();
  44. field02.detach();
  45. field07.detach();
  46. field08.detach();
  47. groupOneLayout.append(field01);
  48. groupOneLayout.append(field02);
  49. groupOneLayout.append(field03);
  50. groupOneLayout.append(field04);
  51. groupOneLayout.append(field05);
  52. groupOneLayout.append(field06);
  53. groupOneLayout.append(field07);
  54. groupOneLayout.append(field08);
  55. groupOneLayout.append(field09);
  56. }
  57. // Rearrange the fields according to their original order
  58. function rearrangeFieldsOriginal() {
  59. field01.detach();
  60. field02.detach();
  61. field03.detach();
  62. field04.detach();
  63. field05.detach();
  64. field06.detach();
  65. field07.detach();
  66. field08.detach();
  67. field09.detach();
  68. groupOneLayout.append(field01);
  69. groupOneLayout.append(field03);
  70. groupOneLayout.append(field04);
  71. groupOneLayout.append(field09);
  72. groupOneLayout.append(field05);
  73. groupOneLayout.append(field06);
  74. groupOneLayout.append(field02);
  75. groupTwoLayout.append(field07);
  76. groupTwoLayout.append(field08);
  77. }
  78. // If the web page is opened on a mobile
  79. if ($(window).width() <= 499) {
  80. rearrangeFields();
  81. }
  82. // When resizing the window
  83. $(window).on("resize", function() {
  84. if (isRearranged === false && $(window).width() <= 499) {
  85. rearrangeFields();
  86. isRearranged = true;
  87. }
  88. else if(isRearranged === true && $(window).width() >= 500) {
  89. rearrangeFieldsOriginal();
  90. isRearranged = false;
  91. }
  92. });
  93. /* ================================================
  94. "Jump to" functions
  95. ================================================= */
  96. var hasScrolledDown = false;
  97. function showFixedJumpedTo() {
  98. // Add classes
  99. $("div.jump-to-click-area").addClass("jump-to-click-area-toggle");
  100. $("div.jump-to").addClass("jump-to-toggle");
  101. $("div.jump-to-list").addClass("jump-to-list-toggle");
  102. $("img.jump-to-img").addClass("jump-to-img-toggle");
  103. // If the user clicks on a link and scrolls up the page, hide list
  104. $("div.jump-to-list").hide();
  105. }
  106. function showInitialJumpedTo() {
  107. // Toggle classes
  108. $("div.jump-to-click-area").removeClass("jump-to-click-area-toggle");
  109. $("div.jump-to").removeClass("jump-to-toggle");
  110. $("div.jump-to-list").removeClass("jump-to-list-toggle");
  111. $("img.jump-to-img").removeClass("jump-to-img-toggle");
  112. // If the user clicks on a link and scrolls up the page, toggle list
  113. $("div.jump-to-list").show();
  114. hasScrolledDown = false;
  115. }
  116. // When the user scrolls down, change "position" to "fixed"
  117. $(window).scroll(function() {
  118. // If the web page is opened on a mobile
  119. if($(window).width() <= 499) {
  120. // Declarations
  121. var scrollPosition = $(window).scrollTop();
  122. var jumpToPosition = $("div.jump-to").offset().top;
  123. // If the user has scrolled down to "Jump To..."
  124. if(hasScrolledDown === false && scrollPosition >= jumpToPosition) {
  125. showFixedJumpedTo();
  126. // Update scroll position
  127. $(window).scrollTop($("div.saved-tags").offset().top - 300);
  128. hasScrolledDown = true;
  129. }
  130. // If the user has scrolled up to the top
  131. else if(hasScrolledDown === true && scrollPosition === 0) {
  132. showInitialJumpedTo();
  133. }
  134. }
  135. // If the web page is not open on a mobile
  136. else {
  137. showInitialJumpedTo();
  138. }
  139. });
  140. // If the user clicks on "Jump to", show menu
  141. $("div.jump-to-click-area").on("click", function() {
  142. // Toggle the menu
  143. $("div.jump-to div div").slideToggle(300);
  144. // Rotate arrow
  145. // $("div.jump-to span.jump-to-arrow").toggleClass("rotate-arrow-180");
  146. // $("div.jump-to span.jump-to-arrow").toggleClass("rotate-arrow-0");
  147. $("img.jump-to-img").toggleClass("jump-to-arrow-90");
  148. $("img.jump-to-img").toggleClass("jump-to-arrow-0");
  149. return false;
  150. });
  151. // If the user clicks on a menu item
  152. $("div.jump-to ul a").on("click", function() {
  153. // Declarations
  154. var chosenLiIndex = $(this).parent().index();
  155. var chosenFieldPosition;
  156. var scrollPositionNew;
  157. // Detect the scroll position of the chosen field
  158. // If the user has chosen the list item 0
  159. if(chosenLiIndex === 0) {
  160. chosenFieldPosition = $("div.saved-tags").offset().top;
  161. }
  162. // If the user has chosen the list item 1-9
  163. else if(chosenLiIndex >= 1 && chosenLiIndex <= 9) {
  164. chosenFieldPosition = $("div.field_0" + chosenLiIndex).offset().top;
  165. }
  166. // If the user has chosen the list item 10 or higher
  167. else {
  168. chosenFieldPosition = $("div.field_" + chosenLiIndex).offset().top;
  169. }
  170. // If the user hasn't scrolled down
  171. if(hasScrolledDown === false) {
  172. console.log(chosenLiIndex);
  173. // Set the new scroll position
  174. scrollPositionNew = chosenFieldPosition - $("div.jump-to").height() - 89;
  175. // Add classes
  176. showFixedJumpedTo();
  177. // Update scroll position
  178. hasScrolledDown = true;
  179. }
  180. // If the user has scrolled down to "Jump To..."
  181. else {
  182. // Set the new scroll position
  183. scrollPositionNew = chosenFieldPosition - 57;
  184. // Toggle the menu
  185. $("div.jump-to-list").slideToggle(300);
  186. // Rotate arrow
  187. // $("div.jump-to span.jump-to-arrow").toggleClass("rotate-arrow-90");
  188. // $("div.jump-to span.jump-to-arrow").toggleClass("rotate-arrow-0");
  189. $("img.jump-to-img").toggleClass("jump-to-arrow-90");
  190. $("img.jump-to-img").toggleClass("jump-to-arrow-0");
  191. }
  192. // Apply the new scroll position
  193. $(window).scrollTop(scrollPositionNew);
  194. return false;
  195. });
  196. /* ================================================
  197. Show tooltip for every category
  198. ================================================= */
  199. $("a[data-toggle='tooltip']").tooltip({container: "body"});
  200. $("a[data-toggle='tooltip']").on("click", function() {
  201. return false;
  202. });
  203. /* ================================================
  204. Remove all tags from all fields
  205. ================================================= */
  206. // Remove all tags from all fields
  207. function removeTags() {
  208. // AJAX
  209. $.ajax({
  210. type: "POST",
  211. url: "php/get-tags.php",
  212. dataType: "JSON",
  213. data: {
  214. "username": username
  215. },
  216. timeout: 5000,
  217. success: function(returnData) {
  218. // Declarations
  219. var loopCounter = 0;
  220. tags = returnData;
  221. // For every added item
  222. $("li.added_item div").each(function() {
  223. var thisDiv = $(this); // Delete the declaration (because it's ugly!)
  224. // For every tag in the database that belongs to the active user
  225. for(t in tags) {
  226. // If the current tag exists in the textarea
  227. if(thisDiv.html().indexOf("</a>") != -1) {
  228. // Delete the tag
  229. var text = thisDiv.html();
  230. text = thisDiv.text().replace("<a class='tag'>" + tags[loopCounter] + "</a>", tags[loopCounter]);
  231. thisDiv.html(text);
  232. }
  233. loopCounter++;
  234. }
  235. });
  236. if(tags.length === 0) {
  237. $("div.saved-tags p").html("You haven't added any tags yet.");
  238. }
  239. },
  240. error: function(xhr) {
  241. console.log(xhr.statusText);
  242. }
  243. });
  244. /*
  245. // The code below is much better than the code above, because no database connection is being called, but the "You haven't added any tags yet" text is not being reset
  246. // Declarations
  247. var loopCounter = 0;
  248. // For every added item
  249. $("li.added_item div").each(function() {
  250. var thisDiv = $(this); // Delete the declaration (because it's ugly)
  251. // For every tag that belongs to the active user
  252. for(t in tags) {
  253. // If the current tag exists in the textarea
  254. if(thisDiv.html().indexOf("</a>") != -1) {
  255. // Delete the tag
  256. var text = thisDiv.html();
  257. text = thisDiv.text().replace("<a class='tag'>" + tags[loopCounter] + "</a>", tags[loopCounter]);
  258. thisDiv.html(text);
  259. }
  260. loopCounter++;
  261. }
  262. });
  263. console.log(tags);
  264. console.log(tags.length);
  265. if(tags.length === 0) {
  266. $("div.saved-tags p").html("You haven't added any tags yet.");
  267. }
  268. */
  269. }
  270. /* ================================================
  271. Get the user's tags from the database and apply them on the canvas
  272. ================================================= */
  273. // Apply tags (Incoming parameter is an array with every tag from the database belonging to the active user)
  274. function applyTags() {
  275. var loopCounter = 0;
  276. // For every added item
  277. // $("li.added_item textarea").each(function() {
  278. $("li.added_item div").each(function() {
  279. // Here the selector is retrieving the objects it is supposed to...
  280. // For every tag in the database that belongs to the active user
  281. for(t in tags) {
  282. // console.log("tags.length: " + tags.length);
  283. // console.log("tag: " + tags[loopCounter]);
  284. // console.log($(this));
  285. // console.log("Now working with " + tags[loopCounter]);
  286. // console.log($(this).html());
  287. // console.log("The tag " + tags[loopCounter] + " has index '" + tags[loopCounter].indexOf("&nbsp;") + "' for '&nbsp;'");
  288. var text = $(this).html();
  289. // If the current tag exists in the textarea
  290. if(text.indexOf(tags[loopCounter]) != -1) {
  291. // console.log($(this).html());
  292. // ... But here the selector only seems to retrieve the first instance
  293. // console.log("Inside if!");
  294. // Apply the tag
  295. text = text.replace(tags[loopCounter], "<a class='tag' contenteditable='false'>" + tags[loopCounter] + "</a>");
  296. $(this).html(text);
  297. }
  298. loopCounter++;
  299. }
  300. });
  301. // Populate "Saved Tags"
  302. loopCounter = 0;
  303. var savedTags = $("div.saved-tags p");
  304. savedTags.html("");
  305. for(t in tags) {
  306. if(savedTags.html() === "You haven't added any tags yet.") {
  307. savedTags.html("");
  308. }
  309. savedTags.append("<a href='#' class='tag'>" + tags[loopCounter] + "</a>").append("&nbsp;&nbsp; ");
  310. loopCounter++;
  311. }
  312. }
  313. // Get the tags from the database
  314. function getTags() {
  315. // Declarations
  316. var username = $("input[name='username']").val();
  317. // AJAX
  318. $.ajax({
  319. type: "POST",
  320. url: "php/get-tags.php",
  321. dataType: "JSON",
  322. data: {
  323. "username": username
  324. },
  325. timeout: 5000,
  326. success: function(returnData) {
  327. if(returnData.length > 0) {
  328. // Assign returnData to the tags variable
  329. tags = returnData;
  330. // Sort the tags
  331. returnData.sort(function (a, b) {
  332. return a.toLowerCase().localeCompare(b.toLowerCase())
  333. });
  334. // Apply the tags on the canvas
  335. applyTags();
  336. showTagWindowOnTagClick();
  337. }
  338. },
  339. error: function(xhr) {
  340. console.log(xhr.statusText);
  341. }
  342. });
  343. }
  344. /* ================================================
  345. Load tags if the user is logged in
  346. ================================================= */
  347. if($("input[name='name']").val() != "") {
  348. getTags();
  349. }
  350. /* ================================================
  351. Tag window functions
  352. ================================================= */
  353. // Declarations
  354. // var textarea = $("div#tag-window textarea#tag-description");
  355. var textareaHasBeenChanged = false;
  356. var tag;
  357. var tagIsNew = false;
  358. var username = $("input[name='username']").val();
  359. // Check if the tag is new
  360. function checkIfTagIsNew() {
  361. // Declarations
  362. var tagToAJAX = tag;
  363. // AJAX
  364. $.ajax({
  365. type: "POST",
  366. url: "php/check-if-tag-exists.php",
  367. dataType: "text",
  368. data: {
  369. "tag": tagToAJAX,
  370. "username": username
  371. },
  372. timeout: 5000,
  373. success: function(returnData) {
  374. // If the current tag does not exist in the database
  375. if(returnData === "") {
  376. // Hide "Delete tag" button
  377. $("div#tag-window button#delete-tag").css("display", "none");
  378. // Hide "Similar tags"
  379. $("div#tag-window h2.similar-tags").css("display", "none");
  380. // The tag is new
  381. tagIsNew = true;
  382. }
  383. // If the current tag exists in the database
  384. else {
  385. // Show "Delete tag" button
  386. $("div#tag-window button#delete-tag").css("display", "inline");
  387. // Show "Similar tags"
  388. $("div#tag-window h2.similar-tags").css("display", "block");
  389. }
  390. },
  391. error: function(xhr) {
  392. console.log(xhr.statusText);
  393. }
  394. });
  395. }
  396. // Updating the remaining characters information
  397. function updateRemainingCharacters() {
  398. // Declarations
  399. var length = $("div#tag-window textarea#tag-description").val().length; // Retrieving from "Please enter a description..."
  400. var maxLength = 200;
  401. /*
  402. var length;
  403. if(description != "") {
  404. length = description;
  405. }
  406. else {
  407. length = $("div#tag-window textarea#tag-description").val().length;
  408. }
  409. */
  410. // Update length
  411. length = maxLength - length;
  412. // Show the remaining characters
  413. $("div#tag-window span.chars").text(length);
  414. }
  415. // Get the description for the selected tag
  416. function getDescriptionForSelectedTag() {
  417. // Declarations
  418. var tagToAJAX = tag;
  419. // AJAX
  420. $.ajax({
  421. type: "POST",
  422. url: "php/get-description-for-selected-tag.php",
  423. dataType: "text",
  424. data: {
  425. "tag": tagToAJAX,
  426. "username": username
  427. },
  428. timeout: 5000,
  429. success: function(returnData) {
  430. if(returnData != "") {
  431. // Update description field with the description for the selected tag
  432. $("div#tag-window textarea#tag-description").val(returnData);
  433. // Update remaining characters
  434. updateRemainingCharacters();
  435. }
  436. textareaHasBeenChanged = true;
  437. },
  438. error: function(xhr) {
  439. console.log(xhr.statusText);
  440. }
  441. });
  442. }
  443. // Get similar tags by other users
  444. function getSimilarTags() {
  445. // Declarations
  446. var tagToAJAX = tag;
  447. // AJAX
  448. $.ajax({
  449. type: "POST",
  450. url: "php/get-similar-tags.php",
  451. dataType: "JSON",
  452. data: {
  453. "tag": tagToAJAX,
  454. "username": username
  455. },
  456. timeout: 5000,
  457. success: function(returnData) {
  458. // Reset similar tags if similar tags are to be showed again
  459. $("p.similar-tags-tag").remove();
  460. $("p.similar-tags-description").remove();
  461. $("p.similar-tags-username").remove();
  462. // Declarations
  463. var htmlToAppend = "";
  464. // If similar tags exist
  465. if(returnData.length > 0) {
  466. // Declarations
  467. var index = 0;
  468. // While there still are tags to append
  469. while(index < returnData.length) {
  470. htmlToAppend += "<p class='similar-tags-tag'><a class='tag'>" + returnData[index]["tag"] + "</a></p>"
  471. + "<p class='similar-tags-description'>" + returnData[index]["description"] + "</p>"
  472. + "<p class='similar-tags-username'>by <a href='#'>" + returnData[index]["username"] + "</a></p>";
  473. index++;
  474. }
  475. // Append tags
  476. $("div#tag-window h2.similar-tags").after(htmlToAppend);
  477. $("p.similar-tags-description-none").css("display", "none");
  478. // Apply the tags on the canvas
  479. // applyTags();
  480. // showTagWindowOnTagClick();
  481. }
  482. else {
  483. // If the tag isn't a new tag
  484. if(tagIsNew === false) {
  485. // Show the message saying that there are no similar tags to show
  486. $("p.similar-tags-description-none").css("display", "block");
  487. }
  488. }
  489. },
  490. error: function(xhr) {
  491. console.log(xhr.statusText);
  492. }
  493. });
  494. }
  495. // Close the tag window
  496. function closeTagWindow() {
  497. $("div#shadow").css("display", "none");
  498. $("div#tag-window").css("display", "none");
  499. }
  500. // Show the tag window
  501. function showTagWindow() {
  502. // Show the tag window
  503. $("div#shadow").css("display", "block");
  504. $("div#tag-window").css("display", "block");
  505. // Move the window to the centre of the screen
  506. /*
  507. The code below should be working, but the height of the second selector seems completely wrong!
  508. var middle = (document.body.clientHeight / 2) - ($("div#tag-window").height() / 2);
  509. $("div#tag-window").css("top", middle);
  510. */
  511. // Check if the tag is new
  512. checkIfTagIsNew();
  513. // Get the description for the selected tag
  514. getDescriptionForSelectedTag();
  515. // Get tags by other users from the database
  516. getSimilarTags();
  517. // Update remaining characters in case description is loaded
  518. updateRemainingCharacters();
  519. // If a description hasn't been entered
  520. if($("div#tag-window textarea#tag-description") !== "Please enter a description..." && textareaHasBeenChanged !== true) {
  521. // Reset the remaining characters information
  522. $("div#tag-window span.chars").text("200");
  523. }
  524. }
  525. // If the user presses a key in the description textarea
  526. $("div#tag-window textarea#tag-description").on("keyup", function() {
  527. // Update remaining characters
  528. updateRemainingCharacters();
  529. });
  530. // If the user clicks on the "Save tag" button
  531. $("div#tag-window #save-tag").on("click", function() {
  532. // Declarations
  533. var description = $("div#tag-window textarea#tag-description").val();
  534. var tagToAJAX = tag;
  535. // If a description has been entered
  536. if(description != "" && description != "Please enter a description...") {
  537. // AJAX
  538. $.ajax({
  539. timeout: 5000,
  540. dataType: "text",
  541. type: "post",
  542. data: {
  543. "tag": tagToAJAX,
  544. "description": description,
  545. "username": username
  546. },
  547. url: "php/save-tag.php",
  548. success: function() {
  549. textareaHasBeenChanged = true;
  550. // Get the user's tags from the database and apply them on the canvas
  551. getTags();
  552. },
  553. error: function(xhr) {
  554. console.log(xhr.statusText);
  555. }
  556. });
  557. // Close the tag window
  558. closeTagWindow();
  559. }
  560. tagIsNew = false;
  561. });
  562. // If the user clicks on the "Close" button
  563. $("div#tag-window button.close").on("click", function() {
  564. // Close the tag window
  565. closeTagWindow();
  566. });
  567. // If the user clicks on the "Delete tag" button
  568. $("div#tag-window #delete-tag").on("click", function() {
  569. // Declarations
  570. var tagToAJAX = tag;
  571. // AJAX
  572. $.ajax({
  573. timeout: 5000,
  574. dataType: "text",
  575. type: "post",
  576. data: {
  577. "tag": tagToAJAX,
  578. "username": username
  579. },
  580. url: "php/delete-tag.php",
  581. success: function() {
  582. // Remove all tags from all fields
  583. removeTags();
  584. // Get the user's tags from the database and apply them on the canvas
  585. getTags();
  586. },
  587. error: function(xhr) {
  588. console.log(xhr.statusText);
  589. }
  590. });
  591. // Close the tag window
  592. closeTagWindow();
  593. });
  594. // If the user focuses in the textarea
  595. $("div#tag-window textarea#tag-description").on("focusin", function() {
  596. // Declarations
  597. var description = $("div#tag-window textarea#tag-description").val();
  598. // If a description hasn't been entered
  599. // if(textareaHasBeenChanged === false) {
  600. if(description === "Please enter a description...") {
  601. // Empty the description textarea
  602. $("div#tag-window textarea#tag-description").val("");
  603. }
  604. });
  605. // If the user focuses out the textarea
  606. $("div#tag-window textarea#tag-description").on("focusout", function() {
  607. // Declarations
  608. var description = $("div#tag-window textarea#tag-description").val();
  609. // If a description hasn't been entered
  610. // if(textareaHasBeenChanged === false) {
  611. if(description === "") {
  612. // Reset the description textarea
  613. $("div#tag-window textarea#tag-description").val("Please enter a description...");
  614. }
  615. });
  616. /* ================================================
  617. If the user types a new character in the added idea field, add tag if a term could be found
  618. ================================================= */
  619. // 8an33j24
  620. function applyTagOnTypeMatch() {
  621. $("li.added_item div").on("focusin", function() {
  622. // If the user presses a key in the description textarea
  623. $("li.added_item div").on("keyup", function() {
  624. if(event.which !== 9 && // Tab
  625. event.which !== 16 && // Shift
  626. event.which !== 37 && // Left
  627. event.which !== 38 && // Up
  628. event.which !== 39 && // Right
  629. event.which !== 40) { // Down
  630. // Declarations
  631. var bugCounter = 0;
  632. var loopCounter = 0;
  633. var tagLinkToSearch;
  634. var tagLinkHits;
  635. var tagTextToSearch;
  636. var tagTextHits;
  637. var text = $(this).html();
  638. // Fix the bug that triggers the event twice
  639. if(!bugCounter > 0) {
  640. /*
  641. console.log("tagTextHits: " + tagTextHits);
  642. console.log("tagLinkHits: " + tagLinkHits);
  643. */
  644. // For every tag that belongs to the active user
  645. for(t in tags) {
  646. if($(this).html().indexOf(tags[loopCounter]) != -1) {
  647. // Search for the tag inside the added idea as a link
  648. tagLinkToSearch = text.match(tags[loopCounter] + "</a>"); // match() can only count to one instance (67hi9nt3)
  649. // Search for the tag inside the added idea as plain text
  650. tagTextToSearch = text.match(tags[loopCounter]); // match() can only count to one instance (67hi9nt3)
  651. // If the tag exists inside the added idea as a link
  652. if(tagLinkToSearch !== null) {
  653. // tagLinkHits = 1
  654. tagLinkHits = tagLinkToSearch.length;
  655. }
  656. else {
  657. // tagLinkHits = 0
  658. tagLinkHits = 0;
  659. }
  660. // If the tag exists inside the added idea as plain text
  661. if(tagTextToSearch !== null) {
  662. // tagTextHits = 1
  663. tagTextHits = tagTextToSearch.length;
  664. }
  665. else {
  666. // tagTextHits = 0
  667. tagTextHits = 0;
  668. }
  669. // If there are more instances of the tag as plain text than the tag as links, and there is a maximum of 1 instances of the tag as plain text
  670. // Change "tagTextHits <= 1" to "tagLinkHits === 0"
  671. if(tagTextHits > tagLinkHits && tagLinkHits === 0) {
  672. // Apply the tag
  673. text = text.replace(tags[loopCounter], "<a class='tag' contenteditable='false'>" + tags[loopCounter] + "</a>");
  674. $(this).html(text);
  675. }
  676. }
  677. loopCounter++;
  678. }
  679. }
  680. bugCounter++;
  681. }
  682. });
  683. });
  684. };
  685. /* ----------------------------------------------
  686. Limiting the number of characters the user is able to type
  687. ----------------------------------------------- */
  688. var maxLength = 100;
  689. $('.card').on('keyup', '.new_item', function() {
  690. var length = $(this).val().length;
  691. length = maxLength - length;
  692. // show the characters remaining only on this field
  693. $(this).closest('.user-input').find('.chars').text(length);
  694. });
  695. function limitLengthOnInput() {
  696. // Limit text on key press
  697. $("li.added_item div").on("keypress", function(event) {
  698. // Declarations
  699. var numberOfTags = $(this).children().filter("a").length;
  700. var textLength = $(this).html().length;
  701. // Subtract 43 from textLength per tag (the tag HTML is 43 characters)
  702. $(this).each(function() {
  703. textLength -= 43 * numberOfTags;
  704. });
  705. if(textLength === 100) { // Windows menu/Right cmd
  706. event.preventDefault();
  707. }
  708. });
  709. // 28jek79t
  710. // Limit text on paste
  711. /*
  712. $("li.added_item div").on("paste", function(event) {
  713. var pastedText;
  714. $("li.added_item div").bind("paste", function(e) {
  715. pastedText = e.originalEvent.clipboardData.getData("Text");
  716. console.log(pastedText);
  717. if($(this).html().length + pastedText >= 100) {
  718. event.preventDefault();
  719. }
  720. });
  721. // var text = $(this).html()
  722. // var length = text.length;
  723. // var newText = text.substring(0, maxLength);
  724. // if(length >= maxLength) {
  725. // $(this).html(newText);
  726. // Move the text cursor to the very end
  727. }
  728. });
  729. */
  730. }
  731. /* ================================================
  732. If the user closes a dialog
  733. ================================================= */
  734. $("div.dialog button").on("click", function() {
  735. $("div#shadow").css("display", "none");
  736. $("div.dialog").css("display", "none");
  737. });
  738. /* ================================================
  739. The user clicks on the "Tag selected term" link
  740. ================================================= */
  741. // Declaration
  742. var selection = "";
  743. // var newRange = "";
  744. // Initiate tag
  745. tag = "";
  746. // Update the tag variable
  747. document.onselectionchange = function() {
  748. // if($("textarea").is(":focus")) {
  749. if($("li.added_item div").is(":focus")) { // This is nicer: $("li.added_item div").on("focusin", function() {
  750. selection = window.getSelection().toString();
  751. tag = selection.trim();
  752. }
  753. }
  754. // If the user clicks on the "Tag selected term" link
  755. $("p.tag-selected-term a").on("click", function() {
  756. // If the tag isn't empty
  757. if(tag != "") {
  758. // Show the tag window
  759. showTagWindow();
  760. }
  761. else {
  762. // Show a dialog
  763. $("div#shadow").css("display", "block");
  764. $("div#dialog-select-term").css("display", "block");
  765. }
  766. // Prevent the current view to jump to the top of the screen
  767. return false;
  768. });
  769. // If the user moves the focus from the added idea, reset variables
  770. // $("li.added_item textarea").on("focusout", function() {
  771. $("li.added_item div").on("focusout", function() {
  772. selection = "";
  773. tag = "";
  774. });
  775. /* ================================================
  776. If the user clicks on a tag, show the tag window
  777. ================================================= */
  778. // If the user clicks on a tag
  779. function showTagWindowOnTagClick() {
  780. $("a.tag").on("click", function() { // Event not triggered...
  781. // Declarations
  782. tag = $(this).text();
  783. // If the user is logged in
  784. if($("input[name='username']").val() != "") {
  785. // Show the tag window
  786. showTagWindow();
  787. }
  788. else {
  789. // Show a dialog
  790. $("div#shadow").css("display", "block");
  791. $("div#dialog-log-in").css("display", "block");
  792. }
  793. });
  794. }
  795. /*
  796. // Mouse-enabled devices
  797. $("tag").mouseenter(function() {
  798. timer = setTimeout(function() {
  799. showTagWindow(); // showTagWindow needs a parameter!
  800. }, 400);
  801. }).mouseleave(function() {
  802. clearTimeout(timer);
  803. });
  804. // Touch devices
  805. try {
  806. document.createEvent("TouchEvent");
  807. $("a.tag").on("click", showTagWindow()); // showTagWindow needs a parameter!
  808. return true;
  809. }
  810. catch(e) {
  811. return false;
  812. }
  813. */
  814. /* ================================================
  815. Serialize Form to JSON
  816. ================================================= */
  817. $.fn.serializeObject = function() {
  818. var o = {};
  819. var a = this.serializeArray();
  820. $.each(a, function() {
  821. if (o[this.name]) {
  822. if (!o[this.name].push) {
  823. o[this.name] = [o[this.name]];
  824. }
  825. o[this.name].push(this.value || '');
  826. } else {
  827. o[this.name] = this.value || '';
  828. }
  829. });
  830. return o;
  831. };
  832. /* ================================================
  833. Getting the current date
  834. ================================================= */
  835. var fullDate = new Date();
  836. var twoDigitMonth = fullDate.getMonth() + "";
  837. if (twoDigitMonth.length == 1) twoDigitMonth = "0" + twoDigitMonth;
  838. var twoDigitDate = fullDate.getDate() + "";
  839. if (twoDigitDate.length == 1) twoDigitDate = "0" + twoDigitDate;
  840. var currentDate = fullDate.getFullYear() + "-" + twoDigitMonth + "-" + twoDigitDate;
  841. // set the currebt date in the date input field
  842. $('.proj_date').val(currentDate);
  843. /* ================================================
  844. USER LOGS OUT (dropdown menu)
  845. ================================================= */
  846. $('.user-profile').on('click', '#logout', function() {
  847. var url = 'php/logout.php';
  848. $.post(url, function(data, status) {
  849. if (data == 200) {
  850. $('.user-profile').hide();
  851. window.location.href="https://www.ethicscanvas.org";
  852. }
  853. });
  854. });
  855. /* ================================================
  856. When the page loads, Import the chosen canvas if the user has picked one from the dashbord,
  857. otherwise load an empty canvas
  858. ================================================= */
  859. // if a canvas is chosen by the user to be loaded
  860. if (current_canvas_id !== '') {
  861. var url = 'json/' + current_canvas_id + '.json';
  862. // var url= 'json/test_canvas.json';
  863. // get the saved ISON object in the sendJSON.text file
  864. $.getJSON(url, function(returnedObj) {
  865. // Display the json data in the html
  866. var itemListHTML = '';
  867. // iterate through the object
  868. $.each(returnedObj, function(key, value) {
  869. // project name and tem field
  870. if (key === 'field_00[]') {
  871. $('.form-header').find('input.proj_title').val(value[
  872. 0]);
  873. $('.form-header').find('input.proj_date').val(value[1]);
  874. }
  875. else if (key !== 'new_item') {
  876. if ($.type(value) === "array") {
  877. $.each(value, function(i, itm) {
  878. /** FIX DUPLICATIONs in the canvas when importing
  879. /* Importing will override the canvas content
  880. clear the canvas by giving en emty content to the ul list (remove previous list items) */
  881. $('.canvas-form').find('.card').filter('.' + key.substr(0, 8)).find('ul.item_list').html('');
  882. /* Create a list item with each value item
  883. and give it text area with the name attribute as the "key" (right field name) */
  884. itemListHTML +=
  885. // '<li class="added_item"><span class="handle glyphicon glyphicon-move"></span><textarea maxlength="100" class="expandable" rows="3" data-limit-rows="true" data-autoresize name="' + key + '">' + itm + '</textarea><span class="remove glyphicon glyphicon-remove-circle"></span></li>';
  886. '<li class="added_item"><div class="expandable" name="' + key + '" contenteditable="true">' + itm + '</div><span class="handle glyphicon glyphicon-list"></span><span class="move-up glyphicon glyphicon-chevron-up"></span><span class="move-down glyphicon glyphicon-chevron-down"></span><span class="remove glyphicon glyphicon-remove-circle"></span></li>';
  887. });
  888. }
  889. else { // a single value string
  890. itemListHTML +=
  891. // '<li class="added_item"><span class="handle glyphicon glyphicon-move"></span><textarea maxlength="100" class="expandable" rows="3" data-limit-rows="true" data-autoresize name="' + key + '">' + value + '</textarea><span class="remove glyphicon glyphicon-remove-circle"></span></li>';
  892. '<li class="added_item"><div class="expandable" name="' + key + '" contenteditable="true">' + value + '</div><span class="handle glyphicon glyphicon-list"></span><span class="move-up glyphicon glyphicon-chevron-up"></span><span class="move-down glyphicon glyphicon-chevron-down"></span><span class="remove glyphicon glyphicon-remove-circle"></span></li>';
  893. }
  894. /* Append the created list items/textatreas to the right field based on the "key"*/
  895. /* the str.substr(start,length)
  896. is used to remove the [] from the end of the "key"name (for each field. also the name attributes accociated with each fiels) so that we can select the right class (right field) and append the created lists to the right field
  897. so field names/key/name attribute will tuen into class names: ex: field_1[] becomes field_1
  898. */
  899. // find the field by its class names besed on the current key name
  900. // append the created list of item values to that right field
  901. $('.canvas-form').find('.card').filter('.' + key.substr(0, 8)).find('ul.item_list').append(itemListHTML);
  902. /*$('form').find('.card').filter('.field_1').find('ul.item_list').append(itemListHTML); */
  903. /* !! Empty the item list after each count of "key" so that the previous item lists from the other fields (associated with the previous key) don't get added up to the item list of other fields */
  904. itemListHTML = '';
  905. }
  906. });
  907. });
  908. // Get the user's tags from the database and apply them on the canvas
  909. getTags();
  910. // Show "Tag selected term" link
  911. if($(this).parent().parent().parent().prev().prev().children().length > 0) {
  912. $(this).parent().parent().parent().prev().css("display", "block");
  913. }
  914. // Fix the heights after importing
  915. // fixHeights();
  916. }
  917. /* ================================================
  918. Toggle the introduction text in fields
  919. ================================================= */
  920. //$(selector).toggle(speed,easing,callback)
  921. $('.card').on('click', '.intro-toggle', function() {
  922. var $TogglingText = $($(this).closest('.card').find('.intro'));
  923. var $Toggler = $($(this).closest('.card').find('.intro-toggle'));
  924. $TogglingText.toggle('slow', function() {
  925. // Do this when toggling:
  926. // the boolean .is(':visible') of the current toggle state
  927. if ($TogglingText.is(':visible')) {
  928. // change the text of the toggle
  929. $Toggler.find('.intro-toggle-text').text('Hide description');
  930. // change the icon of the toggle
  931. $Toggler.find('.intro-toggle-icon').switchClass("glyphicon-plus-sign", "glyphicon-minus-sign", 1000, "easeInOutQuad");
  932. }
  933. else {
  934. $Toggler.find('.intro-toggle-text').text('Show description');
  935. $Toggler.find('.intro-toggle-icon').switchClass("glyphicon-minus-sign", "glyphicon-plus-sign", 1000, "easeInOutQuad");
  936. }
  937. });
  938. });
  939. /* ================================================
  940. Auto expand user input textareas
  941. ================================================= */
  942. /* Works for textareas already exsting in the html when the page loads -> User input
  943. $.each($('textarea[data-autoresize]'), function() {
  944. var offset = this.offsetHeight - this.clientHeight;
  945. var resizeTextarea = function(el) {
  946. $(el).css('height', 'auto').css('height', el.scrollHeight + offset);
  947. };
  948. $(this).on('keyup input', function() {
  949. resizeTextarea(this);
  950. }).removeAttr('data-autoresize');
  951. });
  952. /* ================================================
  953. Limiting the number of lines in textareas
  954. ================================================= */
  955. // <textarea data-limit-rows="true" cols="60" rows="8"></textarea>
  956. /*
  957. $('.card').on('keypress', 'textarea[data-limit-rows=true]', function(event) {
  958. var textarea = $(this),
  959. text = textarea.val(),
  960. // match() -> Searches a string for a match against a regular expression, and returns the matches, as an Array object.
  961. numberOfLines = (text.match(/\n/g) || []).length + 1,
  962. maxRows = parseInt(textarea.attr('rows'));
  963. // if the number of lines have reached the max rows
  964. if (numberOfLines === maxRows) {
  965. return false;
  966. }
  967. });
  968. */
  969. /* ================================================
  970. Handling user input, ADD items
  971. A. Add button
  972. B. Clicking enter
  973. ================================================= */
  974. /* ----------------------------------------------
  975. add new idea slide effect
  976. ----------------------------------------------- */
  977. // When clicking on "add a new idea", Slide down and show the input field for adding a new item (from the begining, it is set to display:hidden with CSS). If clicked again, slide it up. After that, set the textarea in automatic focus
  978. $('.card').on('click', 'a.add-idea', function(event) {
  979. // stop the default behavior of the link (jumping back to the start of the page)
  980. event.preventDefault();
  981. // set the textarea automatically in focus
  982. $(this).closest('.card').find('.user-input').slideToggle("slow", function() {
  983. // When the toggle animation is complete:
  984. // set the text area in focus
  985. $(this).closest('.card').find('.new_item').val('');
  986. $(this).closest('.card').find('.chars').text(maxLength);
  987. $(this).closest('.card').find('.new_item').focus();
  988. });
  989. });
  990. /* ----------------------------------------------
  991. A. When we click the add btn to
  992. add the item to the list
  993. ----------------------------------------------- */
  994. // event deligation to handle the present and future elements that are dynamically added
  995. $('.card').on('click', '.add_btn', function() {
  996. var new_item = $(this).closest('.card').find('.new_item').val();
  997. var new_item_height = $(this).closest('.card').find('.new_item').height();
  998. // number of items are in the list
  999. var fieldItemCount = $(this).closest('.card').find('ul.item_list').find('li').length;
  1000. // new item added, increment the number of items
  1001. fieldItemCount++;
  1002. // add the input value as a textarea item
  1003. /* create a name attribute in the "field_nr[]" format to be able to tag each new item with the right field attr name (based on the field they are added to). This is to format the json file for each group of dynamically added items. We get the name attribute directly from the id attribute of the ul list in each card (the one we pressed the add button in) */
  1004. // if the new item input exist (is not empty), add the item
  1005. if (new_item) {
  1006. var field_attr = $(this).closest('.card').find('ul.item_list').attr('id') + '[]';
  1007. // The height of the newly added item = the height of the add new idea textarea
  1008. $(this).closest('.card').find('ul.item_list').append(
  1009. // '<li class="added_item"><span class="handle glyphicon glyphicon-move"></span><textarea maxlength="100" class="expandable" rows="3" data-limit-rows="true" data-autoresize name="' + field_attr + '">' + new_item + '</textarea><span class="remove glyphicon glyphicon-remove-circle"></span></li>'
  1010. '<li class="added_item"><div class="expandable" name="' + field_attr + '" contenteditable="true">' + new_item + '</div><span class="handle glyphicon glyphicon-list"></span><span class="move-up glyphicon glyphicon-chevron-up"></span><span class="move-down glyphicon glyphicon-chevron-down"></span><span class="remove glyphicon glyphicon-remove-circle"></span></li>'
  1011. );
  1012. // Fix the heights only after a new item is added
  1013. // fixHeights();
  1014. }
  1015. // clear the new item the text area value
  1016. $(this).closest('.card').find('.new_item').val('');
  1017. /* When clicking on "add idea", hide the input field for adding a new item (slideUp() doesn't work nicely here)*/
  1018. $(this).closest('.card').find('.user-input').hide("fast", function() {
  1019. // Animation complete.
  1020. });
  1021. // Limit the text in the idea field if it reaches the max length
  1022. limitLengthOnInput();
  1023. // Apply event listener to the added idea handling the application of tags should the user writes one of the terms in it
  1024. applyTagOnTypeMatch();
  1025. // Remove all tags from all fields
  1026. removeTags();
  1027. // Get the user's tags from the database and apply them on the canvas
  1028. getTags(); // Really necessary?
  1029. // Show "Tag selected term" link
  1030. if($(this).parent().parent().prev().prev().children().length > 0) {
  1031. $(this).parent().parent().prev().css("display", "block");
  1032. }
  1033. });
  1034. /* ----------------------------------------------
  1035. B. Clicking enter in the add idea textarea,
  1036. will add the new item to the card
  1037. ----------------------------------------------- */
  1038. $('.card').on('keypress', 'textarea[data-limit-rows=true]', function(event) {
  1039. var textarea = $(this);
  1040. var text = textarea.val();
  1041. /* The jQuery event.which -->
  1042. Returns which keyboard key was pressed: */
  1043. // if the enter is pressed, event.which === 13
  1044. if (event.which === 13) {
  1045. var new_item = $(this).closest('.card').find('.new_item').val();
  1046. var new_item_height = $(this).closest('.card').find('.new_item').height();
  1047. // number of items are in the list
  1048. var fieldItemCount = $(this).closest('.card').find('ul.item_list')
  1049. .find('li').length;
  1050. // new item added, increment the number of items
  1051. fieldItemCount++;
  1052. // add the input value as a textarea item
  1053. /* create a name attribute in the "field_nr[]" format to be able to tag each new item with the right field attr name (based on the field they are added to). This is to format the json file for each group of dynamically added items. We get the name attribute directly from the id attribute of the ul list in each card (the one we pressed the add button in) */
  1054. // if the new item input exist (is not empty), add the item
  1055. if (new_item) {
  1056. var field_attr = $(this).closest('.card').find('ul.item_list').attr(
  1057. 'id') + '[]';
  1058. // The height of the newly added item = the height of the add new idea textarea
  1059. $(this).closest('.card').find('ul.item_list').append(
  1060. // '<li class="added_item"><span class="handle glyphicon glyphicon-move"></span><textarea maxlength="100" class="expandable" rows="3" data-limit-rows="true" data-autoresize name="' + field_attr + '">' + new_item + '</textarea><span class="remove glyphicon glyphicon-remove-circle"></span></li>');
  1061. '<li class="added_item"><div class="expandable" name="' + field_attr + '" contenteditable="true">' + new_item + '</div><span class="handle glyphicon glyphicon-list"></span><span class="move-up glyphicon glyphicon-chevron-up"></span><span class="move-down glyphicon glyphicon-chevron-down"></span><span class="remove glyphicon glyphicon-remove-circle"></span></li>'
  1062. );
  1063. // Fix the heights only after a new item is added
  1064. // fixHeights();
  1065. }
  1066. // clear the new item the text area value
  1067. $(this).closest('.card').find('.new_item').val('');
  1068. /* When clicking on "add idea", hide the input field for adding a new item (slideUp() doesn't work nicely here)*/
  1069. $(this).closest('.card').find('.user-input').hide("fast", function() {
  1070. // Animation complete.
  1071. });
  1072. // Limit the text in the idea field if it reaches the max length
  1073. limitLengthOnInput();
  1074. // Apply event listener to the added idea handling the application of tags should the user writes one of the terms in it
  1075. applyTagOnTypeMatch();
  1076. // Remove all tags from all fields
  1077. removeTags();
  1078. // Get the user's tags from the database and apply them on the canvas
  1079. getTags(); // Really necessary?
  1080. // Show "Tag selected term" link
  1081. if($(this).parent().parent().parent().prev().prev().children().length > 0) {
  1082. $(this).parent().parent().parent().prev().css("display", "block");
  1083. }
  1084. }
  1085. });
  1086. /* ================================================
  1087. Moving ideas to a different category
  1088. ================================================= */
  1089. // Declarations
  1090. // var categoryUl = $("div#move-window ul");
  1091. var categoryDestination;
  1092. var categoryLis = $("div#move-window ul").html();
  1093. var categoryOrigin;
  1094. var ideaLi;
  1095. var linkLi;
  1096. var linkText;
  1097. // Remove the link to the current category, as the idea cannot be moved to its own category where it already is
  1098. function removeLinkToOriginCategory(text) {
  1099. // If the number of the category is between 10-99
  1100. if(text.substring(6, 7) != "0") {
  1101. categoryOrigin = text.substring(6, 9) - 1;
  1102. }
  1103. // If the number of the category is between 0-9
  1104. else {
  1105. categoryOrigin = text.substring(7, 9) - 1;
  1106. }
  1107. // Remove "a" tag
  1108. linkLi = $("div#move-window ul li").get(categoryOrigin);
  1109. linkText = $("div#move-window ul a").get(categoryOrigin).innerHTML;
  1110. linkLi.innerHTML = linkText;
  1111. }
  1112. // When the user clicks on the "Move" icon, show the move window
  1113. $('.card').on('click', 'span.handle', function() {
  1114. // Show the move window
  1115. $("div#shadow").css("display", "block");
  1116. $("div#move-window").css("display", "block");
  1117. // Assign the idea that is going to be moved
  1118. ideaLi = $(this).parent();
  1119. // Remove the link to the current category, as the idea cannot be moved to its own category where it already is
  1120. removeLinkToOriginCategory($(this).parent().parent().attr("id"));
  1121. });
  1122. // Close the move window
  1123. function closeMoveWindow() {
  1124. // Close the move window
  1125. $("div#shadow").css("display", "none");
  1126. $("div#move-window").css("display", "none");
  1127. }
  1128. // When the user clicks on the "Close" button
  1129. $("div#move-window button.close").on("click", function() {
  1130. // Close the move window
  1131. closeMoveWindow();
  1132. });
  1133. // When the user clicks on a category, move the idea
  1134. function moveIdeaOnClick() {
  1135. $("div#move-window ul a").on("click", function() {
  1136. // If the number of the category is between 10-99
  1137. if($(this).parent().text().trim().substring(1, 2) != ".") {
  1138. categoryDestination = $(this).parent().text().trim().substring(0, 2);
  1139. }
  1140. // If the number of the category is between 0-9
  1141. else {
  1142. categoryDestination = $(this).parent().text().trim().substring(0, 1);
  1143. }
  1144. // Move the idea
  1145. // If the number of the category is between 10-99
  1146. if(categoryDestination > 9) {
  1147. $("ul#field_" + categoryDestination).append(ideaLi);
  1148. // $("ul#field_0" + (categoryOrigin + 1)).parent().find("p.tag-selected-term").css("display", "none");
  1149. }
  1150. // If the number of the category is between 0-9
  1151. else {
  1152. $("ul#field_0" + categoryDestination).append(ideaLi);
  1153. // $("ul#field_0" + (categoryOrigin + 1)).parent().find("p.tag-selected-term").css("display", "none");
  1154. }
  1155. // Close the move window
  1156. closeMoveWindow();
  1157. // Restore links in the move window
  1158. if($("div#move-window li").length > $("div#move-window a").length) {
  1159. $("div#move-window ul").children().remove()
  1160. $("div#move-window ul").append(categoryLis);
  1161. moveIdeaOnClick();
  1162. }
  1163. // Hide "Tag selected term" link in the origin category if there are no ideas left
  1164. if($("ul#field_0" + (categoryOrigin + 1)).children().length === 0) {
  1165. // If the number of the category is between 10-99
  1166. if((categoryOrigin + 1) > 9) {
  1167. $("ul#field_" + (categoryOrigin + 1)).parent().find("p.tag-selected-term").css("display", "none");
  1168. }
  1169. // If the number of the category is between 0-9
  1170. else {
  1171. $("ul#field_0" + (categoryOrigin + 1)).parent().find("p.tag-selected-term").css("display", "none");
  1172. }
  1173. }
  1174. // Show "Tag selected term" link in the destination category
  1175. // If the number of the category is between 10-99
  1176. if((categoryOrigin + 1) > 9) {
  1177. $("ul#field_" + categoryDestination).parent().find("p.tag-selected-term").css("display", "block");
  1178. }
  1179. // If the number of the category is between 0-9
  1180. else {
  1181. $("ul#field_0" + categoryDestination).parent().find("p.tag-selected-term").css("display", "block");
  1182. }
  1183. });
  1184. }
  1185. moveIdeaOnClick();
  1186. /* ================================================
  1187. Moving ideas up
  1188. ================================================= */
  1189. // When the user clicks on the "Up" button
  1190. $('.card').on('click', 'span.move-up', function() {
  1191. // Declarations
  1192. var ideaLiCurrent = $(this).parent();
  1193. var ideaLiPrevious = $(this).parent().prev();
  1194. // Move the idea
  1195. if(ideaLiPrevious.is("li")) {
  1196. ideaLiCurrent.detach();
  1197. ideaLiPrevious.before(ideaLiCurrent);
  1198. }
  1199. });
  1200. /* ================================================
  1201. Moving ideas down
  1202. ================================================= */
  1203. // When the user clicks on the "Down" button
  1204. $('.card').on('click', 'span.move-down', function() {
  1205. // Declarations
  1206. var ideaLiCurrent = $(this).parent();
  1207. var ideaLiNext = $(this).parent().next();
  1208. // Move the idea
  1209. if(ideaLiNext.is("li")) {
  1210. ideaLiCurrent.detach();
  1211. ideaLiNext.after(ideaLiCurrent);
  1212. }
  1213. });
  1214. /* ================================================
  1215. Deleting ideas
  1216. ================================================= */
  1217. // when the cross beside the textarea is clicked (span.remove)
  1218. // remove that list item
  1219. $('.card').on('click', 'span.remove', function() {
  1220. // If there are no ideas left, hide "Tag selected term" link
  1221. if($(this).prev().parent().parent().children().length === 1) {
  1222. $(this).prev().parent().parent().next().css("display", "none");
  1223. }
  1224. // Remove the list item
  1225. $(this).closest('li').remove();
  1226. });
  1227. /* ================================================
  1228. Sortable field ideas
  1229. ================================================= */
  1230. // make items sortable in their fields and between fields
  1231. /*
  1232. $('.sortable').sortable({
  1233. connectWith: '.connectedList',
  1234. placeholder: "sort-placeholder",
  1235. // revert: true
  1236. zIndex: 300 // Or greater than any other relative/absolute/fixed elements and droppables
  1237. });
  1238. */
  1239. /* ================================================
  1240. Sorting and Dragging events
  1241. ================================================= */
  1242. /* sortstart
  1243. sortover
  1244. sortstop */
  1245. // Every textarea in a item needs to get the right name attribute once they have been dropped in another field (so it ends up in the right place in the json file)
  1246. /*
  1247. // Dragging starts
  1248. $(".sortable").on("sortstart", function(event, ui) {
  1249. // WHEN WE SORT CARDS, $(this) ---> the "begining" ul with the class of .sortable
  1250. });
  1251. // Dragging ends: item dropped
  1252. $(".sortable").on("sortstop", function(event, ui) {
  1253. // get the id of the field ul (to set the name attribute of textareas)
  1254. // # mouseleave is the right event for when we release and leave a card mouseup doesn't work properly in this case
  1255. $('.card').on('mouseleave', 'li', function() {
  1256. //$(selector).attr(attribute,value)
  1257. var fieldAttr = $(this).closest('ul.item_list').attr('id');
  1258. // $(this).find('textarea').attr('name', fieldAttr + '[]');
  1259. $(this).find('li.added_item div').attr('name', fieldAttr + '[]');
  1260. });
  1261. });
  1262. */
  1263. /* ================================================
  1264. SAVING THE CANVAS:
  1265. CLICK ON #EXPORT JSON# form button
  1266. ================================================= */
  1267. /* ----------------------------------------------
  1268. When the user clicks on the SAVE CANVAS button
  1269. ----------------------------------------------- */
  1270. $('.canvas-form').on('click', '.json_exp', function() {
  1271. /* ----------------------------------------------
  1272. A: saving the canvas
  1273. as a registered user
  1274. ----------------------------------------------- */
  1275. // php variables are retieved in the header of the canvas index.php as js variables -->
  1276. var name_save_canvas = $('.form-header').find('.proj_title').val();
  1277. var date_save_canvas = $('.form-header').find('.proj_date').val();
  1278. var save_canvas_obj = {
  1279. 'email_save_canvas': email_save_canvas,
  1280. 'name_save_canvas': name_save_canvas,
  1281. 'date_save_canvas': date_save_canvas
  1282. };
  1283. var save_canvas = $.param(save_canvas_obj);
  1284. /* Post the JSON stringified object to the php file
  1285. (the php script will save it in a .json file )*/
  1286. var save_reg_url = "php/save-canvas.php";
  1287. $.post(save_reg_url, { save_canvas: save_canvas }, function(data, status) {
  1288. //the returned data is successful, is the $canvas_id
  1289. var canvas_id = data;
  1290. // send this canvas_id with the next ajax requestedto the php/canvas.php file and use it as the name of the json file to be saved
  1291. // Give the user feedback that the canvas is saved
  1292. if (data !== 400 || data !== 401) {
  1293. if ($('.imp-exp-btn ').find(".save-canvas-feedback") !== null) {
  1294. $('.imp-exp-btn ').find(".save-canvas-feedback").remove();
  1295. }
  1296. $('.canvas-form').find('.imp-exp-btn ').append('<div class="save-canvas-feedback"><p><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>Your canvas is saved in your dashbord</p></div>');
  1297. // remove the canvas is saves message as soon as user changes the canvas
  1298. // $('.canvas-form').on("change keyup", 'textarea', function() {
  1299. $('.canvas-form').on("change keyup", '.expandable', function() {
  1300. $('.imp-exp-btn ').find(".save-canvas-feedback").remove();
  1301. });
  1302. } else {
  1303. $('.canvas-form').find('.imp-exp-btn ').append('<div class="save-canvas-feedback-fail"><p>Oh! We could not save your canvas. Please try again or contact us at hello@ethicscanvas.org</p></div>');
  1304. }
  1305. // For the second AJAX request:
  1306. /* ----------------------------------------------
  1307. B: Exporing the form data json to a file
  1308. and save it on the server
  1309. ----------------------------------------------- */
  1310. // $('#result').text(JSON.stringify($('.canvas-form').serializeObject()));
  1311. // Make the JSON object into a JSON string
  1312. var JSONstrObj = JSON.stringify($('.canvas-form').serializeObject());
  1313. var url = "php/canvas.php";
  1314. /* Post the JSON stringified object to the php file
  1315. (the php script will save it in a .json file )*/
  1316. // also, send the canvas_id and use it for naming the file
  1317. $.post(url, {
  1318. JSONstrObj: JSONstrObj,
  1319. canvas_id: canvas_id
  1320. }, function(data, status) {
  1321. console.log(
  1322. 'Response from php when sending the form json object: \n' +
  1323. 'data:' + data + '\n status: ' + status);
  1324. }).fail(function(jqXHR) {
  1325. console.log("Error " + jqXHR.status + ' ' + jqXHR.statustext);
  1326. });
  1327. }).fail(function(jqXHR) {
  1328. console.log("Error " + jqXHR.status + ' ' + jqXHR.statustext);
  1329. });
  1330. // Prevent the card item list from reseting itself after clicking on the export button (submission). Because the type of the button is submit
  1331. return false;
  1332. });
  1333. /* ================================================
  1334. HANDLING CLICK ON : Share This Canvas BUTTON
  1335. ================================================= */
  1336. $('.canvas-form').on('click', '.share_canvas', function() {
  1337. $('.share_canvas_email').slideDown(1000, function() {
  1338. // SAVE THE PDF AS file
  1339. $.post('mpdf/canvas-pdf-save.php', function(data, status) { }); // save pdf as file
  1340. });
  1341. });
  1342. $('.canvas-form').on('click', '.share_canvas_send', function() {
  1343. var share_email = $('.share_canvas_email').find('input').serialize();
  1344. // This sends a serialized array share_email to the php file. Example: the value of this array will be: share-canvas-email=eternalflame.f%40gmail.com
  1345. $.post('php/share-canvas.php', {
  1346. share_email: share_email
  1347. }, function(data, status) {
  1348. if (data == 200) { // canvas successfully shared
  1349. $('.canvas-form').find('.imp-exp-btn ').append('<div class="save-canvas-feedback"><p><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>Your canvas has been shared by email</p></div>')
  1350. }
  1351. else {
  1352. $('.canvas-form').find('.imp-exp-btn ').append('<div class="save-canvas-feedback"><p><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>Your canvas could not be shared by email</p></div>')
  1353. }
  1354. });
  1355. // slide up the .share_canvas_email area
  1356. $('.share_canvas_email').slideUp();
  1357. });
  1358. /* ================================================
  1359. Controlling the height of divs dynamically
  1360. ================================================= */
  1361. // Call this function after adding a new item and importing
  1362. // $( window ).width(); ->Returns width of browser viewport
  1363. /*
  1364. function fixHeights() {
  1365. // Returns width of browser viewport
  1366. var screenSize = $(window).width();
  1367. //the longest card of the group 1 .masonry-layout7-5
  1368. var longest_1 = $('.masonry-layout7-5').height();
  1369. //the longest card of the group 2 .masonry-layout7-5
  1370. var longest_2 = $('.masonry-layout4').height();
  1371. // --- 5 COL Range ----
  1372. if (screenSize >= 1139) {
  1373. // inforcing a fixed height ".height(longest_1/2)" will create some layout issues when we try to add new items the add item area will go outside the box and the heights don't increase naturally
  1374. // card group 1:
  1375. // $('.field_05,.field_11, .field_07').css('min-height', longest_1 - longest_1 * 5 / 100);
  1376. // $('.field_06,.field_08, .field_12').css('min-height', longest_1 + longest_1 * 5 / 100);
  1377. // $('.field_01, .field_02').css('min-height', longest_1 * 2 + longest_1 * 1 / 100);
  1378. // card group 2:
  1379. // $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 * 2 - longest_2 * 20 / 100);
  1380. $('.field_05,.field_11, .field_07').css('min-height', longest_1 - longest_1 * 20 / 100);
  1381. $('.field_06,.field_08, .field_12').css('min-height', longest_1 - longest_1 * 20 / 100);
  1382. $('.field_01, .field_02').css('min-height', longest_1 + longest_1 * 40 / 100);
  1383. // card group 2:
  1384. $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 - longest_2 * 10 / 100);
  1385. }
  1386. // 4 COL Range //
  1387. else if (screenSize >= 977 && screenSize <= 1138) {
  1388. // card group 1:
  1389. // row 1
  1390. $('.field_01,.field_06, .field_12,.field_08 ').css('min-height', longest_1 + longest_2 * 20 / 100);
  1391. // row 2
  1392. $('.field_05,.field_11, .field_07,.field_02 ').css('min-height', longest_1 + longest_2 * 20 / 100);
  1393. // card group 2:
  1394. // row 3
  1395. $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 * 2 - longest_2 * 20 / 100);
  1396. } else if (screenSize >= 920 && screenSize <= 976) {
  1397. // card group 1:
  1398. // row 1
  1399. $('.field_01,.field_06, .field_12,.field_08 ').css('min-height', longest_1 + longest_2 * 20 / 100);
  1400. // row 2
  1401. $('.field_05,.field_11, .field_07,.field_02 ').css('min-height', longest_1 + longest_2 * 20 / 100);
  1402. // card group 2:
  1403. // row 3
  1404. $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 * 2 - longest_2 * 20 / 100);
  1405. } else if (screenSize >= 485 && screenSize <= 919) {
  1406. // else if (500 <= screenSize < 920) {
  1407. // card group 1:
  1408. $('.field_01,.field_06, .field_12,.field_08 ').css('min-height', longest_1 * 80 / 100);
  1409. $('.field_05,.field_11, .field_07,.field_02 ').css('min-height', longest_1 * 80 / 100);
  1410. // card group 2:
  1411. $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 * 80 / 100);
  1412. // --- 1 COL Range ----
  1413. } else {
  1414. // card group 1:
  1415. $('.field_01,.field_06, .field_12,.field_08 ').css('min-height', longest_1 * 20 / 100);
  1416. $('.field_05,.field_11, .field_07,.field_02 ').css('min-height', longest_1 * 20 / 100);
  1417. // card group 2:
  1418. $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 * 20 / 100);
  1419. }
  1420. }
  1421. function fixHeights() {
  1422. // Returns width of browser viewport
  1423. var screenSize = $(window).width();
  1424. var field01 = $("div.field_01");
  1425. var field02 = $("div.field_02");
  1426. var field03 = $("div.field_03");
  1427. var field04 = $("div.field_04");
  1428. var field05 = $("div.field_05");
  1429. var field06 = $("div.field_06");
  1430. var field07 = $("div.field_07");
  1431. var field08 = $("div.field_08");
  1432. var field09 = $("div.field_09");
  1433. // Set the height of every field
  1434. var heightOfField01 = field01.height();
  1435. var heightOfField03 = field03.height();
  1436. var heightOfField04 = field04.height();
  1437. var heightOfField03And04 = field03.height() + field04.height();
  1438. var heightOfField09 = field09.height();
  1439. var heightOfField05 = field05.height();
  1440. var heightOfField06 = field06.height();
  1441. var heightOfField05And06 = field05.height() + field06.height();
  1442. var heightOfField02 = field02.height();
  1443. // --- 5 COL Range ---
  1444. if (screenSize >= 935) {
  1445. var longestInGroup1 = Math.max(heightOfField01, heightOfField03, heightOfField04, heightOfField03And04, heightOfField09, heightOfField05, heightOfField06, heightOfField05And06, heightOfField02);
  1446. console.log(longestInGroup1);
  1447. // Field group 1
  1448. switch(longestInGroup1) {
  1449. case heightOfField01:
  1450. $(".field_04").css("min-height", longestInGroup1 + 10);
  1451. $(".field_09").css("min-height", longestInGroup1 + 448);
  1452. $(".field_06").css("min-height", longestInGroup1 + 10);
  1453. $(".field_02").css("min-height", longestInGroup1 + 448);
  1454. break;
  1455. case heightOfField03:
  1456. // Code goes here
  1457. break;
  1458. case heightOfField04:
  1459. // Code goes here
  1460. break;
  1461. case heightOfField03And04:
  1462. // Code goes here
  1463. break;
  1464. case heightOfField09:
  1465. // Code goes here
  1466. break;
  1467. case heightOfField05:
  1468. // Code goes here
  1469. break;
  1470. case heightOfField06:
  1471. // Code goes here
  1472. break;
  1473. case heightOfField05And06:
  1474. // Code goes here
  1475. break;
  1476. case heightOfField02:
  1477. // Code goes here
  1478. break;
  1479. default:
  1480. break;
  1481. }
  1482. }
  1483. // --- 4 COL Range ---
  1484. else if (screenSize >= 935 && screenSize <= 991) {
  1485. // Field group 1
  1486. // Code goes here
  1487. // Field group 2
  1488. // Code goes here
  1489. }
  1490. else if (screenSize >= 992 && screenSize <= 1153) {
  1491. // Field group 1
  1492. // Code goes here
  1493. // Field group 2
  1494. // Code goes here
  1495. }
  1496. // --- 2 COL Range ---
  1497. else if (screenSize >= 500 && screenSize <= 934) {
  1498. // Field group 1
  1499. // Code goes here
  1500. // Field group 2
  1501. // Code goes here
  1502. }
  1503. // --- 2-5 COL Range ---
  1504. if (screenSize >= 500) {
  1505. var longestInGroup2;
  1506. // Field group 2
  1507. // Determine the longest Field in group 2
  1508. if(field07.height() < field08.height()) {
  1509. longestInGroup2 = field08;
  1510. }
  1511. else {
  1512. longestInGroup2 = field07;
  1513. }
  1514. if(longestInGroup2 === field07) {
  1515. $(".field_08").css("min-height", longestInGroup2.height() + 10);
  1516. }
  1517. else {
  1518. $(".field_07").css("min-height", longestInGroup2.height() + 10);
  1519. }
  1520. }
  1521. // --- 1 COL Range ---
  1522. // Field group 1
  1523. // Code goes here
  1524. // Field group 2
  1525. // Code goes here
  1526. }
  1527. */
  1528. /*
  1529. new ResizeSensor($(".field_01, .field_02, .field_03, .field_04, .field_05, .field_06, .field_07, .field_08, .field_09"), function() {
  1530. // fixHeights();
  1531. });
  1532. */
  1533. });