canvas.js 92 KB

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