all files / app/main/components/material-docs/demo/ demo.js

17.78% Statements 8/45
0% Branches 0/26
7.14% Functions 1/14
17.78% Lines 8/45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142                                                                                                                                                                                                                                                                            
(function ()
{
    'use strict';
    angular
        .module('app.components.material-docs')
        .directive('docsDemo', docsDemo)
        .directive('demoFile', demoFile)
        .filter('toHtml', toHtml)
        .directive('layoutAlign', function ()
        {
            return angular.noop;
        })
        .directive('layout', function ()
        {
            return angular.noop;
        });
 
    /** @ngInject */
    function docsDemo($mdUtil)
    {
        return {
            restrict        : 'E',
            scope           : true,
            templateUrl     : 'app/main/components/material-docs/demo/docs-demo.tmpl.html',
            transclude      : true,
            controller      :DocsDemoCtrl,
            controllerAs    : 'demoCtrl',
            bindToController: true
        };
 
        function DocsDemoCtrl($scope, $element, $attrs, $interpolate)
        {
            var self = this;
 
            self.interpolateCode = angular.isDefined($attrs.interpolateCode);
            self.demoId = $interpolate($attrs.demoId || '')($scope.$parent);
            self.demoTitle = $interpolate($attrs.demoTitle || '')($scope.$parent);
            self.demoModule = $interpolate($attrs.demoModule || '')($scope.$parent);
            self.files = {
                css : [],
                js  : [],
                html: []
            };
 
            self.addFile = function (name, contentsPromise)
            {
                var file = {
                    name           : convertName(name),
                    contentsPromise: contentsPromise,
                    fileType       : name.split('.').pop()
                };
                contentsPromise.then(function (contents)
                {
                    file.contents = contents;
                });
 
                if ( name === 'index.html' )
                {
                    self.files.index = file;
                }
                else if ( name === 'readme.html' )
                {
                    self.demoDescription = file;
                }
                else
                {
                    self.files[file.fileType] = self.files[file.fileType] || [];
                    self.files[file.fileType].push(file);
                }
 
                self.orderedFiles = []
                    .concat(self.files.index || [])
                    .concat(self.files.js || [])
                    .concat(self.files.css || [])
                    .concat(self.files.html || []);
 
            };
 
            self.editOnCodepen = function ()
            {
                codepen.editOnCodepen({
                    title : self.demoTitle,
                    files : self.files,
                    id    : self.demoId,
                    module: self.demoModule
                });
            };
 
            function convertName(name)
            {
                switch ( name )
                {
                    case "index.html" :
                        return "HTML";
                    case "script.js" :
                        return "JS";
                    case "style.css" :
                        return "CSS";
                    default :
                        return name;
                }
            }
 
        }
    }
 
    /** @ngInject */
    function demoFile($q, $interpolate)
    {
        return {
            restrict: 'E',
            require : '^docsDemo',
            compile : compile
        };
 
        function compile(element, attr)
        {
            var contentsAttr = attr.contents;
            var html = element.html();
            var name = attr.name;
            element.contents().remove();
 
            return function postLink(scope, element, attr, docsDemoCtrl)
            {
                docsDemoCtrl.addFile(
                    $interpolate(name)(scope),
                    $q.when(scope.$eval(contentsAttr) || html)
                );
                element.remove();
            };
        }
    }
 
    /** @ngInject */
    function toHtml($sce)
    {
        return function (str)
        {
            return $sce.trustAsHtml(str);
        };
    }
})();