canvas.js 70 KB

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