509 | Snippet:
510 |
511 |
512 | Filter |
513 | Source |
514 | Rendered |
515 |
516 |
517 | linky filter |
518 |
519 | <div ng-bind-html="snippet | linky"> </div>
520 | |
521 |
522 |
523 | |
524 |
525 |
526 | linky target |
527 |
528 | <div ng-bind-html="snippetWithTarget | linky:'_blank'"> </div>
529 | |
530 |
531 |
532 | |
533 |
534 |
535 | no filter |
536 | <div ng-bind="snippet"> </div> |
537 | |
538 |
539 |
540 |
541 |
542 | it('should linkify the snippet with urls', function() {
543 | expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()).
544 | toBe('Pretty text with some links: http://angularjs.org/, us@somewhere.org, ' +
545 | 'another@somewhere.org, and one more: ftp://127.0.0.1/.');
546 | expect(element.all(by.css('#linky-filter a')).count()).toEqual(4);
547 | });
548 |
549 | it('should not linkify snippet without the linky filter', function() {
550 | expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText()).
551 | toBe('Pretty text with some links: http://angularjs.org/, mailto:us@somewhere.org, ' +
552 | 'another@somewhere.org, and one more: ftp://127.0.0.1/.');
553 | expect(element.all(by.css('#escaped-html a')).count()).toEqual(0);
554 | });
555 |
556 | it('should update', function() {
557 | element(by.model('snippet')).clear();
558 | element(by.model('snippet')).sendKeys('new http://link.');
559 | expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()).
560 | toBe('new http://link.');
561 | expect(element.all(by.css('#linky-filter a')).count()).toEqual(1);
562 | expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText())
563 | .toBe('new http://link.');
564 | });
565 |
566 | it('should work with the target property', function() {
567 | expect(element(by.id('linky-target')).
568 | element(by.binding("snippetWithTarget | linky:'_blank'")).getText()).
569 | toBe('http://angularjs.org/');
570 | expect(element(by.css('#linky-target a')).getAttribute('target')).toEqual('_blank');
571 | });
572 |
573 |
574 | */
575 | angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
576 | var LINKY_URL_REGEXP =
577 | /((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>]/,
578 | MAILTO_REGEXP = /^mailto:/;
579 |
580 | return function(text, target) {
581 | if (!text) return text;
582 | var match;
583 | var raw = text;
584 | var html = [];
585 | var url;
586 | var i;
587 | while ((match = raw.match(LINKY_URL_REGEXP))) {
588 | // We can not end in these as they are sometimes found at the end of the sentence
589 | url = match[0];
590 | // if we did not match ftp/http/mailto then assume mailto
591 | if (match[2] == match[3]) url = 'mailto:' + url;
592 | i = match.index;
593 | addText(raw.substr(0, i));
594 | addLink(url, match[0].replace(MAILTO_REGEXP, ''));
595 | raw = raw.substring(i + match[0].length);
596 | }
597 | addText(raw);
598 | return $sanitize(html.join(''));
599 |
600 | function addText(text) {
601 | if (!text) {
602 | return;
603 | }
604 | html.push(sanitizeText(text));
605 | }
606 |
607 | function addLink(url, text) {
608 | html.push('
');
617 | addText(text);
618 | html.push('');
619 | }
620 | };
621 | }]);
622 |
623 |
624 | })(window, window.angular);
625 |
--------------------------------------------------------------------------------
/src/main/resources/public/scripts/app.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Created by shekhargulati on 10/06/14.
3 | */
4 |
5 | var app = angular.module('todoapp', [
6 | 'ngCookies',
7 | 'ngResource',
8 | 'ngSanitize',
9 | 'ngRoute'
10 | ]);
11 |
12 | app.config(function ($routeProvider) {
13 | $routeProvider.when('/', {
14 | templateUrl: 'views/list.html',
15 | controller: 'ListCtrl'
16 | }).when('/create', {
17 | templateUrl: 'views/create.html',
18 | controller: 'CreateCtrl'
19 | }).otherwise({
20 | redirectTo: '/'
21 | })
22 | });
23 |
24 | app.controller('ListCtrl', function ($scope, $http) {
25 | $http.get('/api/v1/todos').success(function (data) {
26 | $scope.todos = data;
27 | }).error(function (data, status) {
28 | console.log('Error ' + data)
29 | })
30 |
31 | $scope.todoStatusChanged = function (todo) {
32 | console.log(todo);
33 | $http.put('/api/v1/todos/' + todo.id, todo).success(function (data) {
34 | console.log('status changed');
35 | }).error(function (data, status) {
36 | console.log('Error ' + data)
37 | })
38 | }
39 | });
40 |
41 | app.controller('CreateCtrl', function ($scope, $http, $location) {
42 | $scope.todo = {
43 | done: false
44 | };
45 |
46 | $scope.createTodo = function () {
47 | console.log($scope.todo);
48 | $http.post('/api/v1/todos', $scope.todo).success(function (data) {
49 | $location.path('/');
50 | }).error(function (data, status) {
51 | console.log('Error ' + data)
52 | })
53 | }
54 | });
--------------------------------------------------------------------------------
/src/main/resources/public/views/create.html:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/main/resources/public/views/list.html:
--------------------------------------------------------------------------------
1 |
3 |
4 |
--------------------------------------------------------------------------------