Faris Ansari | 709a4a3 | 2018-08-01 14:09:07 +0530 | [diff] [blame] | 1 | function get_empty_state(message, action) { |
| 2 | return `<div class="empty-state flex align-center flex-column justify-center"> |
| 3 | <p class="text-muted">${message}</p> |
| 4 | ${action ? `<p>${action}</p>`: ''} |
| 5 | </div>`; |
| 6 | } |
| 7 | |
Prateeksha Singh | 318cca8 | 2018-08-07 12:41:21 +0530 | [diff] [blame^] | 8 | function get_item_card_container_html(items, title='', get_item_html = get_item_card_html, action='') { |
Faris Ansari | 709a4a3 | 2018-08-01 14:09:07 +0530 | [diff] [blame] | 9 | const items_html = (items || []).map(item => get_item_html(item)).join(''); |
| 10 | const title_html = title |
Prateeksha Singh | 318cca8 | 2018-08-07 12:41:21 +0530 | [diff] [blame^] | 11 | ? `<div class="hub-card-container-header col-sm-12 margin-bottom flex"> |
Prateeksha Singh | 658d1bb | 2018-08-06 19:13:10 +0530 | [diff] [blame] | 12 | <h4>${title}</h4> |
Prateeksha Singh | 318cca8 | 2018-08-07 12:41:21 +0530 | [diff] [blame^] | 13 | ${action} |
Faris Ansari | 709a4a3 | 2018-08-01 14:09:07 +0530 | [diff] [blame] | 14 | </div>` |
| 15 | : ''; |
| 16 | |
| 17 | const html = `<div class="row hub-card-container"> |
| 18 | ${title_html} |
| 19 | ${items_html} |
| 20 | </div>`; |
| 21 | |
| 22 | return html; |
| 23 | } |
| 24 | |
| 25 | function get_item_card_html(item) { |
| 26 | const item_name = item.item_name || item.name; |
| 27 | const title = strip_html(item_name); |
| 28 | const img_url = item.image; |
| 29 | const company_name = item.company; |
| 30 | |
| 31 | // Subtitle |
| 32 | let subtitle = [comment_when(item.creation)]; |
| 33 | const rating = item.average_rating; |
| 34 | if (rating > 0) { |
| 35 | subtitle.push(rating + `<i class='fa fa-fw fa-star-o'></i>`) |
| 36 | } |
| 37 | subtitle.push(company_name); |
| 38 | |
| 39 | let dot_spacer = '<span aria-hidden="true"> · </span>'; |
| 40 | subtitle = subtitle.join(dot_spacer); |
| 41 | |
| 42 | const item_html = ` |
| 43 | <div class="col-md-3 col-sm-4 col-xs-6"> |
| 44 | <div class="hub-card" data-route="marketplace/item/${item.hub_item_code}"> |
| 45 | <div class="hub-card-header"> |
| 46 | <div class="hub-card-title ellipsis bold">${title}</div> |
| 47 | <div class="hub-card-subtitle ellipsis text-muted">${subtitle}</div> |
| 48 | </div> |
| 49 | <div class="hub-card-body"> |
| 50 | <img class="hub-card-image" src="${img_url}" /> |
| 51 | <div class="overlay hub-card-overlay"></div> |
| 52 | </div> |
| 53 | </div> |
| 54 | </div> |
| 55 | `; |
| 56 | |
| 57 | return item_html; |
| 58 | } |
| 59 | |
| 60 | function get_local_item_card_html(item) { |
| 61 | const item_name = item.item_name || item.name; |
| 62 | const title = strip_html(item_name); |
| 63 | const img_url = item.image; |
| 64 | const company_name = item.company; |
| 65 | |
| 66 | const is_active = item.publish_in_hub; |
| 67 | const id = item.hub_item_code || item.item_code; |
| 68 | |
| 69 | // Subtitle |
| 70 | let subtitle = [comment_when(item.creation)]; |
| 71 | const rating = item.average_rating; |
| 72 | if (rating > 0) { |
| 73 | subtitle.push(rating + `<i class='fa fa-fw fa-star-o'></i>`) |
| 74 | } |
| 75 | subtitle.push(company_name); |
| 76 | |
| 77 | let dot_spacer = '<span aria-hidden="true"> · </span>'; |
| 78 | subtitle = subtitle.join(dot_spacer); |
| 79 | |
| 80 | const edit_item_button = `<div class="hub-card-overlay-button" style="right: 15px; bottom: 15px;" data-route="Form/Item/${item.item_name}"> |
| 81 | <button class="btn btn-default zoom-view"> |
| 82 | <i class="octicon octicon-pencil text-muted"></i> |
| 83 | </button> |
| 84 | </div>`; |
| 85 | |
| 86 | const item_html = ` |
| 87 | <div class="col-md-3 col-sm-4 col-xs-6"> |
| 88 | <div class="hub-card is-local ${is_active ? 'active' : ''}" data-id="${id}"> |
| 89 | <div class="hub-card-header"> |
| 90 | <div class="hub-card-title ellipsis bold">${title}</div> |
| 91 | <div class="hub-card-subtitle ellipsis text-muted">${subtitle}</div> |
| 92 | <i class="octicon octicon-check text-success"></i> |
| 93 | </div> |
| 94 | <div class="hub-card-body"> |
| 95 | <img class="hub-card-image" src="${img_url}" /> |
| 96 | <div class="hub-card-overlay"> |
| 97 | <div class="hub-card-overlay-body"> |
| 98 | ${edit_item_button} |
| 99 | </div> |
| 100 | </div> |
| 101 | </div> |
| 102 | </div> |
| 103 | </div> |
| 104 | `; |
| 105 | |
| 106 | return item_html; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | function get_rating_html(rating) { |
| 111 | let rating_html = ``; |
| 112 | for (var i = 0; i < 5; i++) { |
| 113 | let star_class = 'fa-star'; |
| 114 | if (i >= rating) star_class = 'fa-star-o'; |
| 115 | rating_html += `<i class='fa fa-fw ${star_class} star-icon' data-index=${i}></i>`; |
| 116 | } |
| 117 | return rating_html; |
| 118 | } |
| 119 | |
| 120 | function make_search_bar({wrapper, on_search, placeholder = __('Search for anything')}) { |
| 121 | const $search = $(` |
| 122 | <div class="hub-search-container"> |
| 123 | <input type="text" class="form-control" placeholder="${placeholder}"> |
| 124 | </div>` |
| 125 | ); |
| 126 | wrapper.append($search); |
| 127 | const $search_input = $search.find('input'); |
| 128 | |
| 129 | $search_input.on('keydown', frappe.utils.debounce((e) => { |
| 130 | if (e.which === frappe.ui.keyCode.ENTER) { |
| 131 | const search_value = $search_input.val(); |
| 132 | on_search(search_value); |
| 133 | } |
| 134 | }, 300)); |
| 135 | } |
| 136 | |
| 137 | export { |
| 138 | get_empty_state, |
| 139 | get_item_card_container_html, |
| 140 | get_item_card_html, |
| 141 | get_local_item_card_html, |
| 142 | get_rating_html, |
| 143 | make_search_bar, |
Prateeksha Singh | 658d1bb | 2018-08-06 19:13:10 +0530 | [diff] [blame] | 144 | } |