mpVueComponent.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. (function(global,factory){
  2. typeof define == 'function' && define.amd && define(['lodash'],factory);
  3. })(this,function(_){ 'use strict';
  4. //创建iframe框
  5. var $cf = {
  6. name:'CreateFrame',
  7. //父级元素
  8. parent:document.body,
  9. //iframe高度
  10. height:'100%',
  11. //iframe宽度
  12. width:'100%',
  13. itemList:{},
  14. count:1,
  15. frameClass:'m-frame-item',
  16. frameName:'m-frame-name',
  17. //删除frame
  18. destroy:function(frameName){
  19. $cf.itemList[frameName].$destroy();
  20. },
  21. //配置参数
  22. setConf:function(conf){
  23. conf.hieght && ($cf.hieght = conf.hieght);
  24. conf.wight && ($cf.wight = conf.wight);
  25. conf.parent &&($cf.parent = conf.parent);
  26. },
  27. //隐藏所有iframe
  28. hideAllFrame:function(){
  29. _($cf.itemList).each(function(v,k){
  30. v.isShow = false;
  31. })
  32. },
  33. //隐藏一个iframe
  34. hideFrame:function(frameName){
  35. $cf._displayItem(frameName,false);
  36. },
  37. //显示一个iframe
  38. showFrame:function(frameName){
  39. $cf._displayItem(frameName,true);
  40. },
  41. toggleFrame:function(frameName){
  42. $cf.hideAllFrame();
  43. $cf.showFrame(frameName);
  44. },
  45. _displayItem:function(frameName,type){
  46. _($cf.itemList).each(function(v,k){
  47. v.frameName == frameName && ($cf.itemList[k].isShow = type);
  48. });
  49. },
  50. _getFrameClass:function(){
  51. return $cf.frameClass + (++$cf.count);
  52. },
  53. _getDefaultName:function(){
  54. return $cf.frameName+$cf.count;
  55. },
  56. _setItem:function($e){
  57. $cf.itemList[$e.frameName] = $e
  58. },
  59. _destroyItem:function(frameName){
  60. $cf.itemList[frameName].$el.remove();
  61. delete $cf.itemList[frameName];
  62. },
  63. _factory: function (src,opt) {
  64. return {
  65. template: '<transition name="frameMove"><iframe v-show="isShow" :name="frameName" class="animated m-frame" :class="frameClass" :src="src" frameborder="false" scrolling="auto" width="100%" height="auto" allowtransparency="true" :style="{height:Height,width:Width}"></iframe></transition>',
  66. data: function () {
  67. return {
  68. frameClass: $cf._getFrameClass(),
  69. frameName: opt.frameName || $cf._getDefaultName(),
  70. isShow: true,
  71. src: src,
  72. height: opt.height || $cf.height,
  73. width: opt.width || $cf.width,
  74. load:false
  75. }
  76. },
  77. methods:{
  78. show:function(){
  79. this.isShow = true;
  80. },
  81. hide:function(){
  82. this.isShow = false;
  83. },
  84. destroy:function(){
  85. this.$destroy();
  86. }
  87. },
  88. computed: {
  89. Height: function () {
  90. return _.isNumber(this.height) ? this.height + 'px' : this.height;
  91. },
  92. Width: function () {
  93. return _.isNumber(this.width) ? this.width + 'px' : this.width;
  94. }
  95. },
  96. beforeDestroy:function(){
  97. $cf._destroyItem(this.frameName);
  98. }
  99. }
  100. }
  101. };
  102. var $CreateFrameInstall = function(Vue){
  103. var $m = function(src,opt){
  104. var loadFn = opt.loadFn,parent = opt.parent || $cf.parent,frame = Vue.extend($cf._factory(src,opt)),
  105. $vm = new frame(),tpl = $vm.$mount().$el;
  106. tpl.onload = function(){ ($vm.load = true) && loadFn && loadFn(this.contentWindow);};
  107. parent.appendChild(tpl) && $cf._setItem($vm);
  108. return $vm;
  109. };
  110. $m.hide = $cf.hideFrame;
  111. $m.show = $cf.showFrame;
  112. $m.destroy = $cf.destroy;
  113. $m.toggle = $cf.toggleFrame;
  114. $m.hideAll = $cf.hideAllFrame;
  115. $m.conf = $cf.setConf;
  116. Vue.prototype.$CreateFrame = $m;
  117. };
  118. return {
  119. install:function(Vue){
  120. $CreateFrameInstall(Vue);
  121. }
  122. }
  123. });