TileViewListController.js 15.4 KB

'use strict';

AIA.controller("TileViewListController", ["$scope", "$rootScope", "$compile", "$http", "$log", "$location", "$timeout", "DA", "Modules", "$routeParams", "BodyRegions", "BodySystems", "ViewOrientations", "ImageTypes", "DataService", "ModuleService",
function ($scope, $rootScope, $compile, $http, $log, $location, $timeout, DA, Modules, $routeParam,BodyRegions, BodySystems, ViewOrientations, ImageTypes, DataService, ModuleService) {


    $rootScope.OpenedTileData = [];
    var ATLAS_ANATOMY = 'Atlas Anatomy';
    var CLINICAL_ILLUSTRATION = 'Clinical Illustrations';
    var CLINICAL_ANIMATION = 'Clinical Animations';
    //variables to bind Filter by Controls
    $scope.searchAAListViewData = [];
    $scope.SelectedAAthumbImage = [];
    $scope.SelectedAAImage = [];
    $scope.SelectedAASummary = [];
    $scope.SelectedAAId = [];
    $scope.SelectedAATitle = [];
    $scope.selectedAAListViewData = [];
    $scope.filterstring = false;
    $scope.AllBodyRegion = [];
    $scope.AllBodySystem = [];
    $scope.AllOrientation = [];
    $scope.AllImageType = [];
    $scope.query = {
        selectedbodyregion: '',
        selectedbodysystem: '',
        selectedorientation: '',
        selectedimagetype: '',
    };
    $scope.loadForModuleById = function (moduleId) {
       
        $scope.moduleId = moduleId;

        console.log('loadForModuleById is called')
        $rootScope.moduleName = Modules[moduleId].Name;
        
        //localStorage.setItem("CurrentBodyRegion", '');
        //localStorage.setItem("CurrentBodySystem", '');
        //localStorage.setItem("CurrentOrientation", '');
        //localStorage.setItem("CurrentImageType", '');
        $scope.AllBodyRegion = BodyRegions;
        $scope.AllBodySystem = BodySystems;
        $scope.AllOrientation = ViewOrientations;
        $scope.AllImageType = ImageTypes;
        var promise = ModuleService.loadModuleDataBasedOnModuleName($scope.moduleName)
            .then(
              function (result) {
                  $scope.moduleLandingData = result;
                 
                  console.log(JSON.stringify(result, null, 4));
              },
               function (error) {
                   // handle errors here
                   console.log(' error: ' + error.statusText);
               }
              )

        
        
      
    }

    $scope.openModuleItemView = function (event) {

        debugger;

        //0. Get selected Image Id
        var moduleItemDataToBeSaved = event.target.id;

        //1.Filter selected module ietem data and get the pushed opened moduile array object
        $rootScope.OpenedTileData = ModuleService.GetOpenedTileData(moduleItemDataToBeSaved, $scope.moduleLandingData);

        //2. Pick the image name to create a source to open
        $rootScope.imageName = $rootScope.OpenedTileData[3];
        $rootScope.OpenItemImagePath = "../../../content/images/aa/images/" + $rootScope.OpenedTileData[3];

        //3. set opened module item title
        localStorage.setItem("currentViewTitle", $rootScope.OpenedTileData[6]);

        //3. Navigate to the Module-item-view
        var u = $location.url();
        $location.url('/module-item-view');
    }


    $scope.openModuleItem = function ()
    {
        var jsContentURL;
        var moduleItemViewDivId;

        //1. create a jsPanel and load the module item view
        if ($rootScope.moduleName == ATLAS_ANATOMY) {

            jsContentURL = 'app/views/aa/atlas-anatomy-detail.html';
            moduleItemViewDivId = 'aaDetailPageDiv';           
        }
        else if ($rootScope.moduleName == CLINICAL_ANIMATION) {
            //to do, in next phase of project 

        }
        else if ($rootScope.moduleName == CLINICAL_ILLUSTRATION) {
            //to do, in next phase of project

        }

        $.jsPanel({
            id: 'ImagePanel',
            selector: '.aaBodyView',
            theme: 'success',
            currentController: 'TileViewListController',
            parentSlug: 'tile-view',
            ajax: {
                url: jsContentURL
            },
            title: localStorage.getItem("currentViewTitle"),

            position: {
                top: 70,
                left: 1,
            },

            size: { width: $(window).outerWidth() - 10, height: $(window).outerHeight() - 110 },

        });

        console.log('jsPanel loaded ImagePanel exist= ' + document.getElementById('ImagePanel'))

        //0.3
        var aaViewElement = angular.element(document.getElementById(moduleItemViewDivId));

        //0.4 for Opening module item, load it into image
        var openedImage = new Image();
        openedImage.name = $rootScope.OpenItemImagePath;
        openedImage.onload = getImageHeightWidth;
        openedImage.onerror = loadFailure;
        openedImage.src = $rootScope.OpenItemImagePath;

        $timeout(function () {
            $compile(aaViewElement.contents())($scope);
           
            if (document.getElementById('aaDetailPageDiv') != null) {

                //0.4 added some stylesheets
                $('#aaBodyView').css("height", $(window).outerHeight());
                $('#aaBodyView').css("width", $(window).outerWidth());

                $scope.currentTitleFromJson = localStorage.getItem("currentViewTitle");
                $("#modItemImage").attr('src', $rootScope.OpenItemImagePath);

                //0.5 create canvas on the top of image so that I can draw a line over the canvas.
                var canvas = document.createElement('canvas');
                canvas.height = parseInt(($scope.imageHeight));
                canvas.width = parseInt($scope.imageWidth);
                canvas.style.left = '0px';
                canvas.style.top = '0px';              
                canvas.style.position = "absolute";

                $("#imageDiv").append(canvas);
                $scope.context = canvas.getContext("2d")
               
                //6. Show all pins on AA opened item
                $scope.showAllPins();
            }
        }, 1050);
    }

    function loadFailure() {
        console.log('unable to load opened module ietem');
       return true;
    }

    function getImageHeightWidth()
    {
           $scope.imageWidth = this.width;
           $scope.imageHeight = this.height;
    }

    angular.element(document).ready(function (e) {
        $("#ImagePanel").resize(function () {
            $("#imageDiv").scrollLeft($rootScope.CanvasDivLeftPosition);
            $("#imageDiv").scrollTop($rootScope.CanvasDivTopPosition);
        });

    })

    $scope.showAllPins = function () {

        var promise = ModuleService.getPinDataForImage($rootScope.imageName)

          .then(
            function (result) {
                $scope.aaPinData = result.data.Root.Item;
                debugger;
                angular.forEach($scope.aaPinData, function (value, key)
                {
                            
                    $scope.context.beginPath();
                    $scope.context.moveTo(value._PinX, value._PinY);
                    $scope.context.lineTo(value._HeadX, value._HeadY);
                    $scope.context.stroke();

                    var headX = (parseInt(value._HeadX)) - 20;
                    var headY = (parseInt(value._HeadY)) - 15;

                        var img = new Image();                  
                        img.src = "~/../../../content/images/noraml-pin.png";
                        img.onload = function () {
                        $scope.context.drawImage(img, headX, headY);
                    }

                })
             
                console.log(JSON.stringify(result, null, 4));
            },
             function (error) {
                 // handle errors here
                 console.log(' error: ' + error.statusText);
             }
            )
    }


    $scope.FilterByImage = function (moduleId, query)
    {
        //alert(JSON.stringify(query));
        $scope.moduleId = moduleId;

        console.log('loadForModuleById is called')
        $rootScope.moduleName = Modules[moduleId].Name;
        while ($scope.searchAAListViewData.length) {
            $scope.searchAAListViewData.pop();
        }

       var filtercount = 0;
        if (typeof (query.selectedbodyregion) !== "undefined" && (query.selectedbodyregion !== null && query.selectedbodyregion !== "")) {
            filtercount = filtercount + 1;
            localStorage.setItem("CurrentBodyRegion", query.selectedbodyregion);
        }
        else {
            localStorage.setItem("CurrentBodyRegion", '');
        }

        if (typeof (query.selectedbodysystem) !== "undefined" && (query.selectedbodysystem !== null && query.selectedbodysystem !== "")) {
            filtercount = filtercount + 1;
            localStorage.setItem("CurrentBodySystem", query.selectedbodysystem);
        }
        else {
            localStorage.setItem("CurrentBodySystem", '');
        }

        if (typeof (query.selectedorientation) !== "undefined" && (query.selectedorientation !== null && query.selectedorientation !== '')) {
            filtercount = filtercount + 1;
            localStorage.setItem("CurrentOrientation", query.selectedorientation);
        }
        else {
            localStorage.setItem("CurrentOrientation", '');
        }

        if (typeof (query.selectedimagetype) !== "undefined" && (query.selectedimagetype !== null && query.selectedimagetype !== '')) {
            filtercount = filtercount + 1;
            localStorage.setItem("CurrentImageType", query.selectedimagetype);
        }
        else {
            localStorage.setItem("CurrentImageType", '');
        }

        var promise = ModuleService.loadModuleDataBasedOnModuleName($scope.moduleName)
           .then(
             function (result) {
                // alert(JSON.stringify($scope.moduleLandingData));
                 $scope.moduleLandingData = result;
                 $scope.selectedAAListViewData = new jinqJs()
                                                     .from($scope.moduleLandingData.data.root.Item)
                                                     //.orderBy([{ field: '_Title', sort: 'asc' }])
                                                     .select();

                 $('#grid-view').empty();
                 //alert(JSON.stringify($scope.selectedAAListViewData))
                 angular.forEach($scope.selectedAAListViewData, function (value, key) {

                     var selectimg = true;
                     var count = 0;


                     if (typeof (query.selectedbodyregion) !== "undefined" && (query.selectedbodyregion !== null && query.selectedbodyregion !== "")) {
                         var posbodyregion = value._BodyRegion.indexOf((query.selectedbodyregion.trim()));
                         if (posbodyregion > -1) {
                             selectimg = true;
                             count = count + 1;

                         }
                         else {
                             selectimg = false;
                             count = count - 1;
                         }

                     }

                     if (typeof (query.selectedbodysystem) !== "undefined" && (query.selectedbodysystem !== null && query.selectedbodysystem !== "")) {
                         var posbodysystem = value._BodySystem.indexOf(query.selectedbodysystem);
                         if (posbodysystem > -1) {
                             count = count + 1;
                             selectimg = true;
                         } else {
                             selectimg = false;
                             count = count - 1;
                         }
                     }

                     if (typeof (query.selectedorientation) !== "undefined" && (query.selectedorientation !== null && query.selectedorientation !== "")) {
                         var posorientation = value._ViewOrientation.indexOf(query.selectedorientation);
                         if (posorientation > -1) {
                             count = count + 1;
                             selectimg = true;

                         } else {
                             selectimg = false;
                             count = count - 1;
                         }

                     }

                     if (typeof (query.selectedimagetype) !== "undefined" && (query.selectedimagetype !== null && query.selectedimagetype !== "")) {
                         var posimagetype = value._ImageType.indexOf(query.selectedimagetype);
                         if (posimagetype > -1) {
                             count = count + 1;
                             selectimg = true;

                         } else {
                             selectimg = false;
                             count = count - 1;
                         }

                     }                    

                     if (selectimg === true && count >= filtercount) {


                         $scope.imagePath = "../content/images/aa/thumbnails/" + value._TileImageName;

                         var $el = $('<div id="' + value._id + '" class="col-sm-3 col-md-3" title = "' + value._Title + '" data-ng-click="openView($event)">'
                             + '<div class="thumbnail" >'
                                        + '<img class="tinyImg" id="' + value._Title + '"ng-src="' + $scope.imagePath + '" alt="" title="" >'
                                        + '<div class="caption"><p>' + value._Title + '</p></div></a></div></div>').appendTo('#grid-view');


                         $compile($el)($scope);

                         $(".sidebar").mCustomScrollbar({
                             autoHideScrollbar: true,
                             //theme:"rounded"
                         });


                         $scope.searchAAListViewData.push(
                         {
                             "_id": value._id,
                             "_ImageId": value._ImageId,
                             "_Title": value._Title,
                             "_ImageName": value._contentImage,
                             "_TileImageName": value._ThumbnailImage,
                             "_BodySystem": value._BodySystem,
                             "_BodyRegion": value._BodyRegion,
                             "_ViewOrientation": value._ViewOrientation,
                             "_ImageType": value._ImageType
                             //"$$hashKey": value._Summary

                         });
                         
                     }


                 });

                 //Show Error Message in case of gridview if no data is found
                 if ($scope.searchCIListViewData.length == 0) {

                     var $el = $('<div class="col-sm-12" style="padding-left:25px;padding-top:10px;"><strong style="color:white">No illustration found for the selected search criteria!</strong></div>').appendTo('#grid-view');
                     $compile($el)($scope);
                 }
                 console.log(JSON.stringify(result, null, 4));
             },
              function (error) {
                  // handle errors here
                  console.log(' error: ' + error.statusText);
              }
             )

    }


}]);