blob: 6992fa1455afe9d1b5838f463498668236f94847 [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +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
Rushabh Mehtab73fa492012-02-24 15:07:39 +053017pscript.onload_questions = function(wrapper) {
18 body = $(wrapper).find('.layout-main-section').get(0);
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053019
Rushabh Mehtaedab76b2012-04-18 16:55:43 +053020 wrapper.appframe = new wn.ui.AppFrame($(wrapper).find('.layout-appframe'));
21 wrapper.appframe.title('Knowledge Base');
22
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053023 // kb
24 var kb = new KnowledgeBase(body);
25
26 // sidebar
Rushabh Mehtaa11bc072012-04-19 17:48:57 +053027 this.sidebar = new wn.widgets.PageSidebar($(wrapper).find('.questions-tags').get(0), {
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053028 sections: [
29 {
30 title: 'Top Tags',
31 render: function(body) {
Rushabh Mehtab73fa492012-02-24 15:07:39 +053032 new wn.widgets.TagCloud(body, 'Question', function(tag)
33 { kb.set_tag_filter(tag) });
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053034 }
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() {
Rushabh Mehtab73fa492012-02-24 15:07:39 +053051 this.search = $(w).find('.kb-search-wrapper textarea').get(0);
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053052
Rushabh Mehtab73fa492012-02-24 15:07:39 +053053 $(w).find('.btn.search').click(function() {
54 me.run();
55 })
56 $(w).find('.btn.ask').click(function() {
57 me.ask();
58 })
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053059 }
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 }
Rushabh Mehtaedab76b2012-04-18 16:55:43 +053066 this.add_question([]);
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053067 }
68
69 // suggest a few users who can answer
70 this.suggest = function() {
71 this.dialog = new wn.widgets.Dialog({
72 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) {
Anand Doshi37df8ab2012-04-20 11:17:10 +053091 $c_page('utilities', 'questions', 'add_question', {
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053092 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
Rushabh Mehtaf81a64e2012-03-07 18:19:41 +0530113 this.list = new wn.ui.Listing({
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530114 parent: this.list_area,
115 no_results_message: 'No questions found. Ask a new question!',
Rushabh Mehtaa11bc072012-04-19 17:48:57 +0530116 appframe: wn.pages.questions.appframe,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530117 as_dict: 1,
118 get_query: function() {
119
120 // filter by search string
121 var v = me.search.value==$(me.search).attr('default_text') ? '' : me.search.value;
122 cond = v ? (' and t1.question like "%'+v+'%"') : '';
123
124 // filter by tags
125 if(me.tag_filter_dict) {
126 for(f in me.tag_filter_dict) {
127 cond += ' and t1.`_user_tags` like "%' + f + '%"'
128 }
129 }
Rushabh Mehta81c8ec22012-04-18 17:14:33 +0530130 return repl('select t1.name, t1.owner, t1.question, t1.modified, t1._user_tags, '
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530131 +'t1._users_voted, t2.first_name, t2.last_name '
132 +'from tabQuestion t1, tabProfile t2 '
133 +'where t1.docstatus!=2 '
134 +'and t1.owner = t2.name'
135 +'%(cond)s order by t1.modified desc', {user:user, cond: cond})
136 },
137 render_row: function(parent, data, listing) {
138 new KBQuestion(parent, data, me);
139 }
140 });
141
142 this.list.run();
143
144 }
145
146 // add a tag filter to the search in the
147 // main page
148 this.set_tag_filter = function(tag) {
149
150 // check if exists
151 if(in_list(keys(me.tag_filter_dict), tag.label)) return;
152
153 // create a tag in filters
154 var filter_tag = new SingleTag({
155 parent: me.tag_area,
156 label: tag.label,
157 dt: 'Question',
158 color: tag.color
159 });
160
161 // remove tag from filters
162 filter_tag.remove = function(tag_remove) {
163 $(tag_remove.body).fadeOut();
164 delete me.tag_filter_dict[tag_remove.label];
165
166 // hide everything?
167 if(!keys(me.tag_filter_dict).length) {
168 $(me.tag_filters).slideUp(); // hide
169 }
170
171 // run
172 me.run();
173 }
174
175 // add to dict
176 me.tag_filter_dict[tag.label] = filter_tag;
177 $ds(me.tag_filters);
178
179 // run
180 me.run();
181 }
182 this.run = function() {
183 this.list.run();
184 }
185
186 this.make_search_bar();
187 this.make_list();
188
189}
190
191// single kb question
192// "question
193// points | tag list"
194
195KBQuestion = function(parent, det, kb) {
196
197 this.make = function() {
198 this.wrapper = $a(parent, 'div', 'kb-question-wrapper');
199 this.q_area = $a($a(this.wrapper, 'div'), 'h3', 'kb-questions link_type', {display:'inline', textDecoration:'none'}, det.question);
200
201 this.q_area.onclick = function() {
202 var q = this;
203 window.location.href = '#!question-view/' + q.id;
204 //loadpage('question-view', function() { pscript.question_view(q.id, q.txt) })
205 }
206
207 this.q_area.id = det.name; this.q_area.txt = det.question;
208
209 new KBItemToolbar({
210 parent: this.wrapper,
211 det: det,
212 with_tags: 1,
213 doctype: 'Question'
214 }, kb)
215
216 }
217
218
219 this.make()
220}
221
Anand Doshi37df8ab2012-04-20 11:17:10 +0530222wn.require('erpnext/utilities/page/kb_common/kb_common.js');