passport.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. passport = function () {
  2. //private menbers
  3. let tagData = null;
  4. let convertData = {}
  5. //private methods
  6. function init() {
  7. console.log('passport init')
  8. getTagData();
  9. $(".photo__icon-2").on("click", function(){
  10. $("#fileInput2").click();
  11. });
  12. $(".passport__toggle__head").on("click", function(){
  13. if(!$(this).parent(".passport__toggle").hasClass("toggleClose")) {
  14. $(this).parent(".passport__toggle").addClass("toggleClose");
  15. }else {
  16. $(this).parent(".passport__toggle").removeClass("toggleClose");
  17. }
  18. });
  19. }
  20. function getTagData(){
  21. $.ajax('./js/tagData.json').then(res => {
  22. tagData = res;
  23. // console.log(res)
  24. })
  25. }
  26. function buildPassportHandler(_data){
  27. let data = _data;
  28. console.log(data);
  29. setTimeout(()=>{
  30. let getHealthAction = convertMultipleTag('healthAction', data.healthAction);
  31. let gethealthTopic = convertMultipleTag('healthTopic', data.healthTopic);
  32. let getHealthAge = convertSingleTag('healthAge', data.healthAge);
  33. let getHealthGender= convertSingleTag('healthGender', data.healthGender);
  34. convertData.headSticker = data.headSticker;
  35. convertData.healthNickname = data.healthNickname;
  36. Promise.all([getHealthAction ,gethealthTopic, getHealthAge, getHealthGender]).then((res) => {
  37. console.log(convertData); // [3, 1337, "foo"]
  38. buildTagHtml();
  39. });
  40. }, 200);
  41. }
  42. function convertMultipleTag(_name, _arr) {
  43. return new Promise(resolve => {
  44. // 篩出特定問題資料
  45. let tags = tagData.filter(quesItem => {
  46. // console.log(item)
  47. return quesItem.questionName == _name
  48. }).map(quesItem => {
  49. return quesItem.tags
  50. })
  51. // console.log(tags)
  52. // 轉換回傳資料
  53. let convertArr = [];
  54. _arr.forEach(element => {
  55. // console.log("element: "+element)
  56. tags[0].forEach((_tagItem) => {
  57. // console.log("tagId: "+_tagItem.tagId)
  58. if(_tagItem.tagId == element) {
  59. convertArr.push(_tagItem.answer)
  60. // console.log("tagId: "+_tagItem.answer)
  61. }
  62. });
  63. });
  64. // console.log(convertArr)
  65. if(_name == "healthAction") {
  66. convertData.healthAction = convertArr;
  67. }
  68. if(_name == "healthTopic") {
  69. convertData.healthTopic = convertArr;
  70. }
  71. resolve(_name + " convertMultipleTag success");
  72. return convertArr;
  73. });
  74. }
  75. function convertSingleTag(_name, _value) {
  76. return new Promise(resolve => {
  77. // 篩出特定問題資料
  78. let tags = tagData.filter(quesItem => {
  79. // console.log(item)
  80. return quesItem.questionName == _name
  81. }).map(quesItem => {
  82. return quesItem.tags
  83. })
  84. // console.log(tags)
  85. let converValue = "";
  86. tags[0].forEach((_tagItem) => {
  87. // console.log("tagId: "+_tagItem.tagId)
  88. if(_tagItem.tagId == _value) {
  89. converValue = _tagItem.answer
  90. // console.log("tagId: "+_tagItem.answer)
  91. }
  92. });
  93. if(_name == "healthAge") {
  94. convertData.healthAge = converValue;
  95. }
  96. if(_name == "healthGender") {
  97. convertData.healthGender = converValue;
  98. }
  99. // console.log(converValue)
  100. resolve(_name + " convertMultipleTag success");
  101. return converValue;
  102. });
  103. }
  104. function buildTagHtml() {
  105. $(".passport__toggle-healthTopic .tags").html("");
  106. $(".passport__toggle-healthAction .tags").html("");
  107. $(".passport__intro .photo__img").html("");
  108. $(".passport__intro__nickname h2").html("");
  109. $(".passport__intro__info .introAge").html("");
  110. $(".passport__intro__info .introGender").html("");
  111. convertData.healthTopic.forEach(_tagName => {
  112. $(".passport__toggle-healthTopic .tags").append(
  113. `
  114. <div class="tags__item tags__item-md checked">
  115. <p>${_tagName}</p>
  116. </div>
  117. `
  118. );
  119. });
  120. convertData.healthAction.forEach(_tagName => {
  121. $(".passport__toggle-healthAction .tags").append(
  122. `
  123. <div class="tags__item tags__item-md checked">
  124. <p>${_tagName}</p>
  125. </div>
  126. `
  127. );
  128. });
  129. $(".passport__intro .photo__img").append(
  130. `
  131. <img src="${convertData.headSticker}" />
  132. `
  133. );
  134. $(".passport__intro__nickname h2").text(convertData.healthNickname);
  135. $(".passport__intro__info .introAge").text(convertData.healthAge);
  136. $(".passport__intro__info .introGender").text(convertData.healthGender);
  137. }
  138. {
  139. $(document).ready(function () {
  140. init();
  141. });
  142. }
  143. //public
  144. return {
  145. buildPassportHandler: function(data) {
  146. buildPassportHandler(data)
  147. }
  148. };
  149. };
  150. var passport = new passport();