Skip to content

Commit 65c361c

Browse files
committed
Add functionality to transfrom the textbox to a canvas
1 parent 0f9e1a8 commit 65c361c

1 file changed

Lines changed: 104 additions & 8 deletions

File tree

Xrm.Portal.js

Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Xrm.Portal = {
1717
var base64 = decodeURIComponent(atob(base64Url).split('').map(function (c) {
1818
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
1919
}).join(''));
20-
20+
2121
return JSON.parse(base64);
2222
}
2323
throw "No login user is detected.";
@@ -90,7 +90,7 @@ Xrm.Portal = {
9090
}
9191
);
9292

93-
validationGroup = (validationGroup == null || validationGroup == "" ) && Page_Validators.length > 0 ? Page_Validators[0].validationGroup : validationGroup;
93+
validationGroup = (validationGroup == null || validationGroup == "") && Page_Validators.length > 0 ? Page_Validators[0].validationGroup : validationGroup;
9494

9595
var vF = validationFunction == null && isRequired ? function () {
9696
return Xrm.Portal.Utility.Validation.required(control)
@@ -378,10 +378,18 @@ Xrm.Portal = {
378378
Xrm.Portal.Utility.Event.removeOnChange(this.c);
379379
return this;
380380
};
381-
this.setValidationGroup = function(g) {
381+
this.setValidationGroup = function (g) {
382382
this.vg = g;
383383
return this;
384384
};
385+
this.transformToCanvas = function () {
386+
this.c.hide();
387+
if (this.c.parent().children().last()[0].tagName !== "CANVAS") {
388+
var canvasId = this.id + "Canvas";
389+
this.c.parent().append("<canvas id=" + canvasId + "></canvas>");
390+
Xrm.Portal.Control.Canvas(this.id);
391+
}
392+
}
385393
},
386394
Lookup: function (c) {
387395
this.s = Xrm.Portal.Utility.Selector;
@@ -427,7 +435,7 @@ Xrm.Portal = {
427435
isRequired || customFunction != undefined ?
428436
Xrm.Portal.Utility.Validation.setValidation(g, this, isRequired, this.vg, customFunction, customMessage) :
429437
Xrm.Portal.Utility.Validation.removeValidation(g, this);
430-
return this;
438+
return this;
431439
};
432440
this.attachOnChange = function (callback) {
433441
Xrm.Portal.Utility.Event.attachOnChange(this.cL, callback);
@@ -437,7 +445,7 @@ Xrm.Portal = {
437445
Xrm.Portal.Utility.Event.removeOnChange(this.cL);
438446
return this;
439447
};
440-
this.setValidationGroup = function(g) {
448+
this.setValidationGroup = function (g) {
441449
this.vg = g;
442450
return this;
443451
};
@@ -481,7 +489,7 @@ Xrm.Portal = {
481489
Xrm.Portal.Utility.Event.removeOnChange(this.c);
482490
return this;
483491
};
484-
this.setValidationGroup = function(g) {
492+
this.setValidationGroup = function (g) {
485493
this.vg = g;
486494
return this;
487495
};
@@ -525,7 +533,7 @@ Xrm.Portal = {
525533
Xrm.Portal.Utility.Event.removeOnChange(this.c);
526534
return this;
527535
};
528-
this.setValidationGroup = function(g) {
536+
this.setValidationGroup = function (g) {
529537
this.vg = g;
530538
return this;
531539
};
@@ -569,11 +577,99 @@ Xrm.Portal = {
569577
Xrm.Portal.Utility.Event.removeOnChange(this.c);
570578
return this;
571579
};
572-
this.setValidationGroup = function(g) {
580+
this.setValidationGroup = function (g) {
573581
this.vg = g;
574582
return this;
575583
};
576584
},
585+
Canvas: function (id) {
586+
var canvas, context, tool;
587+
588+
function init(id) {
589+
// Find the canvas element.
590+
canvas = document.getElementById(id + "Canvas");
591+
if (!canvas) {
592+
alert('Error: I cannot find the canvas element!');
593+
return;
594+
}
595+
596+
if (!canvas.getContext) {
597+
alert('Error: no canvas.getContext!');
598+
return;
599+
}
600+
601+
// Get the 2D canvas context.
602+
context = canvas.getContext('2d');
603+
if (!context) {
604+
alert('Error: failed to getContext!');
605+
return;
606+
}
607+
608+
// Pencil tool instance.
609+
tool = new tool_pencil(id, canvas.id);
610+
611+
// Attach the mousedown, mousemove and mouseup event listeners.
612+
canvas.addEventListener('mousedown', ev_canvas, false);
613+
canvas.addEventListener('mousemove', ev_canvas, false);
614+
canvas.addEventListener('mouseup', ev_canvas, false);
615+
}
616+
617+
// This painting tool works like a drawing pencil which tracks the mouse
618+
// movements.
619+
function tool_pencil(id, canvasId) {
620+
var id = id;
621+
var canvasId = canvasId;
622+
var tool = this;
623+
this.started = false;
624+
625+
// This is called when you start holding down the mouse button.
626+
// This starts the pencil drawing.
627+
this.mousedown = function (ev) {
628+
context.beginPath();
629+
context.moveTo(ev._x, ev._y);
630+
tool.started = true;
631+
};
632+
633+
// This function is called every time you move the mouse. Obviously, it only
634+
// draws if the tool.started state is set to true (when you are holding down
635+
// the mouse button).
636+
this.mousemove = function (ev) {
637+
if (tool.started) {
638+
context.lineTo(ev._x, ev._y);
639+
context.stroke();
640+
}
641+
};
642+
643+
// This is called when you release the mouse button.
644+
this.mouseup = function (ev) {
645+
if (tool.started) {
646+
tool.mousemove(ev);
647+
tool.started = false;
648+
}
649+
Xrm.Portal.Form.get(id).setValue(document.getElementById(canvasId).toDataURL());
650+
};
651+
}
652+
653+
// The general-purpose event handler. This function just determines the mouse
654+
// position relative to the canvas element.
655+
function ev_canvas(ev) {
656+
if (ev.layerX || ev.layerX == 0) { // Firefox
657+
ev._x = ev.layerX;
658+
ev._y = ev.layerY;
659+
} else if (ev.offsetX || ev.offsetX == 0) { // Opera
660+
ev._x = ev.offsetX;
661+
ev._y = ev.offsetY;
662+
}
663+
664+
// Call the event handler of the tool.
665+
var func = tool[ev.type];
666+
if (func) {
667+
func(ev);
668+
}
669+
}
670+
671+
init(id);
672+
}
577673
},
578674
EventType: {
579675
OnChange: 1,

0 commit comments

Comments
 (0)