LabExercController.js 7.34 KB
/// <reference path="../../content/data/json/le/LabExercise.js" />
AIA.controller("LabExercController", ["$scope", "$rootScope", "pages", "$log", '$http', 'DataService', '$filter', '$location', '$document',
function ($scope, $rootScope, pages, log, $http, DataService, $filter, $location, $document) {
    $scope.LabExerciseName;
    $scope.LabExerciseQuiz = null;
    $scope.TotalNumberofQuiz = "0";
    $scope.quiznumber = 1;
    $scope.activityTitle = "";
    $scope.Title = "";
    $scope.Imagepath = "";
    $scope.DraggedList = [];
    $scope.dragableId = "";
    $scope.$on('$viewContentLoaded', function (event) {
        // code that will be executed ... 
        // every time this view is loaded
        $scope.showme = false;
        $rootScope.currentActiveModuleTitle = pages[8].name;
        $scope.getLabExerciseModules();

        $scope.scroll();
        $document.find('div#divoptions div.thumbnail').draggable();
        //$document.find("#droppable").droppable({
        //    drop: function (event, ui) {
        //        $(this)
        //          .find("img")
        //            .html("Dropped!");
        //    }
        //});

    });

    // $scope.showme = false;
    $scope.IsVisible = function () {
        // $scope.scroll();

    }

    $scope.scroll = function () {
        // $window.scrollTo(0, 0);
        $("html,body").scrollTop(0);
        //alert("scroll");
    }
    // $rootScope.currentActiveModuleTitle = pages[8].Name;

    //to get all lab exercise modules from json files
    $scope.getLabExerciseModules = function () {
        //debugger;
        $scope.LabExerciseModules = null;
        var commondataJsonPath = '~/../content/data/json/le/LabExercise.json';

        DataService.getAnotherJson(commondataJsonPath).then(
        function (result) {
            //debugger;
            //alert(JSON.stringify(result));
            $scope.LabExerciseModules = result;
        },
        function (error) {
            console.log(error.statusText)
        }
        )
    }

    $scope.getLabExerciseTitle = function (title) {


        if (title != null) {
            $scope.LabExerciseQuiz = title;
            $scope.LabExerciseName = title.Topic;
            $location.url("/lab-exercises-detail?topic=" + (title.Topic || ""));
        }

    }


    $scope.GetQuizByTopic = function () {
        var keywords = $location.search();
        $scope.LabExerciseName = keywords.topic;
        $scope.LabExerciseModules = null;
        $scope.LabExerciseBoxes = null;
        var commondataJsonPath = '~/../content/data/json/le/LabExercise.json';

        DataService.getAnotherJson(commondataJsonPath).then(
        function (result) {
            //debugger;
            $.each(result.LabExercise, function (index, value) {
                if (result.LabExercise[index].Topic == keywords.topic) {

                    $scope.TotalNumberofQuiz = result.LabExercise[index].NumberofQuestions;
                    $.each(result.LabExercise[index].Questions, function (index1, value1) {
                        if (result.LabExercise[index].Questions[index1].Number == $scope.quiznumber) {
                            $scope.LabExerciseModules = result.LabExercise[index].Questions[index1];
                            $scope.activityTitle = result.LabExercise[index].Questions[index1].activityTitle;
                            $scope.Title = result.LabExercise[index].Questions[index1].Title;
                            $scope.Imagepath = result.LabExercise[index].Questions[index1].ImagePath;
                        }
                    });
                    //$scope.quiznumber++;
                }

            });

        },
        function (error) {
            console.log(error.statusText)
        }
        )
    }

    $scope.nextQuiz = function () {
        if ($scope.quiznumber == $scope.TotalNumberofQuiz)
            return;
        else
            $scope.quiznumber++;
        $scope.GetQuizByTopic();
        $document.find('div.panel-body div.droppable').draggable();
    }

    $scope.prevQuiz = function () {
        if ($scope.quiznumber == 1)
            return;
        else
            $scope.quiznumber--;
        $scope.GetQuizByTopic();
        $document.find('div.panel-body div.droppable').draggable();
    }

    $scope.handleDragStart = function (e) {

        this.style.opacity = '0.4';
        e.dataTransfer.setData('text/plain', this.innerHTML);
        $scope.dragableId = $(this).attr("Id");
    };

    $scope.handleDragEnd = function (e) {
        this.style.opacity = '1.0';
    };

    $scope.handleDrop = function (e) {
        e.preventDefault();
        e.stopPropagation();
        var x = $("#droppable").offset();
        //alert(x.left + "," + $("#droppable").clientWidth + "," + x.top);
        var id = $(this).attr("id");
        //alert(id);
        var keywords = $location.search();
        var dataText = e.dataTransfer.getData('text/plain');
        var commondataJsonPath = '~/../content/data/json/le/LabExercise.json';
        DataService.getAnotherJson(commondataJsonPath).then(
        function (result) {
            $.each(result.LabExercise, function (index, value) {
                if (result.LabExercise[index].Topic == keywords.topic) {
                    $.each(result.LabExercise[index].Questions, function (index1, value1) {
                        if (result.LabExercise[index].Questions[index1].Number == $scope.quiznumber) {
                            var Options = result.LabExercise[index].Questions[index1].OptionBox;
                            $.each(Options, function (inx, value2) {
                                if (Options[inx].BoxName == id.split('-')[1]) {
                                    //Options[inx].Answervalue = dataText;
                                   //Options[inx].leftcoord
                                    $scope.DraggedList.push({ "id": id.split('-')[1], "Value": dataText, "topcoord": Options[inx].topcoord, "leftcoord":x.left  });
                                  
                                   $('#' + $scope.dragableId).remove();
                                   //$('#' + id).remove();
                                }
                            });
                        }
                    });
                    //$scope.quiznumber++;
                    //$scope.$apply();
                }

            });
           
        },
        function (error) {
            console.log(error.statusText)
        }
        )
        $scope.$apply();
    };

    $scope.handleDragOver = function (e) {
        e.preventDefault(); // Necessary. Allows us to drop.
        e.dataTransfer.dropEffect = 'move';  // See the section on the DataTransfer object.
        return false;
    };

    $scope.resetQuiz=function()
    {
        $scope.DraggedList = [];
        $scope.GetQuizByTopic();
    }

    $scope.ReviewAttempt=function()
    {

    }

}]

);

AIA.directive('draggable', function () {
    return {
        link: function ($scope, element, attrs) {
            element[0].addEventListener('dragstart', $scope.handleDragStart, false);
            element[0].addEventListener('dragend', $scope.handleDragEnd, false);
        }
    }
});

AIA.directive('droppable', function () {
    return {
        link: function ($scope, element, attrs) {
            element[0].addEventListener('drop', $scope.handleDrop, false);
            element[0].addEventListener('dragover', $scope.handleDragOver, false);
        }
    }
});