blob: e7e99b5323a0e678a744256dfc6cb34578156c64 [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
Rushabh Mehta0ad99542013-02-22 10:34:22 +053028 wn.model.with_doctype("Question", function() {
29 this.sidebar_stats = new wn.views.SidebarStats({
30 doctype: "Question",
31 stats: ["_user_tags"],
32 parent: $(wrapper).find('.questions-tags'),
33 set_filter: function(fieldname, label) {
34 kb.set_filter(fieldname, label);
35 //me.set_filter(fieldname, label);
Rushabh Mehta2fa2f712012-09-24 19:13:42 +053036 }
Rushabh Mehta0ad99542013-02-22 10:34:22 +053037 });
38 })
Rushabh Mehta2fa2f712012-09-24 19:13:42 +053039}
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
Rushabh Mehta0ad99542013-02-22 10:34:22 +0530138 this.set_filter = function(fieldname, label) {
139 this.set_tag_filter({label:label});
140 }
Rushabh Mehta2fa2f712012-09-24 19:13:42 +0530141 // add a tag filter to the search in the
142 // main page
143 this.set_tag_filter = function(tag) {
144
145 // check if exists
146 if(in_list(keys(me.tag_filter_dict), tag.label)) return;
147
148 // create a tag in filters
149 var filter_tag = new SingleTag({
150 parent: me.tag_area,
151 label: tag.label,
152 dt: 'Question',
153 color: tag.color
154 });
155
156 // remove tag from filters
157 filter_tag.remove = function(tag_remove) {
158 $(tag_remove.body).fadeOut();
159 delete me.tag_filter_dict[tag_remove.label];
160
161 // hide everything?
162 if(!keys(me.tag_filter_dict).length) {
163 $(me.tag_filters).slideUp(); // hide
164 }
165
166 // run
167 me.run();
168 }
169
170 // add to dict
171 me.tag_filter_dict[tag.label] = filter_tag;
172 $ds(me.tag_filters);
173
174 // run
175 me.run();
176 }
177 this.run = function() {
178 this.list.run();
179 }
180
181 this.make_search_bar();
182 this.make_list();
183
184}
185
186// single kb question
187// "question
188// points | tag list"
189
190KBQuestion = function(parent, det, kb) {
191
192 this.make = function() {
193 this.wrapper = $a(parent, 'div', 'kb-question-wrapper');
194 this.q_area = $a($a(this.wrapper, 'div'), 'h3',
195 'kb-questions link_type', {display:'inline', textDecoration:'none'}, det.question);
196 if(det.answers==0) {
197 $(this.q_area).addClass('un-answered')
198 }
199
200 this.q_area.onclick = function() {
201 var q = this;
202 window.location.href = '#!question-view/' + q.id;
203 //loadpage('question-view', function() { pscript.question_view(q.id, q.txt) })
204 }
205
206 this.q_area.id = det.name; this.q_area.txt = det.question;
207
208 new KBItemToolbar({
209 parent: this.wrapper,
210 det: det,
211 with_tags: 1,
212 doctype: 'Question'
213 }, kb)
214
215 }
216
217
218 this.make()
219}
220
221wn.require('app/js/kb_common.js');