MyPictureController.js 16.3 KB
'use strict';

AIA.controller("MyPictureController", ["$scope", "$window", "$rootScope", "$compile", "$http", "$log", "$location", "$timeout","ModuleService", "$interval",
function ($scope, $window, $rootScope, $compile, $http, log, $location, $timeout, ModuleService, $interval) {
  
    $scope.ObjectAttribute=function(windowviewid)
    {
        var windata={
                    'multiwinid': windowviewid,
                    'imageSource': [],
                    'moduleName': '',
                    'currentViewTitle': '',
                    'parentSlugName': '',
                    'currentSlug': '',
                    'imageId': '',
                    'top': 0,
                    'left': 0,
                    'width': 0,
                    'height': 0,
                    'minimised': false,
                    'maximised': false,
                    'annotationData':{shapeStates:[],paintCanvasState:[]},

            };
            return windata;
    }

    $scope.initializePICWindowData = function (windowviewid, isOpenWithExistsModule, openPanelNo) {
        if (isOpenWithExistsModule || openPanelNo == 0) {
            if ($rootScope.PICWindowData != undefined) {
                $rootScope.PICWindowData.length = 0;
            }
            else {
                $rootScope.PICWindowData = [];
            }

            $rootScope.PICWindowData.push($scope.ObjectAttribute(windowviewid));

        }
        else {
            var isNewWindow = true;
            for (var k = 0; k < $rootScope.PICWindowData.length; k++) {
                if ($rootScope.PICWindowData[k].multiwinid == windowviewid) {
                    isNewWindow = false;
                    break;
                }
            }

            if (isNewWindow) {
                $rootScope.PICWindowData.push($scope.ObjectAttribute(windowviewid));
            }
        }
    }
  
    $scope.GetPICwindowStoreData = function (windowviewid, keyname) {
        for (var x = 0 ; x < $rootScope.PICWindowData.length; x++) {

            if ($rootScope.PICWindowData[x].multiwinid == windowviewid) {
                return $rootScope.PICWindowData[x][keyname];
            }
        }
    }

    $scope.SetPICwindowStoreData = function (windowviewid, keyname, value) {
        for (var x = 0 ; x < $rootScope.PICWindowData.length; x++) {

            if ($rootScope.PICWindowData[x].multiwinid == windowviewid) {
                $rootScope.PICWindowData[x][keyname] = value;
            }
        }
    }

    // access from home controller
    $rootScope.GetPICwindowData=function(windowviewid,keyname)
    {
        for(var x=0 ;x < $rootScope.PICWindowData.length;x++){

            if($rootScope.PICWindowData[x].multiwinid==windowviewid)
            {
                return $rootScope.PICWindowData[x][keyname];
            }
        }
    }
    // access from home controller
    $rootScope.SetPICwindowData=function(windowviewid,keyname,data) {
        for(var x=0 ;x < $rootScope.PICWindowData.length;x++){

            if($rootScope.PICWindowData[x].multiwinid==windowviewid)
            {
                    $rootScope.PICWindowData[x][keyname]=data;
            }
        }
    }
    
    $scope.DisableUI = function () {

        var myImagePanelConetent = document.getElementsByClassName("jsPanel-content");
        for (var i = 0; i < myImagePanelConetent.length; i++) {
            myImagePanelConetent[i].style.pointerEvents = "none";
            myImagePanelConetent[i].style.opacity = "0.7";

        }
        $rootScope.isLoading = true;
        $('#spinner').css('visibility', 'visible');

        // CB module disable all
        $('#CBDetailPageDiv').css('pointer-events', 'none');
        $('#CBDetailPageDiv').css('opacity', '0.7');
    }

    $scope.EnableUI = function () {

        var myImagePanelConetent = document.getElementsByClassName("jsPanel-content");
        for (var i = 0; i < myImagePanelConetent.length; i++) {
            myImagePanelConetent[i].style.pointerEvents = "auto";
            myImagePanelConetent[i].style.opacity = "1";
        }

        $rootScope.isLoading = false;
        $('#spinner').css('visibility', 'hidden');
        // CB module enable all
        $('#CBDetailPageDiv').css('pointer-events', 'auto');
        $('#CBDetailPageDiv').css('opacity', '1');

    }

    $scope.RemoveJSPanel = function (panelid) {

        var len = (panelid).split("_").length;
        var windowviewid = (panelid).split("_")[len - 1];

        // remove old stored data after close panel
        for (var index = 0 ; index < $rootScope.PICWindowData.length; index++) {

            if ($rootScope.PICWindowData[index].multiwinid == windowviewid) {

                if (index != -1) {
                    // remove module which one is loaded
                    $rootScope.PICWindowData.splice(index, 1);
                    if ($('#' + panelid).html() != undefined) {

                        $('#' + panelid).remove();

                    }
                    $rootScope.resetjsPanelTop(panelid);
                }
            }
        }
    }
    $scope.PanelActivity = function () {

        $("#" + $scope.jsPanelID).resizable({
            stop: function (event, ui) {
                var len = (event.currentTarget.id).split("_").length;
                var windowviewid = (event.currentTarget.id).split("_")[len - 1];
                $scope.SetPICwindowStoreData(windowviewid, 'width', ui.size.width);
                $scope.SetPICwindowStoreData(windowviewid, 'height', ui.size.height);
            }
        });

        $("#" + $scope.jsPanelID).draggable({
            stop: function (event, ui) {
                var len = (event.currentTarget.id).split("_").length;
                var windowviewid = (event.currentTarget.id).split("_")[len - 1];
                //var offSets = $(this).offset(); var postions = (this).position(); (this).offsetTop (this).offsetLeft
                $scope.SetPICwindowStoreData(windowviewid, 'top', ui.position.top);
                $scope.SetPICwindowStoreData(windowviewid, 'left', ui.position.left);
            }
        });   
        // close panel
        $(document).on("click", "#" + $scope.jsPanelID + " .jsPanel-hdr .jsPanel-hdr-r .jsPanel-btn-close .jsglyph-remove", function () {

            var panelid = $(event.target).parent().parent().parent().parent().attr('id');

            $scope.RemoveJSPanel(panelid);

        });
    }

    $rootScope.OpenMyPictureViewMain = function () {  
        $scope.DisableUI(); 
        if ($rootScope.isCallFromOtherModule) {
            $scope.PicModuleData = ModuleService.getModuleData("MY_PICTURES");
            $scope.readyToLoad = true;
            $rootScope.PicLoadComplete = false;
            $scope.wincount = 1;
            var winlen = $scope.PicModuleData.length;
            var timeint = null;
            timeint = $interval(function () {

                if ($scope.readyToLoad == true) {
                    var windata = $scope.PicModuleData[$scope.wincount - 1];
                    $scope.OpenPictureView(windata);

                }
                $scope.readyToLoad = false;
                if ($scope.wincount < winlen && $rootScope.PicLoadComplete == true) {
                    $scope.wincount = $scope.wincount + 1;
                    $rootScope.PicLoadComplete = false;
                    $scope.readyToLoad = true;
                }

                if ($scope.wincount == winlen && $rootScope.PicLoadComplete == true) {
                    $scope.stopInterval();
                    $scope.$emit("LoadModuleComplete", "MY_PICTURES");
                }


            }, 100);

            $scope.stopInterval = function () {
                if (angular.isDefined(timeint)) {
                    $interval.cancel(timeint);
                    timeint = undefined;
                }

            };

        }
       
    }
     
    $scope.OpenPictureView = function (picModuleData) { 

        $scope.picOpenInOtherModules = picModuleData;
        $rootScope.MULTI_VIEW_ID += 1;
        var windowviewid = $rootScope.MULTI_VIEW_ID;

        $scope.initializePICWindowData(windowviewid, false, $scope.picOpenInOtherModules.currentWindowId);

        var moduleName = $scope.picOpenInOtherModules.mType;
        $scope.SetPICwindowStoreData(windowviewid, 'moduleName', moduleName);
        // serial no of imageid
        var imageId = $scope.picOpenInOtherModules.id;

        $scope.SetPICwindowStoreData(windowviewid, 'imageId', imageId);

        var imageSource=$scope.picOpenInOtherModules.imageSource;

        $scope.SetPICwindowStoreData(windowviewid, 'imageSource', imageSource);
        
        var aiTitle = $scope.picOpenInOtherModules.anatomyTitle;
        $scope.SetPICwindowStoreData(windowviewid, 'currentViewTitle', aiTitle);
        localStorage.setItem("currentViewTitle", aiTitle);
        var isMaximize = $scope.picOpenInOtherModules.maximised;
        var isMinimize = $scope.picOpenInOtherModules.minimised;
        $scope.SetPICwindowStoreData(windowviewid, 'maximised', isMaximize);
        $scope.SetPICwindowStoreData(windowviewid, 'minimised', isMinimize);

        // store image for export
        $rootScope.StoreTitleName(aiTitle);
        $rootScope.StoreOrgImageName(aiTitle);

        if($location.url()== "/curriculum-builder-detail") {
            $scope.SetPICwindowStoreData(windowviewid,'parentSlugName',($location.url()).replace('/', ''));
        }
        else
        {
            $scope.SetPICwindowStoreData(windowviewid,'parentSlugName','');//back to home screen
        }

        $scope.loadMyPicture(windowviewid);      
        
    }

    $scope.loadMyPicture = function (windowviewid) {

        $scope.jsPanelID = 'picImagePanel' + '_' + windowviewid;
       
        var selectedpicture =  $scope.GetPICwindowStoreData(windowviewid, 'imageSource');

        if (selectedpicture.indexOf('base64') == -1) {
            selectedpicture="data:image/png;base64,"+selectedpicture;
        }
        var tittle = $scope.GetPICwindowStoreData(windowviewid, 'currentViewTitle');

            // open JS panel for curriculum with define cornonate in CB jason
            $scope.jsPanelWidth = $scope.picOpenInOtherModules.size.width;//1000;
            $scope.jsPanelHeight = $scope.picOpenInOtherModules.size.height;
            if ($scope.picOpenInOtherModules.size.height > 540)
                $scope.jsPanelHeight = 540;
            $scope.jsPanelLeft = 320;
            $scope.jsPanelTop = $rootScope.cBModulejsPanelTop();
            if($location.url()!= "/curriculum-builder-detail") {
                $scope.jsPanelLeft = 1;
            }

    $scope.jsPanelPIC =  $.jsPanel({
                    id: $scope.jsPanelID,
                    selector: '.picView',
                    theme: 'success',
                    currentController: 'MyPictureController',
                    parentSlug: $scope.GetPICwindowStoreData(windowviewid, 'parentSlugName'),
                    content: '<div class="row" id="canvasDivPIC_' + windowviewid + '"><canvas id="canvasPaintPIC_' + windowviewid + '" ng-click="FreeStylePaint($event)"  width="1340" height="325" class="canvas-annotationStyle1" style="position: absolute;z-index:0;left:0px"></canvas><canvas id="canvasPIC_' + windowviewid + '" ng-click="onDrawingCanvasClick($event)" width="1340" height="325" class="canvas-annotationStyle" style="position: absolute; background-color: transparent;z-index:1;left:0px "></canvas>' +
                            '<div class="col-sm-12 img-thumbnail" align="center">' +
                            '<img src="' + selectedpicture + '"  id="mypic" alt="" title="" class="img-responsive " style="height:400px;">' +

                            '</div></div>',

                    title: tittle,
                    position: {
                        top: $scope.jsPanelTop,
                        left: $scope.jsPanelLeft
                    },

                    size: {
                        width: $scope.jsPanelWidth,
                        height: $scope.jsPanelHeight
                    },

                });

                $('#canvasDivPIC_' + windowviewid + ' img').load(function () {

                    var isMaximize = $scope.GetPICwindowStoreData(windowviewid, 'maximised');
                        var isMinimize = $scope.GetPICwindowStoreData(windowviewid, 'minimised');
                        if (isMaximize) {
                            $scope.jsPanelPIC.maximize();
                        }
                        else if (isMinimize) {
                            $scope.jsPanelPIC.minimize();
                        }
                        else {
                            $scope.jsPanelPIC.normalize();
                        }
                        $scope.SetPICwindowStoreData(windowviewid, 'top', $scope.jsPanelTop);
                        $scope.SetPICwindowStoreData(windowviewid, 'left', $scope.jsPanelLeft);
                        $scope.SetPICwindowStoreData(windowviewid, 'width', $scope.jsPanelWidth);
                        $scope.SetPICwindowStoreData(windowviewid, 'height', $scope.jsPanelHeight);


                    $scope.JsPanelclick(windowviewid);
                   
                    var canvas = document.getElementById("canvasPIC_" + windowviewid);
                    var canvasPaint = document.getElementById("canvasPaintPIC_" + windowviewid);

                    canvas.height = $("#" + $scope.jsPanelID + " .jsPanel-content").height();
                    canvasPaint.height = $("#" + $scope.jsPanelID + " .jsPanel-content").height();
                   
                        var annotationData= $scope.picOpenInOtherModules.annotationData; 
                        // load annotation  
                        if(annotationData!="" && annotationData!=undefined) 
                        {
                            if(annotationData.shapeStates.length>0||annotationData.paintCanvasState.length>0)
                            {
                                //first draw shape and then store in object
                                $rootScope.LoadCBSavedAnnotation("canvasPIC_"+windowviewid,"canvasPaintPIC_"+windowviewid,annotationData); 
                            }               
                         }
                         $scope.EnableUI(); 
                    $rootScope.PicLoadComplete = true;
                });

                $scope.SetPICwindowStoreData(windowviewid, 'currentSlug', 'my-picture');
               
                $("#" + $scope.jsPanelID + " .img-thumbnail").css("min-height", $("#" + $scope.jsPanelID + " .jsPanel-content").height());
                $('.jsPanel-content .jsPanel-theme-success').css('overflow-y', 'auto !important')
               
                $('#PicView').css("height", $(window).outerHeight() - 65);

                $('#PicView').css("width", $(window).outerWidth() - 15);

                var canvasDIvHeight = $("#" + $scope.jsPanelID + " .jsPanel-content").height();

                $('#canvasDivPIC_' + windowviewid).css('height', canvasDIvHeight);

            $scope.PanelActivity();
    }
    $rootScope.picAnnotationToolEvent = function (windowviewid) {
        
        $('#editstylebackgroundcolor .minicolors >.minicolors-swatch > .minicolors-swatch-color').css("background-color", "rgb(255,255,255)")
        $('#editstylebackgroundcolor .minicolors >.minicolors-panel > .minicolors-grid >.minicolors-picker').css({ "top": "145px", "left": "4px" });
        $('#outlineColor .minicolors >.minicolors-swatch > .minicolors-swatch-color').css("background-color", "rgb(0,0,0)")
        $('#outlineColor .minicolors >.minicolors-panel > .minicolors-grid >.minicolors-picker').css({ "top": "145px", "left": "4px" });

        $('#canvasPIC_'+windowviewid).css("display", "block");
        $('#canvasPaintPIC_'+windowviewid).css("display", "block");
       
    }

    $scope.JsPanelclick = function (windowviewid) {
        
        //reset option list manager and annotation
        //call when module loaded 
        $rootScope.resetMenuOption();

        // call from while open module in CB
        $("#picImagePanel_" + windowviewid).on('click', function (event) {

            var pnlName = event.currentTarget.id;
            $rootScope.resetMenuOptionOnClick(pnlName);
           
            // store image for export while click on panel

            var currentViewTitle = $scope.GetPICwindowStoreData(windowviewid, 'currentViewTitle');
            $rootScope.StoreTitleName(currentViewTitle);
            $rootScope.StoreOrgImageName(currentViewTitle);
        });
    }
        
    }])