]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/js/qna.js
QnA - Work on getting questions and answers to appear correctly inline
[quix0rs-gnu-social.git] / plugins / QnA / js / qna.js
1
2 var QnA = {
3
4     // hide all the 'close' and 'best' buttons for this question
5
6     // @fixme: Should use ID
7     close: function(closeButt) {
8         notice = $(closeButt).closest('li.hentry.notice.question');
9         notice.find('input[name=best],[name=close]').hide();
10         notice.find('textarea').hide();
11         notice.find('li.notice-answer-placeholder').hide();
12         notice.find('#answer-form').hide();
13     },
14
15     init: function() {
16
17         var that = this;
18
19         QnA.NoticeInlineAnswerSetup();
20
21         $('input[name=close]').live('click', function() {
22             that.close(this);
23         });
24         $('input[name=best]').live('click', function() {
25             that.close(this);
26         });
27
28
29
30     },
31
32     /**
33      * Open up a question's inline answer box.
34      *
35      * @param {jQuery} notice: jQuery object containing one notice
36      */
37     NoticeInlineAnswerTrigger: function(notice) {
38         console.log('NoticeInlineAnswerTrigger - begin');
39
40         // Find the question notice we're answering...
41         var id = $($('.notice_id', notice)[0]).text();
42         console.log("parent notice id = " + id);
43         var parentNotice = notice;
44
45         // See if the form's already there...
46         var answerForm = $('#answer-form', parentNotice);
47
48         if (answerForm) {
49             console.log("Found the answer form.");
50         } else {
51             console.log("Did not find the answer form.");
52         }
53
54         var placeholder = parentNotice.find('li.notice-answer-placeholder');
55
56         // Pull the parents threaded list and we'll add on the end of it.
57         var list = $('ul.threaded-replies', notice);
58
59         if (list) {
60             console.log("Found the " + list.length + " notice place holders.");
61         } else {
62             console.log("Found the notice answer placeholder");
63         }
64
65         if (list.length == 0) {
66             console.log("list length = 0 adding <ul>");
67             list = $('<ul class="notices threaded-replies xoxo"></ul>');
68             notice.append(list);
69         } else if (list.length == 2) {
70             // remove duplicate ul added by util.js
71             list.last().remove();
72         }
73
74         var answerItem = $('li.notice-answer', list);
75
76         var nextStep = function() {
77             console.log("nextStep - enter");
78
79             // Set focus...
80             var text = answerForm.find('textarea');
81
82             if (text.length == 0) {
83                 throw "No textarea";
84             }
85
86             $('body').click(function(e) {
87                 console.log("got click");
88
89                 var openAnswers = $('li.notice-answer');
90                     if (openAnswers.length > 0) {
91                         var target = $(e.target);
92                         openAnswers.each(function() {
93                             // Did we click outside this one?
94                             var answerItem = $(this);
95                             if (answerItem.has(e.target).length == 0) {
96                                 var textarea = answerItem.find('.notice_data-text:first');
97                                 var cur = $.trim(textarea.val());
98                                 // Only close if there's been no edit.
99                                 if (cur == '' || cur == textarea.data('initialText')) {
100                                     var parentNotice = answerItem.closest('li.notice');
101                                     answerItem.remove();
102                                     parentNotice.find('li.notice-answer-placeholder').show();
103                                 }
104                             }
105                         });
106                     }
107                 });
108
109             text.focus();
110         };
111
112         placeholder.hide();
113
114         if (answerItem.length > 0) {
115             console.log('answerItem length > 0');
116             // Update the existing form...
117             nextStep();
118         } else {
119
120              // Create the answer form entry at the end
121
122              if (answerItem.length == 0) {
123                  console.log("QQQQQ no notice-answer li");
124                  answerItem = $('<li class="notice-answer"></li>');
125
126                  var intermediateStep = function(formMaster) {
127                      console.log("Intermediate step");
128                      var formEl = document._importNode(formMaster, true);
129                      answerItem.append(formEl);
130                      console.log("appending answerItem");
131                      list.append(answerItem); // *after* the placeholder
132                      console.log("appended answerItem");
133                      console.log(answerItem);
134                      var form = answerForm = $(formEl);
135                      QnA.AnswerFormSetup(form);
136
137                      nextStep();
138                  };
139
140                  if (QnA.AnswerFormMaster) {
141                      // We've already saved a master copy of the form.
142                      // Clone it in!
143                      intermediateStep(QnA.AnswerFormMaster);
144                  } else {
145                      // Fetch a fresh copy of the answer form over AJAX.
146                      // Warning: this can have a delay, which looks bad.
147                      // @fixme this fallback may or may not work
148                      var url = $('#answer-action').attr('value');
149
150                      console.log("fetching new form via HXR");
151
152                      $.get(url, {ajax: 1}, function(data, textStatus, xhr) {
153                          intermediateStep($('form', data)[0]);
154                      });
155                  }
156              }
157          }
158          console.log('NoticeInlineAnswerTrigger - exit');
159
160      },
161
162
163     AnswerFormSetup: function(form) {
164         console.log("AnswerFormSetup - begin");
165         if (!form.data('AnswerFormSetup')) {
166             form.data('AnswerFormSetup', true);
167         }
168         console.log("AnswerFormSetup - exit");
169     },
170
171     /**
172      * Setup function -- DOES NOT apply immediately.
173      *
174      * Sets up event handlers for inline reply mini-form placeholders.
175      * Uses 'live' rather than 'bind', so applies to future as well as present items.
176      */
177     NoticeInlineAnswerSetup: function() {
178         console.log("NoticeInlineAnswerSetup - begin");
179         $('li.notice-answer-placeholder input.placeholder')
180             .live('focus', function() {
181                 var notice = $(this).closest('li.notice');
182                 QnA.NoticeInlineAnswerTrigger(notice);
183                 return false;
184             });
185         console.log("NoticeInlineAnswerSetup - exit");
186     }
187
188 };
189
190 $(document).ready(function() {
191     QnA.init();
192 });