|
| 1 | +var apiHelper = require("@/app/modules/apiHelper"); |
| 2 | +var PopupList = require("@/app/modules/uikit/popupList"); |
| 3 | +var utils = require("@/common/utils"); |
| 4 | +var channel = require("@/app/modules/channel"); |
| 5 | +var tpl = require("./template/indexTpl.html"); |
| 6 | + |
| 7 | +module.exports = function(){ |
| 8 | + // 二级菜单实例 |
| 9 | + var menuInsArr = []; |
| 10 | + var menuArr = []; |
| 11 | + var container = document.querySelector(".em-widget-tip"); |
| 12 | + container.innerHTML = ""; |
| 13 | + utils.addClass(container, "new"); |
| 14 | + |
| 15 | + apiHelper.getNotice() |
| 16 | + .then(function(notice){ |
| 17 | + if(!notice.enabled) return; |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + // test |
| 22 | + // notice.content = [ |
| 23 | + // { |
| 24 | + // name: "菜单", |
| 25 | + // sub_button: [ |
| 26 | + // { |
| 27 | + // type: "view", |
| 28 | + // name: "搜索2666", |
| 29 | + // url: "http://www.soso.com/" |
| 30 | + // } |
| 31 | + // ] |
| 32 | + // }, |
| 33 | + // { |
| 34 | + // name: "222", |
| 35 | + // sub_button: [ |
| 36 | + // { |
| 37 | + // type: "media_id", |
| 38 | + // name: "111", |
| 39 | + // media_id: "75cffa4b-e462-40e8-a517-0ff807db29a6" |
| 40 | + // }, |
| 41 | + // { |
| 42 | + // type: "media_id", |
| 43 | + // name: "香格里拉", |
| 44 | + // media_id: "4150c891-9917-4482-909c-ab7c9954110a" |
| 45 | + // } |
| 46 | + // ] |
| 47 | + // }, |
| 48 | + // { |
| 49 | + // type: "media_id", |
| 50 | + // name: "333", |
| 51 | + // media_id: "75cffa4b-e462-40e8-a517-0ff807db29a6" |
| 52 | + // } |
| 53 | + // ]; |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + menuArr = notice.content; |
| 59 | + container.innerHTML = _.template(tpl)({ |
| 60 | + menu: menuArr, |
| 61 | + }); |
| 62 | + createMenu(); |
| 63 | + utils.live(".tip-btn", "click", onMenuClick, container); |
| 64 | + }); |
| 65 | + |
| 66 | + // 菜单点击 |
| 67 | + function onMenuClick(e){ |
| 68 | + var menuId; |
| 69 | + var menuDat; |
| 70 | + var target = e.srcElement || e.target; |
| 71 | + var targetBounding; |
| 72 | + if(utils.hasClass(target.parentNode, "tip-btn")){ |
| 73 | + target = target.parentNode; |
| 74 | + } |
| 75 | + menuId = target.getAttribute("menuId"); |
| 76 | + menuDat = menuArr[menuId]; |
| 77 | + |
| 78 | + targetBounding = target.getBoundingClientRect(); |
| 79 | + handleMenuClick(menuDat, menuId, { |
| 80 | + top: targetBounding.top + 48, |
| 81 | + left: targetBounding.left + (target.clientWidth * 0.1), |
| 82 | + width: target.clientWidth * 0.8, |
| 83 | + }); |
| 84 | + // ie8 |
| 85 | + // 使得 popupList 可以自动关闭 |
| 86 | + e.stopPropagation(); |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + function handleMenuClick(menuDat, menuId, pos){ |
| 91 | + // 有子菜单 |
| 92 | + if(menuDat.sub_button){ |
| 93 | + popupList(menuId, pos || { |
| 94 | + left: 0, |
| 95 | + top: 0, |
| 96 | + width: 0, |
| 97 | + }); |
| 98 | + } |
| 99 | + // 无子菜单 |
| 100 | + else{ |
| 101 | + hideAllSubMenu(); |
| 102 | + fireMsg(menuDat); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + function fireMsg(dat){ |
| 107 | + switch(dat.type){ |
| 108 | + // 发送 article |
| 109 | + case "media_id": |
| 110 | + fireArticle(dat); |
| 111 | + break; |
| 112 | + // 发送 url |
| 113 | + case "view": |
| 114 | + fireUrl(dat); |
| 115 | + break; |
| 116 | + // 发送 text |
| 117 | + case "txt": |
| 118 | + fireText(dat); |
| 119 | + break; |
| 120 | + default: |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + function fireArticle(menuDat){ |
| 126 | + apiHelper.getArticleJson({ |
| 127 | + media_id: menuDat.media_id |
| 128 | + }).then(function(articles){ |
| 129 | + articles = _.map(articles, function(article){ |
| 130 | + return { |
| 131 | + title: article.title, |
| 132 | + digest: article.digest, |
| 133 | + description: article.digest, |
| 134 | + // url: "/v1/webimplugin/tenants/" + article.tenantId + "/robot/article/html/" + article.articleId, |
| 135 | + url: "/v1/Tenants/" + article.tenantId + "/robot/article/html/" + article.articleId, |
| 136 | + thumbUrl: "/v1/Tenant/" + article.tenantId + "/MediaFiles/" + article.thumb_media_id, |
| 137 | + picurl: "/v1/Tenant/" + article.tenantId + "/MediaFiles/" + article.thumb_media_id, |
| 138 | + prop: article.prop, |
| 139 | + }; |
| 140 | + }); |
| 141 | + channel.handleMessage( |
| 142 | + { |
| 143 | + ext: { |
| 144 | + msgtype: { |
| 145 | + articles: articles, |
| 146 | + } |
| 147 | + }, |
| 148 | + }, |
| 149 | + { |
| 150 | + type: "article", |
| 151 | + noPrompt: true |
| 152 | + } |
| 153 | + ); |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + function fireText(menuDat){ |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + function fireUrl(menuDat){ |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + // 每个 btn 创建一个菜单 |
| 166 | + function createMenu(){ |
| 167 | + _.each(menuArr, function(menuDat, menuId){ |
| 168 | + menuInsArr[menuId] = new PopupList({ |
| 169 | + items: menuDat.sub_button, |
| 170 | + reportClick: handleMenuClick, |
| 171 | + }); |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + // 弹出菜单 |
| 176 | + function popupList(menuId, pos){ |
| 177 | + hideAllSubMenu(); |
| 178 | + menuInsArr[menuId].show(pos); |
| 179 | + } |
| 180 | + |
| 181 | + function hideAllSubMenu(){ |
| 182 | + _.each(menuInsArr, function(menuIns){ |
| 183 | + menuIns.hide(); |
| 184 | + }); |
| 185 | + } |
| 186 | + |
| 187 | + return { |
| 188 | + show: function(){ |
| 189 | + container.style.display = "block"; |
| 190 | + }, |
| 191 | + hide: function(){ |
| 192 | + container.style.display = "none"; |
| 193 | + } |
| 194 | + }; |
| 195 | +}; |
0 commit comments