blob: 841d460e1fa355c17b8bb13860873a196470d704 [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) {
Rushabh Mehtaf4a8c872012-04-23 10:31:29 +053018 console.log(1);
Rushabh Mehtab73fa492012-02-24 15:07:39 +053019 body = $(wrapper).find('.layout-main-section').get(0);
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053020
Rushabh Mehtaedab76b2012-04-18 16:55:43 +053021 wrapper.appframe = new wn.ui.AppFrame($(wrapper).find('.layout-appframe'));
22 wrapper.appframe.title('Knowledge Base');
23
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053024 // kb
25 var kb = new KnowledgeBase(body);
26
27 // sidebar
Rushabh Mehtaa11bc072012-04-19 17:48:57 +053028 this.sidebar = new wn.widgets.PageSidebar($(wrapper).find('.questions-tags').get(0), {
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053029 sections: [
30 {
31 title: 'Top Tags',
32 render: function(body) {
Rushabh Mehtab73fa492012-02-24 15:07:39 +053033 new wn.widgets.TagCloud(body, 'Question', function(tag)
34 { kb.set_tag_filter(tag) });
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053035 }
36 }
37 ]
38 });
39 set_title('Knowledge Base');
40}
41
42// knowledge base object
43// has a box for search or ask a question
44// and list of top rated search results
45//
46function KnowledgeBase(w) {
47 var me = this;
48 this.sort_by = 'modified';
49 this.tag_filter_dict = {};
50
51 this.make_search_bar = function() {
Rushabh Mehtab73fa492012-02-24 15:07:39 +053052 this.search = $(w).find('.kb-search-wrapper textarea').get(0);
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053053
Rushabh Mehtab73fa492012-02-24 15:07:39 +053054 $(w).find('.btn.search').click(function() {
55 me.run();
56 })
57 $(w).find('.btn.ask').click(function() {
58 me.ask();
59 })
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053060 }
61
62 // ask a new question
63 this.ask = function() {
64 if(this.search.value==$(this.search).attr('default_text')) {
65 msgprint('Please enter some text'); return;
66 }
Rushabh Mehtaedab76b2012-04-18 16:55:43 +053067 this.add_question([]);
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053068 }
69
70 // suggest a few users who can answer
71 this.suggest = function() {
72 this.dialog = new wn.widgets.Dialog({
73 title: 'Suggest a users',
74 width: 400,
75 fields: [
76 {fieldtype:'HTML', options:'Optional: Suggest a few users who can help you answer this question<br>'},
77 {fieldtype:'Link', fieldname:'profile1', label:'1st User',options:'Profile'},
78 {fieldtype:'Link', fieldname:'profile2', label:'2nd User',options:'Profile'},
79 {fieldtype:'Link', fieldname:'profile3', label:'3rd User',options:'Profile'},
80 {fieldtype:'Button', fieldname:'ask', label:'Add the Question'}
81 ]
82 });
83 this.dialog.fields_dict.ask.input.onclick = function() {
84 me.dialog.hide();
85 me.add_question(values(me.dialog.get_values()));
86 }
87 this.dialog.show();
88 }
89
90 // add a new question to the database
91 this.add_question = function(suggest_list) {
Anand Doshi37df8ab2012-04-20 11:17:10 +053092 $c_page('utilities', 'questions', 'add_question', {
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053093 question: this.search.value,
94 suggest: suggest_list
95 }, function(r,rt) {
96 $(me.search).val('').blur();
97 me.run();
98 })
99 }
100
101 // where tags that filter will be displayed
102 this.make_tag_filter_area = function() {
103 this.tag_filters = $a(w, 'div', 'kb-tag-filter-area');
104 $a(this.tag_filters,'span','',{marginRight:'4px',color:'#442'}, '<i>Showing for:</i>');
105 this.tag_area = $a(this.tag_filters, 'span');
106 }
107
108 // make a list of questions
109 this.make_list = function() {
110 this.make_tag_filter_area();
111 this.list_area = $a(w, 'div', '', {marginRight:'13px'})
112 this.no_result = $a(w, 'div','help_box',{display:'none'},'No questions asked yet! Be the first one to ask')
113
Rushabh Mehtaf81a64e2012-03-07 18:19:41 +0530114 this.list = new wn.ui.Listing({
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530115 parent: this.list_area,
116 no_results_message: 'No questions found. Ask a new question!',
Rushabh Mehtaa11bc072012-04-19 17:48:57 +0530117 appframe: wn.pages.questions.appframe,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530118 as_dict: 1,
119 get_query: function() {
120
121 // filter by search string
122 var v = me.search.value==$(me.search).attr('default_text') ? '' : me.search.value;
123 cond = v ? (' and t1.question like "%'+v+'%"') : '';
124
125 // filter by tags
126 if(me.tag_filter_dict) {
127 for(f in me.tag_filter_dict) {
128 cond += ' and t1.`_user_tags` like "%' + f + '%"'
129 }
130 }
Rushabh Mehta81c8ec22012-04-18 17:14:33 +0530131 return repl('select t1.name, t1.owner, t1.question, t1.modified, t1._user_tags, '
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530132 +'t1._users_voted, t2.first_name, t2.last_name '
133 +'from tabQuestion t1, tabProfile t2 '
134 +'where t1.docstatus!=2 '
135 +'and t1.owner = t2.name'
136 +'%(cond)s order by t1.modified desc', {user:user, cond: cond})
137 },
138 render_row: function(parent, data, listing) {
139 new KBQuestion(parent, data, me);
140 }
141 });
142
143 this.list.run();
144
145 }
146
147 // add a tag filter to the search in the
148 // main page
149 this.set_tag_filter = function(tag) {
150
151 // check if exists
152 if(in_list(keys(me.tag_filter_dict), tag.label)) return;
153
154 // create a tag in filters
155 var filter_tag = new SingleTag({
156 parent: me.tag_area,
157 label: tag.label,
158 dt: 'Question',
159 color: tag.color
160 });
161
162 // remove tag from filters
163 filter_tag.remove = function(tag_remove) {
164 $(tag_remove.body).fadeOut();
165 delete me.tag_filter_dict[tag_remove.label];
166
167 // hide everything?
168 if(!keys(me.tag_filter_dict).length) {
169 $(me.tag_filters).slideUp(); // hide
170 }
171
172 // run
173 me.run();
174 }
175
176 // add to dict
177 me.tag_filter_dict[tag.label] = filter_tag;
178 $ds(me.tag_filters);
179
180 // run
181 me.run();
182 }
183 this.run = function() {
184 this.list.run();
185 }
186
187 this.make_search_bar();
188 this.make_list();
189
190}
191
192// single kb question
193// "question
194// points | tag list"
195
196KBQuestion = function(parent, det, kb) {
197
198 this.make = function() {
199 this.wrapper = $a(parent, 'div', 'kb-question-wrapper');
200 this.q_area = $a($a(this.wrapper, 'div'), 'h3', 'kb-questions link_type', {display:'inline', textDecoration:'none'}, det.question);
201
202 this.q_area.onclick = function() {
203 var q = this;
204 window.location.href = '#!question-view/' + q.id;
205 //loadpage('question-view', function() { pscript.question_view(q.id, q.txt) })
206 }
207
208 this.q_area.id = det.name; this.q_area.txt = det.question;
209
210 new KBItemToolbar({
211 parent: this.wrapper,
212 det: det,
213 with_tags: 1,
214 doctype: 'Question'
215 }, kb)
216
217 }
218
219
220 this.make()
221}
222
Anand Doshi37df8ab2012-04-20 11:17:10 +0530223wn.require('erpnext/utilities/page/kb_common/kb_common.js');