blob: fced563d662d0ea15456238a6ff217c05dbf46d8 [file] [log] [blame]
Rushabh Mehta2fa2f712012-09-24 19:13:42 +05301// ERPNext - web based ERP (http://erpnext.com)
2// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17pscript.onload_questions = function(wrapper) {
18 body = $(wrapper).find('.layout-main-section').get(0);
19
20 wrapper.appframe = new wn.ui.AppFrame($(wrapper).find('.layout-appframe'));
21 wrapper.appframe.title('Knowledge Base');
22
23 // kb
24 var kb = new KnowledgeBase(body);
25
26 // sidebar
27 this.sidebar = new wn.widgets.PageSidebar($(wrapper).find('.questions-tags').get(0), {
28 sections: [
29 {
30 title: 'Top Tags',
31 render: function(body) {
32 new wn.widgets.TagCloud(body, 'Question', function(tag)
33 { kb.set_tag_filter(tag) });
34 }
35 }
36 ]
37 });
38 set_title('Knowledge Base');
39}
40
41// knowledge base object
42// has a box for search or ask a question
43// and list of top rated search results
44//
45function KnowledgeBase(w) {
46 var me = this;
47 this.sort_by = 'modified';
48 this.tag_filter_dict = {};
49
50 this.make_search_bar = function() {
51 this.search = $(w).find('.kb-search-wrapper textarea').get(0);
52
53 $(w).find('.btn.search').click(function() {
54 me.run();
55 })
56 $(w).find('.btn.ask').click(function() {
57 me.ask();
58 })
59 }
60
61 // ask a new question
62 this.ask = function() {
63 if(this.search.value==$(this.search).attr('default_text')) {
64 msgprint('Please enter some text'); return;
65 }
66 this.add_question([]);
67 }
68
69 // suggest a few users who can answer
70 this.suggest = function() {
Rushabh Mehta2d700dd2012-10-01 14:46:37 +053071 this.dialog = new wn.ui.Dialog({
Rushabh Mehta2fa2f712012-09-24 19:13:42 +053072 title: 'Suggest a users',
73 width: 400,
74 fields: [
75 {fieldtype:'HTML', options:'Optional: Suggest a few users who can help you answer this question<br>'},
76 {fieldtype:'Link', fieldname:'profile1', label:'1st User',options:'Profile'},
77 {fieldtype:'Link', fieldname:'profile2', label:'2nd User',options:'Profile'},
78 {fieldtype:'Link', fieldname:'profile3', label:'3rd User',options:'Profile'},
79 {fieldtype:'Button', fieldname:'ask', label:'Add the Question'}
80 ]
81 });
82 this.dialog.fields_dict.ask.input.onclick = function() {
83 me.dialog.hide();
84 me.add_question(values(me.dialog.get_values()));
85 }
86 this.dialog.show();
87 }
88
89 // add a new question to the database
90 this.add_question = function(suggest_list) {
91 $c_page('utilities', 'questions', 'add_question', {
92 question: this.search.value,
93 suggest: suggest_list
94 }, function(r,rt) {
95 $(me.search).val('').blur();
96 me.run();
97 })
98 }
99
100 // where tags that filter will be displayed
101 this.make_tag_filter_area = function() {
102 this.tag_filters = $a(w, 'div', 'kb-tag-filter-area');
103 $a(this.tag_filters,'span','',{marginRight:'4px',color:'#442'}, '<i>Showing for:</i>');
104 this.tag_area = $a(this.tag_filters, 'span');
105 }
106
107 // make a list of questions
108 this.make_list = function() {
109 this.make_tag_filter_area();
110 this.list_area = $a(w, 'div', '', {marginRight:'13px'})
111 this.no_result = $a(w, 'div','help_box',{display:'none'},'No questions asked yet! Be the first one to ask')
112
113 this.list = new wn.ui.Listing({
114 parent: this.list_area,
115 no_results_message: 'No questions found. Ask a new question!',
116 appframe: wn.pages.questions.appframe,
117 as_dict: 1,
118 method: 'utilities.page.questions.questions.get_questions',
119 get_args: function() {
120 var args = {};
121 if(me.search.value) {
122 args.search_text = me.search.value;
123 }
124 if(me.tag_filter_dict) {
125 args.tag_filters = keys(me.tag_filter_dict);
126 }
127 return args
128 },
129 render_row: function(parent, data, listing) {
130 new KBQuestion(parent, data, me);
131 }
132 });
133
134 this.list.run();
135
136 }
137
138 // add a tag filter to the search in the
139 // main page
140 this.set_tag_filter = function(tag) {
141
142 // check if exists
143 if(in_list(keys(me.tag_filter_dict), tag.label)) return;
144
145 // create a tag in filters
146 var filter_tag = new SingleTag({
147 parent: me.tag_area,
148 label: tag.label,
149 dt: 'Question',
150 color: tag.color
151 });
152
153 // remove tag from filters
154 filter_tag.remove = function(tag_remove) {
155 $(tag_remove.body).fadeOut();
156 delete me.tag_filter_dict[tag_remove.label];
157
158 // hide everything?
159 if(!keys(me.tag_filter_dict).length) {
160 $(me.tag_filters).slideUp(); // hide
161 }
162
163 // run
164 me.run();
165 }
166
167 // add to dict
168 me.tag_filter_dict[tag.label] = filter_tag;
169 $ds(me.tag_filters);
170
171 // run
172 me.run();
173 }
174 this.run = function() {
175 this.list.run();
176 }
177
178 this.make_search_bar();
179 this.make_list();
180
181}
182
183// single kb question
184// "question
185// points | tag list"
186
187KBQuestion = function(parent, det, kb) {
188
189 this.make = function() {
190 this.wrapper = $a(parent, 'div', 'kb-question-wrapper');
191 this.q_area = $a($a(this.wrapper, 'div'), 'h3',
192 'kb-questions link_type', {display:'inline', textDecoration:'none'}, det.question);
193 if(det.answers==0) {
194 $(this.q_area).addClass('un-answered')
195 }
196
197 this.q_area.onclick = function() {
198 var q = this;
199 window.location.href = '#!question-view/' + q.id;
200 //loadpage('question-view', function() { pscript.question_view(q.id, q.txt) })
201 }
202
203 this.q_area.id = det.name; this.q_area.txt = det.question;
204
205 new KBItemToolbar({
206 parent: this.wrapper,
207 det: det,
208 with_tags: 1,
209 doctype: 'Question'
210 }, kb)
211
212 }
213
214
215 this.make()
216}
217
218wn.require('app/js/kb_common.js');