Add config folder to be able to change config
[clamp.git] / src / main / resources / META-INF / resources / designer / lib / angular-highlightjs.js
1 /*! angular-highlightjs
2 version: 0.4.1
3 build date: 2015-02-03
4 author: Chih-Hsuan Fan
5 https://github.com/pc035860/angular-highlightjs.git */
6
7 /* commonjs package manager support (eg componentjs) */
8 if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){
9   module.exports = 'hljs';
10 }
11
12 (function (window, angular, undefined) {
13 /*global angular*/
14
15 function shouldHighlightStatics(attrs) {
16   var should = true;
17   angular.forEach([
18     'source', 'include'
19   ], function (name) {
20     if (attrs[name]) {
21       should = false;
22     }
23   });
24   return should;
25 }
26
27
28 var ngModule = angular.module('hljs', []);
29
30 /**
31  * hljsService service
32  */
33 ngModule.provider('hljsService', function () {
34   var _hljsOptions = {};
35
36   return {
37     setOptions: function (options) {
38       angular.extend(_hljsOptions, options);
39     },
40     getOptions: function () {
41       return angular.copy(_hljsOptions);
42     },
43     $get: ['$window', function ($window) {
44       ($window.hljs.configure || angular.noop)(_hljsOptions);
45       return $window.hljs;
46     }]
47   };
48 });
49
50 /**
51  * hljsCache service
52  */
53 ngModule.factory('hljsCache', [
54          '$cacheFactory',
55 function ($cacheFactory) {
56   return $cacheFactory('hljsCache');
57 }]);
58
59 /**
60  * HljsCtrl controller
61  */
62 ngModule.controller('HljsCtrl', [
63                   'hljsCache', 'hljsService',
64 function HljsCtrl (hljsCache,   hljsService) {
65   var ctrl = this;
66
67   var _elm = null,
68       _lang = null,
69       _code = null,
70       _hlCb = null;
71
72   ctrl.init = function (codeElm) {
73     _elm = codeElm;
74   };
75
76   ctrl.setLanguage = function (lang) {
77     _lang = lang;
78
79     if (_code) {
80       ctrl.highlight(_code);
81     }
82   };
83
84   ctrl.highlightCallback = function (cb) {
85     _hlCb = cb;
86   };
87
88   ctrl.highlight = function (code) {
89     if (!_elm) {
90       return;
91     }
92
93     var res, cacheKey;
94
95     _code = code;
96
97     if (_lang) {
98       // language specified
99       cacheKey = ctrl._cacheKey(_lang, _code);
100       res = hljsCache.get(cacheKey);
101
102       if (!res) {
103         res = hljsService.highlight(_lang, hljsService.fixMarkup(_code), true);
104         hljsCache.put(cacheKey, res);
105       }
106     }
107     else {
108       // language auto-detect
109       cacheKey = ctrl._cacheKey(_code);
110       res = hljsCache.get(cacheKey);
111
112       if (!res) {
113         res = hljsService.highlightAuto(hljsService.fixMarkup(_code));
114         hljsCache.put(cacheKey, res);
115       }
116     }
117
118     _elm.html(res.value);
119     // language as class on the <code> tag
120     _elm.addClass(res.language);
121
122     if (_hlCb !== null && angular.isFunction(_hlCb)) {
123       _hlCb();
124     }
125   };
126
127   ctrl.clear = function () {
128     if (!_elm) {
129       return;
130     }
131     _code = null;
132     _elm.text('');
133   };
134
135   ctrl.release = function () {
136     _elm = null;
137   };
138
139   ctrl._cacheKey = function () {
140     var args = Array.prototype.slice.call(arguments),
141         glue = "!angular-highlightjs!";
142     return args.join(glue);
143   };
144 }]);
145
146
147 var hljsDir, languageDirFactory, sourceDirFactory, includeDirFactory;
148
149 /**
150  * hljs directive
151  */
152 hljsDir = ['$compile', '$parse', function ($compile, $parse) {
153   return {
154     restrict: 'EA',
155     controller: 'HljsCtrl',
156     compile: function(tElm, tAttrs, transclude) {
157       // get static code
158       // strip the starting "new line" character
159       var staticHTML = tElm[0].innerHTML.replace(/^(\r\n|\r|\n)/m, ''),
160           staticText = tElm[0].textContent.replace(/^(\r\n|\r|\n)/m, '');
161
162       // put template
163       tElm.html('<pre><code class="hljs"></code></pre>');
164
165       return function postLink(scope, iElm, iAttrs, ctrl) {
166         var compileCheck, escapeCheck;
167
168         if (angular.isDefined(iAttrs.compile)) {
169           compileCheck = $parse(iAttrs.compile);
170         }
171
172         if (angular.isDefined(iAttrs.escape)) {
173           escapeCheck = $parse(iAttrs.escape);
174         } else if (angular.isDefined(iAttrs.noEscape)) {
175           escapeCheck = $parse('false');
176         }
177
178         ctrl.init(iElm.find('code'));
179
180         if (iAttrs.onhighlight) {
181           ctrl.highlightCallback(function () {
182             scope.$eval(iAttrs.onhighlight);
183           });
184         }
185
186         if ((staticHTML || staticText) && shouldHighlightStatics(iAttrs)) {
187
188           var code;
189
190           // Auto-escape check
191           // default to "true"
192           if (escapeCheck && !escapeCheck(scope)) {
193             code = staticText;
194           }
195           else {
196             code = staticHTML;
197           }
198
199           ctrl.highlight(code);
200
201           // Check if the highlight result needs to be compiled
202           if (compileCheck && compileCheck(scope)) {
203             // compile the new DOM and link it to the current scope.
204             // NOTE: we only compile .childNodes so that
205             // we don't get into infinite loop compiling ourselves
206             $compile(iElm.find('code').contents())(scope);
207           }
208         }
209
210         scope.$on('$destroy', function () {
211           ctrl.release();
212         });
213       };
214     }
215   };
216 }];
217
218 /**
219  * language directive
220  */
221 languageDirFactory = function (dirName) {
222   return [function () {
223     return {
224       require: '?hljs',
225       restrict: 'A',
226       link: function (scope, iElm, iAttrs, ctrl) {
227         if (!ctrl) {
228           return;
229         }      
230         iAttrs.$observe(dirName, function (lang) {
231           if (angular.isDefined(lang)) {
232             ctrl.setLanguage(lang);
233           }
234         });
235       }
236     };
237   }];
238 };
239
240 /**
241  * source directive
242  */
243 sourceDirFactory = function (dirName) {
244   return ['$compile', '$parse', function ($compile, $parse) {
245     return {
246       require: '?hljs',
247       restrict: 'A',
248       link: function(scope, iElm, iAttrs, ctrl) {
249         var compileCheck;
250
251         if (!ctrl) {
252           return;
253         }
254
255         if (angular.isDefined(iAttrs.compile)) {
256           compileCheck = $parse(iAttrs.compile);
257         }
258
259         scope.$watch(iAttrs[dirName], function (newCode, oldCode) {
260           if (newCode) {
261             ctrl.highlight(newCode);
262
263             // Check if the highlight result needs to be compiled
264             if (compileCheck && compileCheck(scope)) {
265               // compile the new DOM and link it to the current scope.
266               // NOTE: we only compile .childNodes so that
267               // we don't get into infinite loop compiling ourselves
268               $compile(iElm.find('code').contents())(scope);
269             }
270           }
271           else {
272             ctrl.clear();
273           }
274         });
275       }
276     };
277   }];
278 };
279
280 /**
281  * include directive
282  */
283 includeDirFactory = function (dirName) {
284   return [
285              '$http', '$templateCache', '$q', '$compile', '$parse',
286     function ($http,   $templateCache,   $q,   $compile,   $parse) {
287       return {
288         require: '?hljs',
289         restrict: 'A',
290         compile: function(tElm, tAttrs, transclude) {
291           var srcExpr = tAttrs[dirName];
292
293           return function postLink(scope, iElm, iAttrs, ctrl) {
294             var changeCounter = 0, compileCheck;
295
296             if (!ctrl) {
297               return;
298             }
299
300             if (angular.isDefined(iAttrs.compile)) {
301               compileCheck = $parse(iAttrs.compile);
302             }
303
304             scope.$watch(srcExpr, function (src) {
305               var thisChangeId = ++changeCounter;
306
307               if (src && angular.isString(src)) {
308                 var templateCachePromise, dfd;
309
310                 templateCachePromise = $templateCache.get(src);
311                 if (!templateCachePromise) {
312                   dfd = $q.defer();
313                   $http.get(src, {
314                     cache: $templateCache,
315                     transformResponse: function(data, headersGetter) {
316                       // Return the raw string, so $http doesn't parse it
317                       // if it's json.
318                       return data;
319                     }
320                   }).success(function (code) {
321                     if (thisChangeId !== changeCounter) {
322                       return;
323                     }
324                     dfd.resolve(code);
325                   }).error(function() {
326                     if (thisChangeId === changeCounter) {
327                       ctrl.clear();
328                     }
329                     dfd.resolve();
330                   });
331                   templateCachePromise = dfd.promise;
332                 }
333
334                 $q.when(templateCachePromise)
335                 .then(function (code) {
336                   if (!code) {
337                     return;
338                   }
339
340                   // $templateCache from $http
341                   if (angular.isArray(code)) {
342                     // 1.1.5
343                     code = code[1];
344                   }
345                   else if (angular.isObject(code)) {
346                     // 1.0.7
347                     code = code.data;
348                   }
349
350                   code = code.replace(/^(\r\n|\r|\n)/m, '');
351                   ctrl.highlight(code);
352
353                   // Check if the highlight result needs to be compiled
354                   if (compileCheck && compileCheck(scope)) {
355                     // compile the new DOM and link it to the current scope.
356                     // NOTE: we only compile .childNodes so that
357                     // we don't get into infinite loop compiling ourselves
358                     $compile(iElm.find('code').contents())(scope);
359                   }
360                 });
361               }
362               else {
363                 ctrl.clear();
364               }
365             });
366           };
367         }
368       };
369   }];
370 };
371
372 /**
373  * Add directives
374  */
375 ngModule
376 .directive('hljs', hljsDir)
377 .directive('language', languageDirFactory('language'))
378 .directive('source', sourceDirFactory('source'))
379 .directive('include', includeDirFactory('include'));
380 })(window, window.angular);