{"size": 5593, "ext": "js", "max_stars_repo_path": "routes/treeNodes.js", "max_stars_repo_name": "atezaz/VisPeerAssessment", "max_stars_repo_stars_event_min_datetime": "2016-06-25T14:15:30.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-17T02:41:48.000Z", "max_issues_repo_path": "routes/treeNodes.js", "max_issues_repo_name": "atezaz/VisPeerAssessment", "max_issues_repo_issues_event_min_datetime": "2020-03-16T10:26:34.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-02T12:43:36.000Z", "max_forks_repo_path": "routes/treeNodes.js", "max_forks_repo_name": "ude-soco/CourseMapper", "max_forks_repo_forks_event_min_datetime": "2017-08-23T11:28:43.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-19T16:51:04.000Z"}
{"max_stars_count": 12, "max_issues_count": 8, "max_forks_count": 4, "avg_line_length": 29.9090909091, "max_line_length": 105, "alphanum_fraction": 0.5238691221}
var express = require('express');
var config = require('config');
var appRoot = require('app-root-path');
var CourseController = require(appRoot + '/modules/catalogs/course.controller.js');
var NodeController = require(appRoot + '/modules/trees/index.js');
var TabsController = require(appRoot + '/modules/tabs/tabs.controller.js');
var helper = require(appRoot + '/libs/core/generalLibs.js');
var debug = require('debug')('cm:route');
var moment = require('moment');
var mongoose = require('mongoose');
var router = express.Router();
var theme = config.get('theme');
var async = require('asyncawait/async');
var await = require('asyncawait/await');
var _ = require('underscore');

// set a cookie
router.use(function (req, res, next) {
    var isInIframe = false;
    var setCookie = false;

    if (req.query.iframe === 'true') {
        isInIframe = true;
        setCookie = true;
    }
    else if (req.query.iframe === 'false') {
        isInIframe = false;
        setCookie = true;
    }

    // check if client sent cookie
    var cookie = req.cookies.isInIframe;

    // set if not set
    if (setCookie || cookie === undefined) {
        res.cookie('isInIframe', isInIframe, {maxAge: 900000, httpOnly: true});
    }

    next();
});

/**
 * partials related to tab and actionbars
 */
router.get('/treeNode/tab/:tabName', function (req, res, next) {
    res.render(theme + '/treeNode/' + req.params.tabName + '/' + req.params.tabName);
});

router.get('/treeNode/actionBar/:tabName', function (req, res, next) {
    res.render(theme + '/treeNode/' + req.params.tabName + '/' + req.params.tabName + 'ActionBar');
});

/**
 * partial for nodeDetail, (accessed by course detail page)
 */
router.get('/treeNode/:cid/nodeDetail/:nid', function (req, res, next) {
    var TC = new TabsController();
    var nod = new NodeController();
    var nid = mongoose.Types.ObjectId(req.params.nid);

    var op = async(function () {
        var ta = await(TC.getActiveTabs('contentNode')());
        var nd = await(nod.getNodeAsync()({_id: nid}));

        // to avoid returning json 404, instead, we create a mockup object that says it s already deleted
        if (!nd) {
            nd = {
                tabsActive: null,
                isDeleted: true,
                courseId: {name: ""}
            }
        }

        return {tabs: ta, tabsActive: nd.tabsActive, course: nd.courseId, treeNode: nd};
    });

    var isInIframe = (req.cookies.isInIframe === 'true');
    if (!isInIframe)
        if (req.query.iframe === 'true') {
            isInIframe = true;
        } else if (req.query.iframe === 'false') {
            isInIframe = false;
        }

    op()
        .then(function (ret) {
            var activeTabs = [];
            if (!ret.tabsActive)
                ret.tabsActive = {};

            for (var i = 0; i < ret.tabs.length; i++) {
                var tap = ret.tabs[i];
                var isActive = tap.isActive;
                if (!isActive) {
                    continue;
                }

                if (typeof(ret.tabsActive[tap.name]) != "undefined") {
                    if (!ret.tabsActive[tap.name]) {
                        continue;
                    }
                }

                activeTabs.push(tap);
            }

            var le = _.extend(ret, {activeTabs: activeTabs, isInIframe: isInIframe});
            res.render(theme + '/catalogs/nodeDetail', le);
        })
        .catch(function (err) {
            helper.resReturn(err, res);
        });
});

/**
 * full page for displaying course detail page
 */
router.get('/treeNode/:nid', function (req, res, next) {

    var TC = new TabsController();
    var nod = new NodeController();
    var nid = mongoose.Types.ObjectId(req.params.nid);

    var op = async(function () {
        var ta = await(TC.getActiveTabs('contentNode')());
        var nd = await(nod.getNodeAsync()({_id: nid}));

        // to avoid returning json 404, instead, we create a mockup object that says it s already deleted
        if (!nd) {
            nd = {
                tabsActive: null,
                isDeleted: true,
                courseId: {name: ""}
            }
        }

        return {tabs: ta, tabsActive: nd.tabsActive, course: nd.courseId};
    });

    var isInIframe = (req.cookies.isInIframe === 'true');
    if (!isInIframe)
        if (req.query.iframe === 'true') {
            isInIframe = true;
        } else if (req.query.iframe === 'false') {
            isInIframe = false;
        }

    op()
        .then(function (ret) {
            var activeTabs = [];
            if (!ret.tabsActive)
                ret.tabsActive = {};

            for (var i = 0; i < ret.tabs.length; i++) {
                var tap = ret.tabs[i];
                var isActive = tap.isActive;
                if (!isActive) {
                    continue;
                }

                if (typeof(ret.tabsActive[tap.name]) != "undefined") {
                    if (!ret.tabsActive[tap.name]) {
                        continue;
                    }
                }

                activeTabs.push(tap);
            }

            res.render(theme + '/catalogs/course', {
                title: ret.course.name,
                course: ret.course,
                user: req.user,
                moment: moment,
                isInNodeDetailPage: true,
                activeTabs: activeTabs,
                isInIframe: isInIframe
            });
        })
        .catch(function (err) {
            helper.resReturn(err, res);
        });

});

module.exports = router;
