blob: 22a70d8f4119d46c9cf9153de080f53e1b67d418 [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
Anand Doshic6fce632012-07-09 12:34:02 +053017pscript.onload_questions = function(wrapper) {
Rushabh Mehtab73fa492012-02-24 15:07:39 +053018 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,
Rushabh Mehtac7dbe292012-08-07 12:12:55 +0530118 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;
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530123 }
Rushabh Mehtac7dbe292012-08-07 12:12:55 +0530124 if(me.tag_filter_dict) {
125 args.tag_filters = keys(me.tag_filter_dict);
126 }
127 return args
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530128 },
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');
Rushabh Mehtac7dbe292012-08-07 12:12:55 +0530191 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 }
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530196
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
Anand Doshi951d8ec2012-05-10 14:19:11 +0530218wn.require('js/kb_common.js');