Elcarousel.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. o2.xDesktop.requireApp("process.Xform", "$Elinput", null, false);
  2. /** @class Elcarousel 基于Element UI的输入框组件。
  3. * @example
  4. * //可以在脚本中获取该组件
  5. * //方法1:
  6. * var input = this.form.get("name"); //获取组件
  7. * //方法2
  8. * var input = this.target; //在组件事件脚本中获取
  9. * @extends MWF.xApplication.process.Xform.$Module
  10. * @o2category FormComponents
  11. * @o2range {Process|CMS|Portal}
  12. * @hideconstructor
  13. * @see {@link https://element.eleme.cn/#/zh-CN/component/tree|Element UI Tree 树形控件}
  14. */
  15. MWF.xApplication.process.Xform.Elcarousel = MWF.APPElcarousel = new Class(
  16. /** @lends o2.xApplication.process.Xform.Elcarousel# */
  17. {
  18. Implements: [Events],
  19. Extends: MWF.APP$Elinput,
  20. options: {
  21. "moduleEvents": ["load", "queryLoad", "postLoad"],
  22. /**
  23. * 幻灯片切换时触发。this.event[0]为目前激活的幻灯片的索引;
  24. * @event MWF.xApplication.process.Xform.Elcarousel#change
  25. * @see {@link https://element.eleme.cn/#/zh-CN/component/carousel|幻灯片组件的Events章节}
  26. */
  27. "elEvents": ["change"]
  28. },
  29. _loadNode: function(){
  30. this._loadNodeEdit();
  31. },
  32. _loadNodeEdit: function(){
  33. this._createElementHtml().then(function(html){
  34. var id = (this.json.id.indexOf("..")!==-1) ? this.json.id.replace(/\.\./g, "_") : this.json.id;
  35. this.json["$id"] = (id.indexOf("-")!==-1) ? id.replace(/-/g, "_") : id;
  36. this.node.appendHTML(html, "before");
  37. var input = this.node.getPrevious();
  38. this.node.destroy();
  39. this.node = input;
  40. this.node.set({
  41. "id": this.json.id,
  42. "MWFType": this.json.type
  43. });
  44. this.node.addClass("o2_vue");
  45. this._createVueApp();
  46. }.bind(this));
  47. },
  48. _appendVueData: function(){
  49. if (!this.json.heightText) this.json.heightText = this.json.height+"px";
  50. if( !this.json.initialIndex )this.json.initialIndex = 0;
  51. if( !this.json.carouselType )this.json.carouselType = "";
  52. if( !this.json.trigger )this.json.trigger = "";
  53. // if (this.json.dataType === "script"){
  54. // this.json.data = this.form.Macro.exec(((this.json.dataScript) ? this.json.dataScript.code : ""), this);
  55. // }else{
  56. // this.json.data = this.json.dataJson;
  57. // }
  58. // this.parseData();
  59. },
  60. _createElementHtml: function() {
  61. return this.parseItem().then(function ( data ) {
  62. var itemHtml = data[0];
  63. var html = "<el-carousel";
  64. html += " :height=\"heightText\"";
  65. html += " :initial-index=initialIndex";
  66. html += " :trigger=\"trigger\"";
  67. html += " :autoplay=\"autoplay\"";
  68. html += " :interval=interval";
  69. html += " :indicator-position=\"indicatorPosition\"";
  70. html += " :arrow=\"arrow\"";
  71. html += " :type=\"carouselType\"";
  72. html += " :loop=\"loop\"";
  73. html += " :direction=\"direction\"";
  74. this.options.elEvents.forEach(function(k){
  75. html += " @"+k+"=\"$loadElEvent_"+k.camelCase()+"\"";
  76. });
  77. if (this.json.elProperties){
  78. Object.keys(this.json.elProperties).forEach(function(k){
  79. if (this.json.elProperties[k]) html += " "+k+"=\""+this.json.elProperties[k]+"\"";
  80. }, this);
  81. }
  82. if (this.json.elStyles) html += " :style=\"elStyles\"";
  83. html += ">";
  84. if (this.json.vueSlot){
  85. html += this.json.vueSlot;
  86. }else{
  87. html += itemHtml;
  88. }
  89. html += "</el-carousel>";
  90. return html;
  91. }.bind(this))
  92. },
  93. parseItem: function(){
  94. return Promise.all([
  95. this.parseItemHtml(),
  96. this.parseItemData()
  97. ]);
  98. },
  99. parseItemData: function(){
  100. if( this.json.dataType === "hotpicture" ) {
  101. var obj;
  102. if (this.json.filterScript && this.json.filterScript.code) {
  103. obj = this.form.Macro.exec(((this.json.filterScript) ? this.json.filterScript.code : ""), this);
  104. }
  105. if (obj) obj = {};
  106. var action = o2.Actions.load("x_hotpic_assemble_control").HotPictureInfoAction.listForPage(this.json.page, this.json.count, obj);
  107. return action.then(function (json) {
  108. // var lineHeight = this.json.height ? ( "line-height:"+this.json.height + "px;") : "";
  109. this.json.items = json.data;
  110. return this.json.items;
  111. }.bind(this))
  112. }else if( this.json.dataType === "source" ){
  113. return this._getO2Source().then(function (json) {
  114. var paths = ( this.json.dataItemPath || "data" ).split(".");
  115. var d = json;
  116. for(var i=0; i<paths.length; i++){
  117. if( d && d[paths[i]] )d = d[paths[i]];
  118. }
  119. if( !d )d = [];
  120. if( o2.typeOf(d) !== "array" )d = [ d ];
  121. this.json.items = d;
  122. return this.json.items;
  123. }.bind(this))
  124. }else if( this.json.dataType === "script" ){
  125. var data;
  126. if (this.json.dataScript && this.json.dataScript.code) {
  127. data = this.form.Macro.exec(((this.json.dataScript) ? this.json.dataScript.code : ""), this);
  128. }
  129. if( !data ){
  130. return;
  131. }else if (typeof data.then !== 'function') {
  132. data = Promise.resolve(data);
  133. }
  134. return data.then(function ( d ) {
  135. var paths = ( this.json.scriptDataItemPath || "data" ).split(".");
  136. for(var i=0; i<paths.length; i++){
  137. if( d && d[paths[i]] )d = d[paths[i]];
  138. }
  139. if( !d )d = [];
  140. if( o2.typeOf(d) !== "array" )d = [ d ];
  141. this.json.items = d;
  142. return this.json.items;
  143. }.bind(this))
  144. }
  145. },
  146. parseItemHtml: function(data){
  147. var html = "";
  148. if( this.json.dataType === "hotpicture" ){
  149. // if( !this.json.contentType || this.json.contentType === "config" ){
  150. if( (!this.json.contentConfig || !this.json.contentConfig.length) ){
  151. html += "<el-carousel-item v-for=\"item in items\">";
  152. html += "<img :src=\"getImageSrc(item.picId)\" @click=\"clickHotpictureItem(item, $event)\" style=\"height: 100%; width:100%\"/>";
  153. html += "<div style=\"line-height:30px;height: 30px;width:100%;text-align: center;position: absolute;bottom:0px;left:0px;color:#fff;background: rgba(104, 104, 104, 0.5);\">{{item.title}}</div>";
  154. html += "</el-carousel-item>";
  155. return html;
  156. }else{
  157. return this.parseContentConfig()
  158. }
  159. // }
  160. }else{ //if( this.json.contentType === "config" ){
  161. return this.parseContentConfig()
  162. }
  163. },
  164. parseContentConfig: function(){
  165. var _self = this;
  166. var html = "";
  167. html += "<el-carousel-item v-for=\"item in items\">";
  168. this.configFun = {};
  169. this.json.contentConfig.each(function (config, i) {
  170. var srcFunName, textFunName, clickFunName, srcHtml = "", clickHtml = "", textHtml="";
  171. if( config.type === "img" && config.srcScript && config.srcScript.code) {
  172. srcFunName = "getImageSrc_" + i;
  173. srcHtml = " :src=\"" + srcFunName + "(item)\"";
  174. this.configFun[srcFunName] = function (item) {
  175. return _self.form.Macro.fire(this.srcScript.code, _self, item);
  176. }.bind(config);
  177. }
  178. if( config.clickScript && config.clickScript.code ){
  179. clickFunName = "click_"+i;
  180. clickHtml = " @click=\""+clickFunName+"(item, $event)\"";
  181. this.configFun[clickFunName] = function (item, ev) {
  182. return _self.form.Macro.fire(this.clickScript.code, _self, [item, ev]);
  183. }.bind(config);
  184. }
  185. if( config.type === "text" && config.textScript && config.textScript.code) {
  186. textFunName = "getText_" + i;
  187. textHtml = "{{"+textFunName+"(item)}}";
  188. this.configFun[textFunName] = function (item) {
  189. return _self.form.Macro.fire(this.textScript.code, _self, item);
  190. }.bind(config);
  191. }
  192. if( config.type === "img" ){
  193. html += "<img" + srcHtml + clickHtml +" style=\""+this.jsonToStyle(config.styles)+"\"/>";
  194. }else{
  195. html += "<div"+ clickHtml +" style=\""+this.jsonToStyle(config.styles)+"\">"+textHtml+"</div>";
  196. }
  197. }.bind(this));
  198. html += "</el-carousel-item>";
  199. return html;
  200. },
  201. _afterCreateVueExtend: function (app) {
  202. var _self = this;
  203. var flag = false;
  204. if( this.json.dataType === "hotpicture" ) {
  205. // if (this.json.contentType === "config") {
  206. if( (!this.json.contentConfig || !this.json.contentConfig.length) ) {
  207. app.methods.clickHotpictureItem = function (item, ev) {
  208. if (item.application === "CMS") {
  209. _self.openDocument(item.infoId, item.title)
  210. } else if (item.application === "BBS") {
  211. _self.openBBSDocument(item.infoId);
  212. }
  213. };
  214. app.methods.getImageSrc = function (picId) {
  215. return MWF.xDesktop.getImageSrc(picId);
  216. }
  217. flag = true;
  218. }
  219. // }
  220. }
  221. if( !flag && this.configFun ){
  222. Object.each(this.configFun, function (fun, key) {
  223. app.methods[key] = function (item, ev) {
  224. return fun(item, ev);
  225. }.bind(this)
  226. })
  227. }
  228. },
  229. jsonToStyle: function(styles){
  230. var style = "";
  231. for( var key in styles ){
  232. style += key+":"+styles[key]+";";
  233. }
  234. return style;
  235. },
  236. _getO2Source: function(){
  237. this._getO2Address();
  238. this._getO2Uri();
  239. return MWF.restful(this.json.httpMethod, this.uri, JSON.encode(this.body), function(json){
  240. return json;
  241. }.bind(this), true, true);
  242. },
  243. _getO2Address: function(){
  244. try {
  245. this.json.service = JSON.parse(this.json.contextRoot);
  246. }catch(e){
  247. this.json.service = {"root": this.json.contextRoot, "action":"", "method": "", "url": ""};
  248. }
  249. var addressObj = layout.serviceAddressList[this.json.service.root];
  250. var defaultPort = layout.config.app_protocol==='https' ? "443" : "80";
  251. if (addressObj){
  252. var appPort = addressObj.port || window.location.port;
  253. this.address = layout.config.app_protocol+"//"+(addressObj.host || window.location.hostname)+((!appPort || appPort.toString()===defaultPort) ? "" : ":"+appPort)+addressObj.context;
  254. }else{
  255. var host = layout.desktop.centerServer.host || window.location.hostname;
  256. var port = layout.desktop.centerServer.port || window.location.port;
  257. this.address = layout.config.app_protocol+"//"+host+((!port || port.toString()===defaultPort) ? "" : ":"+port)+"/"+this.json.service.root;
  258. }
  259. },
  260. _getO2Uri: function(){
  261. //var uri = this.json.path || this.json.selectPath;
  262. var uri = this.json.path;
  263. var pars = {};
  264. if (this.json.parameters){
  265. Object.each(this.json.parameters, function(v, key){
  266. if (uri.indexOf("{"+key+"}")!=-1){
  267. var reg = new RegExp("{"+key+"}", "g");
  268. uri = uri.replace(reg, encodeURIComponent((v && v.code) ? (this.form.Macro.exec(v.code, this) || "") : v));
  269. }else{
  270. pars[key] = v;
  271. }
  272. }.bind(this));
  273. }
  274. var data = null;
  275. if (this.json.requestBody){
  276. if (this.json.requestBody.code){
  277. data = this.form.Macro.exec(this.json.requestBody.code, this)
  278. }
  279. }
  280. if (this.json.httpMethod=="GET" || this.json.httpMethod=="OPTIONS" || this.json.httpMethod=="HEAD" || this.json.httpMethod=="DELETE"){
  281. var tag = "?";
  282. if (uri.indexOf("?")!=-1) tag = "&";
  283. Object.each(pars, function(v, k){
  284. var value = (v && v.code) ? (this.form.Macro.exec(v.code, this) || "") : v;
  285. uri = uri+tag+k+"="+value;
  286. }.bind(this));
  287. }else{
  288. Object.each(pars, function(v, k){
  289. if (!data) data = {};
  290. var value = (v && v.code) ? (this.form.Macro.exec(v.code, this) || "") : v;
  291. data[k] = value;
  292. }.bind(this));
  293. }
  294. this.body = data;
  295. this.uri = this.address+uri;
  296. },
  297. openDocument: function(id, title, options){
  298. var op = options || {};
  299. op.documentId = id;
  300. op.docTitle = title;
  301. op.appId = (op.appId) || ("cms.Document"+id);
  302. return layout.desktop.openApplication(null, "cms.Document", op);
  303. },
  304. openBBSDocument: function(id){
  305. var appId = "ForumDocument"+id;
  306. if (layout.desktop.apps && layout.desktop.apps[appId]){
  307. layout.desktop.apps[appId].setCurrent();
  308. }else {
  309. return layout.desktop.openApplication(null, "ForumDocument", {
  310. "id" : id,
  311. "appId": appId,
  312. "isEdited" : false,
  313. "isNew" : false
  314. });
  315. }
  316. }
  317. });