Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 1 | pscript.onload_questions = function() { |
| 2 | var w = page_body.pages['questions']; |
| 3 | |
| 4 | var tab = make_table(w, 1, 2, '100%', ['75%', '25%'], {}); |
| 5 | var body = $a($td(tab,0,0),'div','layout_wrapper'); |
| 6 | |
| 7 | new PageHeader(body, 'Knowledge Base'); |
| 8 | |
| 9 | // kb |
| 10 | var kb = new KnowledgeBase(body); |
| 11 | |
| 12 | // sidebar |
| 13 | $y($td(tab, 0, 1), {paddingTop:'53px'}); |
| 14 | this.sidebar = new wn.widgets.PageSidebar($td(tab, 0, 1), { |
| 15 | sections: [ |
| 16 | { |
| 17 | title: 'Top Tags', |
| 18 | render: function(body) { |
| 19 | new wn.widgets.TagCloud(body, 'Question', function(tag) { kb.set_tag_filter(tag) }); |
| 20 | } |
| 21 | } |
| 22 | ] |
| 23 | }); |
| 24 | set_title('Knowledge Base'); |
| 25 | } |
| 26 | |
| 27 | // knowledge base object |
| 28 | // has a box for search or ask a question |
| 29 | // and list of top rated search results |
| 30 | // |
| 31 | function KnowledgeBase(w) { |
| 32 | var me = this; |
| 33 | this.sort_by = 'modified'; |
| 34 | this.tag_filter_dict = {}; |
| 35 | |
| 36 | this.make_search_bar = function() { |
| 37 | this.search = $a($a(w,'div','kb-search-wrapper'), 'textarea'); |
| 38 | |
| 39 | $(this.search).add_default_text('Enter keywords or a new Question'); |
| 40 | |
| 41 | var div = $a(w,'div','kb-btn-wrapper'); |
| 42 | $btn(div, 'Search', function() { me.run() }, {fontSize:'14px'}); |
| 43 | $btn(div, 'Ask', function() { me.ask() }, {fontSize:'14px'}); |
| 44 | } |
| 45 | |
| 46 | // ask a new question |
| 47 | this.ask = function() { |
| 48 | if(this.search.value==$(this.search).attr('default_text')) { |
| 49 | msgprint('Please enter some text'); return; |
| 50 | } |
| 51 | this.suggest(); |
| 52 | } |
| 53 | |
| 54 | // suggest a few users who can answer |
| 55 | this.suggest = function() { |
| 56 | this.dialog = new wn.widgets.Dialog({ |
| 57 | title: 'Suggest a users', |
| 58 | width: 400, |
| 59 | fields: [ |
| 60 | {fieldtype:'HTML', options:'Optional: Suggest a few users who can help you answer this question<br>'}, |
| 61 | {fieldtype:'Link', fieldname:'profile1', label:'1st User',options:'Profile'}, |
| 62 | {fieldtype:'Link', fieldname:'profile2', label:'2nd User',options:'Profile'}, |
| 63 | {fieldtype:'Link', fieldname:'profile3', label:'3rd User',options:'Profile'}, |
| 64 | {fieldtype:'Button', fieldname:'ask', label:'Add the Question'} |
| 65 | ] |
| 66 | }); |
| 67 | this.dialog.fields_dict.ask.input.onclick = function() { |
| 68 | me.dialog.hide(); |
| 69 | me.add_question(values(me.dialog.get_values())); |
| 70 | } |
| 71 | this.dialog.show(); |
| 72 | } |
| 73 | |
| 74 | // add a new question to the database |
| 75 | this.add_question = function(suggest_list) { |
| 76 | $c_page('knowledge_base', 'questions', 'add_question', { |
| 77 | question: this.search.value, |
| 78 | suggest: suggest_list |
| 79 | }, function(r,rt) { |
| 80 | $(me.search).val('').blur(); |
| 81 | me.run(); |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | // where tags that filter will be displayed |
| 86 | this.make_tag_filter_area = function() { |
| 87 | this.tag_filters = $a(w, 'div', 'kb-tag-filter-area'); |
| 88 | $a(this.tag_filters,'span','',{marginRight:'4px',color:'#442'}, '<i>Showing for:</i>'); |
| 89 | this.tag_area = $a(this.tag_filters, 'span'); |
| 90 | } |
| 91 | |
| 92 | // make a list of questions |
| 93 | this.make_list = function() { |
| 94 | this.make_tag_filter_area(); |
| 95 | this.list_area = $a(w, 'div', '', {marginRight:'13px'}) |
| 96 | this.no_result = $a(w, 'div','help_box',{display:'none'},'No questions asked yet! Be the first one to ask') |
| 97 | |
| 98 | this.list = new wn.widgets.Listing({ |
| 99 | parent: this.list_area, |
| 100 | no_results_message: 'No questions found. Ask a new question!', |
| 101 | as_dict: 1, |
| 102 | get_query: function() { |
| 103 | |
| 104 | // filter by search string |
| 105 | var v = me.search.value==$(me.search).attr('default_text') ? '' : me.search.value; |
| 106 | cond = v ? (' and t1.question like "%'+v+'%"') : ''; |
| 107 | |
| 108 | // filter by tags |
| 109 | if(me.tag_filter_dict) { |
| 110 | for(f in me.tag_filter_dict) { |
| 111 | cond += ' and t1.`_user_tags` like "%' + f + '%"' |
| 112 | } |
| 113 | } |
| 114 | return repl('select t1.name, t1.owner, t1.question, t1.points, t1.modified, t1._user_tags, ' |
| 115 | +'t1._users_voted, t2.first_name, t2.last_name ' |
| 116 | +'from tabQuestion t1, tabProfile t2 ' |
| 117 | +'where t1.docstatus!=2 ' |
| 118 | +'and t1.owner = t2.name' |
| 119 | +'%(cond)s order by t1.modified desc', {user:user, cond: cond}) |
| 120 | }, |
| 121 | render_row: function(parent, data, listing) { |
| 122 | new KBQuestion(parent, data, me); |
| 123 | } |
| 124 | }); |
| 125 | |
| 126 | this.list.run(); |
| 127 | |
| 128 | } |
| 129 | |
| 130 | // add a tag filter to the search in the |
| 131 | // main page |
| 132 | this.set_tag_filter = function(tag) { |
| 133 | |
| 134 | // check if exists |
| 135 | if(in_list(keys(me.tag_filter_dict), tag.label)) return; |
| 136 | |
| 137 | // create a tag in filters |
| 138 | var filter_tag = new SingleTag({ |
| 139 | parent: me.tag_area, |
| 140 | label: tag.label, |
| 141 | dt: 'Question', |
| 142 | color: tag.color |
| 143 | }); |
| 144 | |
| 145 | // remove tag from filters |
| 146 | filter_tag.remove = function(tag_remove) { |
| 147 | $(tag_remove.body).fadeOut(); |
| 148 | delete me.tag_filter_dict[tag_remove.label]; |
| 149 | |
| 150 | // hide everything? |
| 151 | if(!keys(me.tag_filter_dict).length) { |
| 152 | $(me.tag_filters).slideUp(); // hide |
| 153 | } |
| 154 | |
| 155 | // run |
| 156 | me.run(); |
| 157 | } |
| 158 | |
| 159 | // add to dict |
| 160 | me.tag_filter_dict[tag.label] = filter_tag; |
| 161 | $ds(me.tag_filters); |
| 162 | |
| 163 | // run |
| 164 | me.run(); |
| 165 | } |
| 166 | this.run = function() { |
| 167 | this.list.run(); |
| 168 | } |
| 169 | |
| 170 | this.make_search_bar(); |
| 171 | this.make_list(); |
| 172 | |
| 173 | } |
| 174 | |
| 175 | // single kb question |
| 176 | // "question |
| 177 | // points | tag list" |
| 178 | |
| 179 | KBQuestion = function(parent, det, kb) { |
| 180 | |
| 181 | this.make = function() { |
| 182 | this.wrapper = $a(parent, 'div', 'kb-question-wrapper'); |
| 183 | this.q_area = $a($a(this.wrapper, 'div'), 'h3', 'kb-questions link_type', {display:'inline', textDecoration:'none'}, det.question); |
| 184 | |
| 185 | this.q_area.onclick = function() { |
| 186 | var q = this; |
| 187 | window.location.href = '#!question-view/' + q.id; |
| 188 | //loadpage('question-view', function() { pscript.question_view(q.id, q.txt) }) |
| 189 | } |
| 190 | |
| 191 | this.q_area.id = det.name; this.q_area.txt = det.question; |
| 192 | |
| 193 | new KBItemToolbar({ |
| 194 | parent: this.wrapper, |
| 195 | det: det, |
| 196 | with_tags: 1, |
| 197 | doctype: 'Question' |
| 198 | }, kb) |
| 199 | |
| 200 | } |
| 201 | |
| 202 | |
| 203 | this.make() |
| 204 | } |
| 205 | |
| 206 | $import(knowledge_base.kb_common); |