|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var directive = {}; |
| 4 | +var service = { value: {} }; |
| 5 | + |
| 6 | +var DEPENDENCIES = { |
| 7 | + 'angular.js': 'http://code.angularjs.org/' + angular.version.full + '/angular.min.js', |
| 8 | + 'angular-resource.js': 'http://code.angularjs.org/' + angular.version.full + '/angular-resource.min.js', |
| 9 | + 'angular-route.js': 'http://code.angularjs.org/' + angular.version.full + '/angular-route.min.js', |
| 10 | + 'angular-animate.js': 'http://code.angularjs.org/' + angular.version.full + '/angular-animate.min.js', |
| 11 | + 'angular-sanitize.js': 'http://code.angularjs.org/' + angular.version.full + '/angular-sanitize.min.js', |
| 12 | + 'angular-cookies.js': 'http://code.angularjs.org/' + angular.version.full + '/angular-cookies.min.js' |
| 13 | +}; |
| 14 | + |
| 15 | + |
| 16 | +function escape(text) { |
| 17 | + return text. |
| 18 | + replace(/\&/g, '&'). |
| 19 | + replace(/\</g, '<'). |
| 20 | + replace(/\>/g, '>'). |
| 21 | + replace(/"/g, '"'); |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * http://stackoverflow.com/questions/451486/pre-tag-loses-line-breaks-when-setting-innerhtml-in-ie |
| 26 | + * http://stackoverflow.com/questions/195363/inserting-a-newline-into-a-pre-tag-ie-javascript |
| 27 | + */ |
| 28 | +function setHtmlIe8SafeWay(element, html) { |
| 29 | + var newElement = angular.element('<pre>' + html + '</pre>'); |
| 30 | + |
| 31 | + element.empty(); |
| 32 | + element.append(newElement.contents()); |
| 33 | + return element; |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +directive.jsFiddle = function(getEmbeddedTemplate, escape, script) { |
| 38 | + return { |
| 39 | + terminal: true, |
| 40 | + link: function(scope, element, attr) { |
| 41 | + var name = '', |
| 42 | + stylesheet = '<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">\n', |
| 43 | + fields = { |
| 44 | + html: '', |
| 45 | + css: '', |
| 46 | + js: '' |
| 47 | + }; |
| 48 | + |
| 49 | + angular.forEach(attr.jsFiddle.split(' '), function(file, index) { |
| 50 | + var fileType = file.split('.')[1]; |
| 51 | + |
| 52 | + if (fileType == 'html') { |
| 53 | + if (index == 0) { |
| 54 | + fields[fileType] += |
| 55 | + '<div ng-app' + (attr.module ? '="' + attr.module + '"' : '') + '>\n' + |
| 56 | + getEmbeddedTemplate(file, 2); |
| 57 | + } else { |
| 58 | + fields[fileType] += '\n\n\n <!-- CACHE FILE: ' + file + ' -->\n' + |
| 59 | + ' <script type="text/ng-template" id="' + file + '">\n' + |
| 60 | + getEmbeddedTemplate(file, 4) + |
| 61 | + ' </script>\n'; |
| 62 | + } |
| 63 | + } else { |
| 64 | + fields[fileType] += getEmbeddedTemplate(file) + '\n'; |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + fields.html += '</div>\n'; |
| 69 | + |
| 70 | + setHtmlIe8SafeWay(element, |
| 71 | + '<form class="jsfiddle" method="post" action="http://jsfiddle.net/api/post/library/pure/" target="_blank">' + |
| 72 | + hiddenField('title', 'AngularJS Example: ' + name) + |
| 73 | + hiddenField('css', '</style> <!-- Ugly Hack due to jsFiddle issue: http://goo.gl/BUfGZ --> \n' + |
| 74 | + stylesheet + |
| 75 | + script.angular + |
| 76 | + (attr.resource ? script.resource : '') + |
| 77 | + '<style>\n' + |
| 78 | + fields.css) + |
| 79 | + hiddenField('html', fields.html) + |
| 80 | + hiddenField('js', fields.js) + |
| 81 | + '<button class="btn btn-primary"><i class="icon-white icon-pencil"></i> Edit Me</button>' + |
| 82 | + '</form>'); |
| 83 | + |
| 84 | + function hiddenField(name, value) { |
| 85 | + return '<input type="hidden" name="' + name + '" value="' + escape(value) + '">'; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | + |
| 92 | +directive.code = function() { |
| 93 | + return {restrict: 'E', terminal: true}; |
| 94 | +}; |
| 95 | + |
| 96 | + |
| 97 | +directive.prettyprint = ['reindentCode', function(reindentCode) { |
| 98 | + return { |
| 99 | + restrict: 'C', |
| 100 | + compile: function(element) { |
| 101 | + var html = element.html(); |
| 102 | + //ensure that angular won't compile {{ curly }} values |
| 103 | + html = html.replace(/\{\{/g, '<span>{{</span>') |
| 104 | + .replace(/\}\}/g, '<span>}}</span>'); |
| 105 | + if (window.RUNNING_IN_NG_TEST_RUNNER) { |
| 106 | + element.html(html); |
| 107 | + } |
| 108 | + else { |
| 109 | + element.html(window.prettyPrintOne(reindentCode(html), undefined, true)); |
| 110 | + } |
| 111 | + } |
| 112 | + }; |
| 113 | +}]; |
| 114 | + |
| 115 | + |
| 116 | +directive.ngSetText = ['getEmbeddedTemplate', function(getEmbeddedTemplate) { |
| 117 | + return { |
| 118 | + restrict: 'CA', |
| 119 | + priority: 10, |
| 120 | + compile: function(element, attr) { |
| 121 | + setHtmlIe8SafeWay(element, escape(getEmbeddedTemplate(attr.ngSetText))); |
| 122 | + } |
| 123 | + } |
| 124 | +}] |
| 125 | + |
| 126 | + |
| 127 | +directive.ngHtmlWrap = ['reindentCode', 'templateMerge', function(reindentCode, templateMerge) { |
| 128 | + return { |
| 129 | + compile: function(element, attr) { |
| 130 | + var properties = { |
| 131 | + head: '', |
| 132 | + module: '', |
| 133 | + body: element.text() |
| 134 | + }, |
| 135 | + html = "<!doctype html>\n<html ng-app{{module}}>\n <head>\n{{head:4}} </head>\n <body>\n{{body:4}} </body>\n</html>"; |
| 136 | + |
| 137 | + angular.forEach((attr.ngHtmlWrap || '').split(' '), function(dep) { |
| 138 | + if (!dep) return; |
| 139 | + dep = DEPENDENCIES[dep] || dep; |
| 140 | + |
| 141 | + var ext = dep.split(/\./).pop(); |
| 142 | + |
| 143 | + if (ext == 'css') { |
| 144 | + properties.head += '<link rel="stylesheet" href="' + dep + '" type="text/css">\n'; |
| 145 | + } else if(ext == 'js') { |
| 146 | + properties.head += '<script src="' + dep + '"></script>\n'; |
| 147 | + } else { |
| 148 | + properties.module = '="' + dep + '"'; |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + setHtmlIe8SafeWay(element, escape(templateMerge(html, properties))); |
| 153 | + } |
| 154 | + } |
| 155 | +}]; |
| 156 | + |
| 157 | + |
| 158 | +directive.ngSetHtml = ['getEmbeddedTemplate', function(getEmbeddedTemplate) { |
| 159 | + return { |
| 160 | + restrict: 'CA', |
| 161 | + priority: 10, |
| 162 | + compile: function(element, attr) { |
| 163 | + setHtmlIe8SafeWay(element, getEmbeddedTemplate(attr.ngSetHtml)); |
| 164 | + } |
| 165 | + } |
| 166 | +}]; |
| 167 | + |
| 168 | + |
| 169 | +directive.ngEvalJavascript = ['getEmbeddedTemplate', function(getEmbeddedTemplate) { |
| 170 | + return { |
| 171 | + compile: function (element, attr) { |
| 172 | + var fileNames = attr.ngEvalJavascript.split(' '); |
| 173 | + angular.forEach(fileNames, function(fileName) { |
| 174 | + var script = getEmbeddedTemplate(fileName); |
| 175 | + try { |
| 176 | + if (window.execScript) { // IE |
| 177 | + window.execScript(script || '""'); // IE complains when evaling empty string |
| 178 | + } else { |
| 179 | + window.eval(script + '//@ sourceURL=' + fileName); |
| 180 | + } |
| 181 | + } catch (e) { |
| 182 | + if (window.console) { |
| 183 | + window.console.log(script, '\n', e); |
| 184 | + } else { |
| 185 | + window.alert(e); |
| 186 | + } |
| 187 | + } |
| 188 | + }); |
| 189 | + } |
| 190 | + }; |
| 191 | +}]; |
| 192 | + |
| 193 | + |
| 194 | +directive.ngEmbedApp = ['$templateCache', '$browser', '$rootScope', '$location', '$sniffer', '$animate', '$exceptionHandler', |
| 195 | + function($templateCache, $browser, docsRootScope, $location, $sniffer, $animate, $exceptionHandler) { |
| 196 | + return { |
| 197 | + terminal: true, |
| 198 | + link: function(scope, element, attrs) { |
| 199 | + var modules = ['ngAnimate'], |
| 200 | + embedRootScope, |
| 201 | + deregisterEmbedRootScope; |
| 202 | + |
| 203 | + modules.push(['$provide', function($provide) { |
| 204 | + $provide.value('$templateCache', $templateCache); |
| 205 | + $provide.value('$anchorScroll', angular.noop); |
| 206 | + $provide.value('$browser', $browser); |
| 207 | + $provide.value('$sniffer', $sniffer); |
| 208 | + //https://github.com/angular-ui/bootstrap/issues/4315 |
| 209 | + //$provide.value('$animate', $animate); |
| 210 | + $provide.provider('$location', function() { |
| 211 | + this.$get = ['$rootScope', function($rootScope) { |
| 212 | + docsRootScope.$on('$locationChangeSuccess', function(event, oldUrl, newUrl) { |
| 213 | + $rootScope.$broadcast('$locationChangeSuccess', oldUrl, newUrl); |
| 214 | + }); |
| 215 | + return $location; |
| 216 | + }]; |
| 217 | + this.html5Mode = angular.noop; |
| 218 | + this.hashPrefix = function () { |
| 219 | + return ''; |
| 220 | + }; |
| 221 | + }); |
| 222 | + |
| 223 | + $provide.decorator('$rootScope', ['$delegate', function($delegate) { |
| 224 | + embedRootScope = $delegate; |
| 225 | + |
| 226 | + // Since we are teleporting the $animate service, which relies on the $$postDigestQueue |
| 227 | + // we need the embedded scope to use the same $$postDigestQueue as the outer scope |
| 228 | + function docsRootDigest() { |
| 229 | + var postDigestQueue = docsRootScope.$$postDigestQueue; |
| 230 | + while (postDigestQueue.length) { |
| 231 | + try { |
| 232 | + postDigestQueue.shift()(); |
| 233 | + } catch (e) { |
| 234 | + $exceptionHandler(e); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + embedRootScope.$watch(function () { |
| 239 | + embedRootScope.$$postDigest(docsRootDigest); |
| 240 | + }) |
| 241 | + |
| 242 | + deregisterEmbedRootScope = docsRootScope.$watch(function embedRootScopeDigestWatch() { |
| 243 | + embedRootScope.$digest(); |
| 244 | + }); |
| 245 | + |
| 246 | + return embedRootScope; |
| 247 | + }]); |
| 248 | + }]); |
| 249 | + if (attrs.ngEmbedApp) modules.push(attrs.ngEmbedApp); |
| 250 | + |
| 251 | + element.on('click', function(event) { |
| 252 | + if (event.target.attributes.getNamedItem('ng-click')) { |
| 253 | + event.preventDefault(); |
| 254 | + } |
| 255 | + }); |
| 256 | + |
| 257 | + element.bind('$destroy', function() { |
| 258 | + deregisterEmbedRootScope(); |
| 259 | + embedRootScope.$destroy(); |
| 260 | + }); |
| 261 | + |
| 262 | + element.data('$injector', null); |
| 263 | + angular.bootstrap(element, modules); |
| 264 | + } |
| 265 | + }; |
| 266 | +}]; |
| 267 | + |
| 268 | +service.reindentCode = function() { |
| 269 | + return function (text, spaces) { |
| 270 | + if (!text) return text; |
| 271 | + var lines = text.split(/\r?\n/); |
| 272 | + var prefix = ' '.substr(0, spaces || 0); |
| 273 | + var i; |
| 274 | + |
| 275 | + // remove any leading blank lines |
| 276 | + while (lines.length && lines[0].match(/^\s*$/)) lines.shift(); |
| 277 | + // remove any trailing blank lines |
| 278 | + while (lines.length && lines[lines.length - 1].match(/^\s*$/)) lines.pop(); |
| 279 | + var minIndent = 999; |
| 280 | + for (i = 0; i < lines.length; i++) { |
| 281 | + var line = lines[0]; |
| 282 | + var reindentCode = line.match(/^\s*/)[0]; |
| 283 | + if (reindentCode !== line && reindentCode.length < minIndent) { |
| 284 | + minIndent = reindentCode.length; |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + for (i = 0; i < lines.length; i++) { |
| 289 | + lines[i] = prefix + lines[i].substring(minIndent); |
| 290 | + } |
| 291 | + lines.push(''); |
| 292 | + return lines.join('\n'); |
| 293 | + } |
| 294 | +}; |
| 295 | + |
| 296 | +service.templateMerge = ['reindentCode', function(indentCode) { |
| 297 | + return function(template, properties) { |
| 298 | + return template.replace(/\{\{(\w+)(?:\:(\d+))?\}\}/g, function(_, key, indent) { |
| 299 | + var value = properties[key]; |
| 300 | + |
| 301 | + if (indent) { |
| 302 | + value = indentCode(value, indent); |
| 303 | + } |
| 304 | + |
| 305 | + return value == undefined ? '' : value; |
| 306 | + }); |
| 307 | + }; |
| 308 | +}]; |
| 309 | + |
| 310 | +service.getEmbeddedTemplate = ['reindentCode', function(reindentCode) { |
| 311 | + return function (id) { |
| 312 | + var element = document.getElementById(id); |
| 313 | + |
| 314 | + if (!element) { |
| 315 | + return null; |
| 316 | + } |
| 317 | + |
| 318 | + return reindentCode(angular.element(element).html(), 0); |
| 319 | + } |
| 320 | +}]; |
| 321 | + |
| 322 | + |
| 323 | +angular.module('bootstrapPrettify', []).directive(directive).factory(service); |
0 commit comments