blob: 0e88728d22eebf3a22ab54536331b34b9e55f509 [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'));
Rushabh Mehtad3c9fb62012-12-21 18:27:40 +053021 wrapper.appframe.add_home_breadcrumb();
22 wrapper.appframe.add_breadcrumb(wn.modules["Knowledge Base"].icon);
Rushabh Mehta2fa2f712012-09-24 19:13:42 +053023 wrapper.appframe.title('Knowledge Base');
24
25 // kb
26 var kb = new KnowledgeBase(body);
27
28 // sidebar
29 this.sidebar = new wn.widgets.PageSidebar($(wrapper).find('.questions-tags').get(0), {
30 sections: [
31 {
32 title: 'Top Tags',
33 render: function(body) {
34 new wn.widgets.TagCloud(body, 'Question', function(tag)
35 { kb.set_tag_filter(tag) });
36 }
37 }
38 ]
39 });
40 set_title('Knowledge Base');
41}
42
43// knowledge base object
44// has a box for search or ask a question
45// and list of top rated search results
46//
47function KnowledgeBase(w) {
48 var me = this;
49 this.sort_by = 'modified';
50 this.tag_filter_dict = {};
51
52 this.make_search_bar = function() {
53 this.search = $(w).find('.kb-search-wrapper textarea').get(0);
54
55 $(w).find('.btn.search').click(function() {
56 me.run();
57 })
58 $(w).find('.btn.ask').click(function() {
59 me.ask();
60 })
61 }
62
63 // ask a new question
64 this.ask = function() {
65 if(this.search.value==$(this.search).attr('default_text')) {
66 msgprint('Please enter some text'); return;
67 }
68 this.add_question([]);
69 }
70
71 // suggest a few users who can answer
72 this.suggest = function() {
Rushabh Mehta2d700dd2012-10-01 14:46:37 +053073 this.dialog = new wn.ui.Dialog({
Rushabh Mehta2fa2f712012-09-24 19:13:42 +053074 title: 'Suggest a users',
75 width: 400,
76 fields: [
77 {fieldtype:'HTML', options:'Optional: Suggest a few users who can help you answer this question<br>'},
78 {fieldtype:'Link', fieldname:'profile1', label:'1st User',options:'Profile'},
79 {fieldtype:'Link', fieldname:'profile2', label:'2nd User',options:'Profile'},
80 {fieldtype:'Link', fieldname:'profile3', label:'3rd User',options:'Profile'},
81 {fieldtype:'Button', fieldname:'ask', label:'Add the Question'}
82 ]
83 });
84 this.dialog.fields_dict.ask.input.onclick = function() {
85 me.dialog.hide();
86 me.add_question(values(me.dialog.get_values()));
87 }
88 this.dialog.show();
89 }
90
91 // add a new question to the database
92 this.add_question = function(suggest_list) {
93 $c_page('utilities', 'questions', 'add_question', {
94 question: this.search.value,
95 suggest: suggest_list
96 }, function(r,rt) {
97 $(me.search).val('').blur();
98 me.run();
99 })
100 }
101
102 // where tags that filter will be displayed
103 this.make_tag_filter_area = function() {
104 this.tag_filters = $a(w, 'div', 'kb-tag-filter-area');
105 $a(this.tag_filters,'span','',{marginRight:'4px',color:'#442'}, '<i>Showing for:</i>');
106 this.tag_area = $a(this.tag_filters, 'span');
107 }
108
109 // make a list of questions
110 this.make_list = function() {
111 this.make_tag_filter_area();
112 this.list_area = $a(w, 'div', '', {marginRight:'13px'})
113 this.no_result = $a(w, 'div','help_box',{display:'none'},'No questions asked yet! Be the first one to ask')
114
115 this.list = new wn.ui.Listing({
116 parent: this.list_area,
117 no_results_message: 'No questions found. Ask a new question!',
118 appframe: wn.pages.questions.appframe,
119 as_dict: 1,
120 method: 'utilities.page.questions.questions.get_questions',
121 get_args: function() {
122 var args = {};
123 if(me.search.value) {
124 args.search_text = me.search.value;
125 }
126 if(me.tag_filter_dict) {
127 args.tag_filters = keys(me.tag_filter_dict);
128 }
129 return args
130 },
131 render_row: function(parent, data, listing) {
132 new KBQuestion(parent, data, me);
133 }
134 });
135
136 this.list.run();
137
138 }
139
140 // add a tag filter to the search in the
141 // main page
142 this.set_tag_filter = function(tag) {
143
144 // check if exists
145 if(in_list(keys(me.tag_filter_dict), tag.label)) return;
146
147 // create a tag in filters
148 var filter_tag = new SingleTag({
149 parent: me.tag_area,
150 label: tag.label,
151 dt: 'Question',
152 color: tag.color
153 });
154
155 // remove tag from filters
156 filter_tag.remove = function(tag_remove) {
157 $(tag_remove.body).fadeOut();
158 delete me.tag_filter_dict[tag_remove.label];
159
160 // hide everything?
161 if(!keys(me.tag_filter_dict).length) {
162 $(me.tag_filters).slideUp(); // hide
163 }
164
165 // run
166 me.run();
167 }
168
169 // add to dict
170 me.tag_filter_dict[tag.label] = filter_tag;
171 $ds(me.tag_filters);
172
173 // run
174 me.run();
175 }
176 this.run = function() {
177 this.list.run();
178 }
179
180 this.make_search_bar();
181 this.make_list();
182
183}
184
185// single kb question
186// "question
187// points | tag list"
188
189KBQuestion = function(parent, det, kb) {
190
191 this.make = function() {
192 this.wrapper = $a(parent, 'div', 'kb-question-wrapper');
193 this.q_area = $a($a(this.wrapper, 'div'), 'h3',
194 'kb-questions link_type', {display:'inline', textDecoration:'none'}, det.question);
195 if(det.answers==0) {
196 $(this.q_area).addClass('un-answered')
197 }
198
199 this.q_area.onclick = function() {
200 var q = this;
201 window.location.href = '#!question-view/' + q.id;
202 //loadpage('question-view', function() { pscript.question_view(q.id, q.txt) })
203 }
204
205 this.q_area.id = det.name; this.q_area.txt = det.question;
206
207 new KBItemToolbar({
208 parent: this.wrapper,
209 det: det,
210 with_tags: 1,
211 doctype: 'Question'
212 }, kb)
213
214 }
215
216
217 this.make()
218}
219
220wn.require('app/js/kb_common.js');