canvas.js 73 KB

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