|
@@ -4,21 +4,31 @@
|
|
|
* Description:
|
|
|
*/
|
|
|
var boxContent = function(style) {
|
|
|
- this.top = (style && style.top) || '10px';
|
|
|
- this.left = (style && style.left) || '10px';
|
|
|
- this.width = (style && style.width) || '310px';
|
|
|
- this.height = (style && style.height) || '50px';
|
|
|
- this.isEditor = (style && style.isEditor) || false;
|
|
|
+ this.top = (style && style.top) || '10';
|
|
|
+ this.left = (style && style.left) || '10';
|
|
|
+ this.width = (style && style.width) || '310';
|
|
|
+ this.height = (style && style.height) || '50';
|
|
|
+ this.isEditor = false;
|
|
|
+
|
|
|
+ this.ox = 0;
|
|
|
+ this.oy = 0;
|
|
|
|
|
|
//组件id名称
|
|
|
this.domId = ('syq_box_' + Math.random()).replace('.','_');
|
|
|
-
|
|
|
//一个实例化的dom
|
|
|
this.vdom = null;
|
|
|
-
|
|
|
//组件当中的所有html
|
|
|
this.editorContent = null;
|
|
|
|
|
|
+ this.drag = false;
|
|
|
+ this.smove = false;
|
|
|
+ this.nmove = false;
|
|
|
+ this.wmove = false;
|
|
|
+ this.emove = false;
|
|
|
+ this.nwmove = false;
|
|
|
+ this.swmove = false;
|
|
|
+ this.semove = false;
|
|
|
+
|
|
|
};
|
|
|
|
|
|
boxContent.prototype.boxTpl = '<div class="line-n line resizable"><div class="circle"></div></div><div class="line-e line resizable"><div class="circle"></div></div><div class="line-s line resizable"><div class="circle"></div></div><div class="line-w line resizable"><div class="circle"></div></div><div class="circle-nw circle resizable"></div><div class="circle-ne circle resizable"></div><div class="circle-se circle resizable"></div><div class="circle-sw circle resizable"></div>';
|
|
@@ -27,23 +37,264 @@ boxContent.prototype.boxTpl = '<div class="line-n line resizable"><div class="ci
|
|
|
//由数据渲染层dom
|
|
|
boxContent.prototype.renderToDom = function() {
|
|
|
this.vdom = $('<div class="outer-box" id="' + this.domId +'"></div>');
|
|
|
- this.vdom.css({ width: this.width, height: this.height,top: this.top, left: this.left });
|
|
|
+ this.vdom.css({ width: this.width, 'min-height': this.height,top: this.top, left: this.left });
|
|
|
this.renderBoxDom();
|
|
|
};
|
|
|
|
|
|
//组件渲染
|
|
|
boxContent.prototype.renderBoxDom = function() {
|
|
|
- let elementBoxDom = $('<div class="element-box-contents"></div>');
|
|
|
- let editorDom = $('<div class="element comp_paragraph editable-text"></div>');
|
|
|
+ let editorDom = $('<div class="element comp_paragraph editable-text" contenteditable="true"></div>');
|
|
|
let circleDom = $('<div class="eqc-select"></div>');
|
|
|
editorDom.text('双击此处进行编辑');
|
|
|
if(this.editorContent)editorDom.html(this.editorContent);
|
|
|
- editorDom.css({ width: this.width, height: this.height, 'pointer-events': this.isEditor ? 'all' : 'none' });
|
|
|
- elementBoxDom.append(editorDom);
|
|
|
+ editorDom.css({ 'pointer-events': this.isEditor ? 'all' : 'none' });
|
|
|
circleDom.append(this.boxTpl);
|
|
|
-
|
|
|
- this.vdom.append(elementBoxDom);
|
|
|
+ this.vdom.append(editorDom);
|
|
|
this.vdom.append(circleDom);
|
|
|
+ this.bindDragEvent(this.vdom);
|
|
|
+ this.bindEditorEvent(this.vdom);
|
|
|
+ //以下是各个方向的拖拽
|
|
|
+ this.bindMoveSEvent(this.vdom);
|
|
|
+};
|
|
|
+
|
|
|
+//拖动属性
|
|
|
+boxContent.prototype.bindDragEvent = function (el) {
|
|
|
+ let _this = this;
|
|
|
+ el.on('mousedown' , function(e) {
|
|
|
+ let $this = $(this);
|
|
|
+ _this.ox = e.pageX;//原始x位置
|
|
|
+ _this.oy = e.pageY;
|
|
|
+ _this.top = parseInt($this.css('top').replace('px', ''));
|
|
|
+ _this.left = parseInt($this.css('left').replace('px', ''));
|
|
|
+ _this.drag = true;
|
|
|
+ });
|
|
|
+ el.on('mousemove', function (e) {
|
|
|
+ if(_this.drag){
|
|
|
+ let x = e.pageX - _this.ox;
|
|
|
+ let y = e.pageY - _this.oy;
|
|
|
+ $(this).css({
|
|
|
+ left: _this.left + x,
|
|
|
+ top: _this.top + y,
|
|
|
+ opacity: .5
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }).on('mouseup',function(e){
|
|
|
+ _this.drag = false;
|
|
|
+ $(this).css('opacity',1);
|
|
|
+ });
|
|
|
+
|
|
|
};
|
|
|
|
|
|
+//朝南方向拖拽
|
|
|
+boxContent.prototype.bindMoveSEvent = function(el) {
|
|
|
+ let _this = this;
|
|
|
+ el.on('mousedown','.line-s, .line-s .circle', function(e){
|
|
|
+ _this.oy = e.pageY;//原始x位置
|
|
|
+ _this.height = el.height();
|
|
|
+ _this.smove = true;
|
|
|
+ el.on('mousemove','.line-s, .line-s .circle', function(e){
|
|
|
+ if(_this.smove){
|
|
|
+ let y = (e.pageY - parseInt(_this.oy));
|
|
|
+ _this.vdom.css({
|
|
|
+ height: parseInt(_this.height) + y
|
|
|
+ }).find('.editable-text').css({
|
|
|
+ height: parseInt(_this.height) + y
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .on('mouseup', function(){
|
|
|
+ _this.smove = false;
|
|
|
+ $(this).off('mousemove');
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+//朝北方向
|
|
|
+boxContent.prototype.bindMoveNEvent = function(el) {
|
|
|
+ let _this = this;
|
|
|
+ el.on('mousedown','.line-n, .line-n .circle', function(e){
|
|
|
+ _this.oy = e.pageY;//原始x位置
|
|
|
+ _this.height = el.height();
|
|
|
+ _this.top = parseInt(_this.vdom.css('top').replace('px', ''));
|
|
|
+ _this.nmove = true;
|
|
|
+ el.on('mousemove','.line-n, .line-n .circle', function(e){
|
|
|
+ if(_this.nmove){
|
|
|
+ let y = (e.pageY - parseInt(_this.oy));
|
|
|
+ _this.vdom.css({
|
|
|
+ height: _this.height - y,
|
|
|
+ top: _this.top + y
|
|
|
+ }).find('.editable-text').css({
|
|
|
+ height: _this.height - y
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .on('mouseup', function(){
|
|
|
+ _this.nmove = false;
|
|
|
+ $(this).off('mousemove');
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+//朝西方向
|
|
|
+boxContent.prototype.bindMoveWEvent = function(el) {
|
|
|
+ let _this = this;
|
|
|
+ el.on('mousedown','.line-w, .line-w .circle', function(e){
|
|
|
+ _this.oy = e.pageX;//原始x位置
|
|
|
+ _this.width = el.width();
|
|
|
+ _this.wmove = true;
|
|
|
+ _this.left = parseInt(_this.vdom.css('left').replace('px', ''));
|
|
|
+ el.on('mousemove','.line-w, .line-w .circle', function(e){
|
|
|
+ if(_this.wmove){
|
|
|
+ let x = (e.pageX - parseInt(_this.ox));
|
|
|
+ _this.vdom.css({
|
|
|
+ width: _this.width - x,
|
|
|
+ left: _this.left + x
|
|
|
+ }).find('.editable-text').css({
|
|
|
+ width: _this.width - x
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .on('mouseup', function(){
|
|
|
+ _this.wmove = false;
|
|
|
+ $(this).off('mousemove');
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+// 朝东方向
|
|
|
+boxContent.prototype.bindMoveEEvent = function (el) {
|
|
|
+ let _this = this;
|
|
|
+ el.on('mousedown','.line-e, .line-e .circle', function(e){
|
|
|
+ _this.oy = e.pageX;//原始x位置
|
|
|
+ _this.width = el.width();
|
|
|
+ _this.emove = true;
|
|
|
+ el.on('mousemove','.line-e, .line-e .circle', function(e){
|
|
|
+ if(_this.emove){
|
|
|
+ let x = (e.pageX - parseInt(_this.ox));
|
|
|
+ _this.vdom.css({
|
|
|
+ width: parseInt(_this.width) + x,
|
|
|
+ }).find('.editable-text').css({
|
|
|
+ width: parseInt(_this.width) - x
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .on('mouseup', function(){
|
|
|
+ _this.emove = false;
|
|
|
+ $(this).off('mousemove');
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+// 朝西北
|
|
|
+boxContent.prototype.bindMoveNWEvent = function (el) {
|
|
|
+ let _this = this;
|
|
|
+ el.on('mousedown','.circle-nw',function(e){
|
|
|
+ _this.ox = e.pageX;//原始x位置
|
|
|
+ _this.oy = e.pageY;
|
|
|
+ _this.width = _this.vdom.width();
|
|
|
+ _this.height = _this.vdom.height();
|
|
|
+ _this.top = parseInt(_this.vdom.css('top').replace('px', ''));
|
|
|
+ _this.left = parseInt(_this.vdom.css('left').replace('px', ''));
|
|
|
+ _this.nwmove = true;
|
|
|
+
|
|
|
+ el.on('mousemove','.circle-nw', function(e){
|
|
|
+ if(_this.nwmove){
|
|
|
+ let x = e.pageX - _this.ox;
|
|
|
+ let y = e.pageY - _this.oy;
|
|
|
+ el.css({
|
|
|
+ height: _this.height - y,
|
|
|
+ // top: otop + y,
|
|
|
+ width: _this.width - x,
|
|
|
+ // left: oleft + x
|
|
|
+ });
|
|
|
+ _this.vdom.css({
|
|
|
+ height: _this.height - y,
|
|
|
+ top: _this.top + y,
|
|
|
+ width: _this.width - x,
|
|
|
+ left: _this.left + x
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .on('mouseup', function(){
|
|
|
+ _this.nwmove = false;
|
|
|
+ $(this).off('mousemove');
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+// 朝西南
|
|
|
+boxContent.prototype.bindMoveNWEvent = function (el) {
|
|
|
+ let _this = this;
|
|
|
+ el.on('mousedown','.circle-nw',function(e){
|
|
|
+ _this.ox = e.pageX;//原始x位置
|
|
|
+ _this.oy = e.pageY;
|
|
|
+ _this.width = _this.vdom.width();
|
|
|
+ _this.height = _this.vdom.height();
|
|
|
+ _this.left = parseInt(_this.vdom.css('left').replace('px', ''));
|
|
|
+ _this.swmove = true;
|
|
|
+
|
|
|
+ el.on('mousemove','.circle-nw', function(e){
|
|
|
+ if(_this.swmove){
|
|
|
+ let x = e.pageX - _this.ox;
|
|
|
+ let y = e.pageY - _this.oy;
|
|
|
+ el.css({
|
|
|
+ height: _this.height + y,
|
|
|
+ width: _this.width - x,
|
|
|
+ });
|
|
|
+ _this.vdom.css({
|
|
|
+ height: _this.height + y,
|
|
|
+ width: _this.width - x,
|
|
|
+ left: _this.left + x
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .on('mouseup', function(){
|
|
|
+ _this.swmove = false;
|
|
|
+ $(this).off('mousemove');
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+//朝东南
|
|
|
+boxContent.prototype.bindMoveSEEvent = function (el){
|
|
|
+ let _this = this;
|
|
|
+ el.on('mousedown','.circle-se',function(e){
|
|
|
+ _this.ox = e.pageX;//原始x位置
|
|
|
+ _this.oy = e.pageY;
|
|
|
+ _this.width = _this.vdom.width();
|
|
|
+ _this.height = _this.vdom.height();
|
|
|
+ _this.semove = true;
|
|
|
+
|
|
|
+ el.on('mousemove','.circle-se', function(e){
|
|
|
+ if(_this.semove){
|
|
|
+ let x = e.pageX - _this.ox;
|
|
|
+ let y = e.pageY - _this.oy;
|
|
|
+ el.css({
|
|
|
+ height: _this.height + y,
|
|
|
+ width: _this.width + x,
|
|
|
+ });
|
|
|
+ _this.vdom.css({
|
|
|
+ height: _this.height + y,
|
|
|
+ width: _this.width + x,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .on('mouseup', function(){
|
|
|
+ _this.semove = false;
|
|
|
+ $(this).off('mousemove');
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+boxContent.prototype.bindEditorEvent = function(el) {
|
|
|
+ el.on('dblclick', function(e) {
|
|
|
+ let child = e.target.children[0] || e.target;
|
|
|
+ $(this).find('.comp_paragraph').css('pointer-events','all').select();
|
|
|
+ let range = window.getSelection();// 创建range
|
|
|
+ range.selectAllChildren(child);// range 选择obj下所有子内容
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
export default boxContent;
|