@@ -361,10 +361,10 @@ redirect to regular / hashbang url, as this conversion happens only during parsi |
361 | 361 | In these examples we use `<base href="/base/index.html" />` |
362 | 362 |
|
363 | 363 | #### Browser in HTML5 mode |
364 | | -<example module="html5-mode"> |
| 364 | +<example module="html5-mode" name="location-html5-mode"> |
365 | 365 | <file name="index.html"> |
366 | 366 | <div ng-controller="LocationController"> |
367 | | - <div ng-address-bar browser="html5"></div><br><br> |
| 367 | + <div ng-address-bar></div><br><br> |
368 | 368 | <div> |
369 | 369 | $location.protocol() = <span ng-bind="$location.protocol()"></span> <br> |
370 | 370 | $location.host() = <span ng-bind="$location.host()"></span> <br> |
@@ -380,93 +380,93 @@ In these examples we use `<base href="/base/index.html" />` |
380 | 380 | </div> |
381 | 381 | </div> |
382 | 382 | </file> |
383 | | - <file name="script.js"> |
| 383 | + <file name="app.js"> |
| 384 | + angular.module('html5-mode', ['fake-browser', 'address-bar']) |
| 385 | + |
| 386 | + .constant('initUrl', 'http://www.example.com/base/path?a=b#h') |
| 387 | + .constant('baseHref', '/base/index.html') |
| 388 | + .value('$sniffer', { history: true }) |
| 389 | + |
| 390 | + .controller("LocationController", function($scope, $location) { |
| 391 | + $scope.$location = {}; |
| 392 | + angular.forEach("protocol host port path search hash".split(" "), function(method){ |
| 393 | + $scope.$location[method] = function(){ |
| 394 | + var result = $location[method].call($location); |
| 395 | + return angular.isObject(result) ? angular.toJson(result) : result; |
| 396 | + }; |
| 397 | + }); |
| 398 | + }) |
| 399 | + |
| 400 | + .config(function($locationProvider) { |
| 401 | + $locationProvider.html5Mode(true).hashPrefix('!'); |
| 402 | + }) |
| 403 | + |
| 404 | + .run(function($rootElement) { |
| 405 | + $rootElement.on('click', function(e) { e.stopPropagation(); }); |
| 406 | + }); |
| 407 | + </file> |
| 408 | + |
| 409 | + <file name="fakeBrowser.js"> |
384 | 410 | angular.module('fake-browser', []) |
385 | 411 |
|
386 | | - .factory('FakeBrowser', function() { |
387 | | - return function FakeBrowser(initUrl, baseHref) { |
388 | | - this.onUrlChange = function(fn) { |
| 412 | + .config(function($provide) { |
| 413 | + $provide.decorator('$browser', function($delegate, baseHref, initUrl) { |
| 414 | + |
| 415 | + $delegate.onUrlChange = function(fn) { |
389 | 416 | this.urlChange = fn; |
390 | 417 | }; |
391 | 418 |
|
392 | | - this.url = function() { |
| 419 | + $delegate.url = function() { |
393 | 420 | return initUrl; |
394 | | - }; |
| 421 | + }; |
395 | 422 |
|
396 | | - this.defer = function(fn, delay) { |
| 423 | + $delegate.defer = function(fn, delay) { |
397 | 424 | setTimeout(function() { fn(); }, delay || 0); |
398 | 425 | }; |
399 | 426 |
|
400 | | - this.baseHref = function() { |
| 427 | + $delegate.baseHref = function() { |
401 | 428 | return baseHref; |
402 | 429 | }; |
403 | 430 |
|
404 | | - this.notifyWhenOutstandingRequests = angular.noop; |
405 | | - }; |
406 | | - }); |
407 | | - |
408 | | - angular.module('html5-mode', ['fake-browser']) |
409 | | - |
410 | | - .factory('$browser', function(FakeBrowser) { |
411 | | - return new FakeBrowser('http://www.example.com/base/path?a=b#h', '/base/index.html'); |
412 | | - }) |
413 | | - |
414 | | - .value('$sniffer', { history: true }) |
415 | | - |
416 | | - .controller("LocationController", function($scope, $location) { |
417 | | - $scope.$location = {}; |
418 | | - angular.forEach("protocol host port path search hash".split(" "), function(method){ |
419 | | - $scope.$location[method] = function(){ |
420 | | - var result = $location[method].call($location); |
421 | | - return angular.isObject(result) ? angular.toJson(result) : result; |
422 | | - }; |
| 431 | + return $delegate; |
423 | 432 | }); |
424 | | - }) |
| 433 | + }); |
| 434 | + </file> |
425 | 435 |
|
| 436 | + <file name="addressBar.js"> |
| 437 | + angular.module('address-bar', []) |
426 | 438 | .directive('ngAddressBar', function($browser, $timeout) { |
427 | 439 | return { |
428 | 440 | template: 'Address: <input id="addressBar" type="text" style="width: 400px" >', |
429 | 441 | link: function(scope, element, attrs){ |
430 | 442 | var input = element.children("input"), delay; |
431 | | - |
| 443 | + |
432 | 444 | input.on('keypress keyup keydown', function(event) { |
433 | 445 | delay = (!delay ? $timeout(fireUrlChange, 250) : null); |
434 | 446 | event.stopPropagation(); |
435 | 447 | }) |
436 | 448 | .val($browser.url()); |
437 | | - |
| 449 | + |
438 | 450 | $browser.url = function(url) { |
439 | 451 | return url ? input.val(url) : input.val(); |
440 | | - }; |
| 452 | + }; |
441 | 453 |
|
442 | 454 | function fireUrlChange() { |
443 | 455 | delay = null; |
444 | 456 | $browser.urlChange(input.val()); |
445 | 457 | } |
446 | 458 | } |
447 | 459 | }; |
448 | | - }) |
449 | | - |
450 | | - .config(function($locationProvider) { |
451 | | - $locationProvider.html5Mode(true).hashPrefix('!'); |
452 | | - }) |
453 | | - |
454 | | - .run(function($rootElement) { |
455 | | - $rootElement.on('click', function(e) { |
456 | | - e.stopPropagation(); |
457 | | - }); |
458 | | - }); |
459 | | - |
| 460 | + }); |
460 | 461 | </file> |
461 | 462 |
|
462 | 463 | <file name="protractor.js" type="protractor"> |
463 | 464 |
|
464 | 465 | var addressBar = element(by.css("#addressBar")), |
465 | | - url = 'http://www.example.com/base/path?a=b#h'; |
| 466 | + url = 'http://www.example.com/base/path?a=b#h'; |
| 467 | + |
466 | 468 |
|
467 | | - |
468 | 469 | it("should show fake browser info on load", function(){ |
469 | | - browser.ignoreSynchronization = true; |
470 | 470 | expect(addressBar.getAttribute('value')).toBe(url); |
471 | 471 |
|
472 | 472 | expect(element(by.binding('$location.protocol')).getText()).toBe('http'); |
@@ -476,11 +476,9 @@ In these examples we use `<base href="/base/index.html" />` |
476 | 476 | expect(element(by.binding('$location.search')).getText()).toBe('{"a":"b"}'); |
477 | 477 | expect(element(by.binding('$location.hash')).getText()).toBe('h'); |
478 | 478 |
|
479 | | - browser.ignoreSynchronization = false; |
480 | 479 | }); |
481 | 480 |
|
482 | 481 | it("should change $location accordingly", function(){ |
483 | | - browser.ignoreSynchronization = true; |
484 | 482 | var navigation = element.all(by.css("#navigation a")); |
485 | 483 |
|
486 | 484 | navigation.get(0).click(); |
@@ -505,18 +503,17 @@ In these examples we use `<base href="/base/index.html" />` |
505 | 503 | expect(element(by.binding('$location.path')).getText()).toBe('/sec/ond'); |
506 | 504 | expect(element(by.binding('$location.search')).getText()).toBe('{"flag":true}'); |
507 | 505 | expect(element(by.binding('$location.hash')).getText()).toBe('hash'); |
508 | | - browser.ignoreSynchronization = false; |
509 | 506 | }); |
510 | 507 |
|
511 | 508 | </file> |
512 | 509 |
|
513 | 510 | </example> |
514 | 511 |
|
515 | 512 | ####Browser in HTML5 Fallback mode (Hashbang mode) |
516 | | -<example module="html5-mode"> |
| 513 | +<example module="hashbang-mode" name="location-hashbang-mode"> |
517 | 514 | <file name="index.html"> |
518 | 515 | <div ng-controller="LocationController"> |
519 | | - <div ng-address-bar browser="html5"></div><br><br> |
| 516 | + <div ng-address-bar></div><br><br> |
520 | 517 | <div> |
521 | 518 | $location.protocol() = <span ng-bind="$location.protocol()"></span> <br> |
522 | 519 | $location.host() = <span ng-bind="$location.host()"></span> <br> |
@@ -532,93 +529,96 @@ In these examples we use `<base href="/base/index.html" />` |
532 | 529 | </div> |
533 | 530 | </div> |
534 | 531 | </file> |
535 | | - <file name="script.js"> |
536 | | - angular.module('fake-browser', []) |
537 | | - |
538 | | - .factory('FakeBrowser', function() { |
539 | | - return function FakeBrowser(initUrl, baseHref) { |
540 | | - this.onUrlChange = function(fn) { |
541 | | - this.urlChange = fn; |
542 | | - }; |
| 532 | + <file name="app.js"> |
| 533 | + angular.module('hashbang-mode', ['fake-browser', 'address-bar']) |
543 | 534 |
|
544 | | - this.url = function() { |
545 | | - return initUrl; |
546 | | - }; |
547 | | - |
548 | | - this.defer = function(fn, delay) { |
549 | | - setTimeout(function() { fn(); }, delay || 0); |
550 | | - }; |
551 | | - |
552 | | - this.baseHref = function() { |
553 | | - return baseHref; |
554 | | - }; |
555 | | - |
556 | | - this.notifyWhenOutstandingRequests = angular.noop; |
557 | | - }; |
558 | | - }); |
559 | | - |
560 | | - angular.module('html5-mode', ['fake-browser']) |
| 535 | + .constant('initUrl', 'http://www.example.com/base/index.html#!/path?a=b#h') |
| 536 | + .constant('baseHref', '/base/index.html') |
| 537 | + .value('$sniffer', { history: false }) |
561 | 538 |
|
562 | | - .factory('$browser', function(FakeBrowser) { |
563 | | - return new FakeBrowser('http://www.example.com/base/index.html#!/path?a=b#h', '/base/index.html'); |
| 539 | + .config(function($locationProvider) { |
| 540 | + $locationProvider.html5Mode(true).hashPrefix('!'); |
564 | 541 | }) |
565 | 542 |
|
566 | | - .value('$sniffer', { history: false }) |
567 | | - |
568 | 543 | .controller("LocationController", function($scope, $location) { |
569 | 544 | $scope.$location = {}; |
570 | | - angular.forEach("protocol host port path search hash".split(" "), function(method){ |
| 545 | + angular.forEach("protocol host port path search hash".split(" "), function(method){ |
571 | 546 | $scope.$location[method] = function(){ |
572 | 547 | var result = $location[method].call($location); |
573 | 548 | return angular.isObject(result) ? angular.toJson(result) : result; |
574 | 549 | }; |
575 | 550 | }); |
576 | 551 | }) |
577 | 552 |
|
578 | | - .directive('ngAddressBar', function($browser) { |
| 553 | + .run(function($rootElement) { |
| 554 | + $rootElement.on('click', function(e) { |
| 555 | + e.stopPropagation(); |
| 556 | + }); |
| 557 | + }); |
| 558 | + |
| 559 | + </file> |
| 560 | + |
| 561 | + <file name="fakeBrowser.js"> |
| 562 | + angular.module('fake-browser', []) |
| 563 | + |
| 564 | + .config(function($provide) { |
| 565 | + $provide.decorator('$browser', function($delegate, baseHref, initUrl) { |
| 566 | + |
| 567 | + $delegate.onUrlChange = function(fn) { |
| 568 | + this.urlChange = fn; |
| 569 | + }; |
| 570 | + |
| 571 | + $delegate.url = function() { |
| 572 | + return initUrl; |
| 573 | + }; |
| 574 | + |
| 575 | + $delegate.defer = function(fn, delay) { |
| 576 | + setTimeout(function() { fn(); }, delay || 0); |
| 577 | + }; |
| 578 | + |
| 579 | + $delegate.baseHref = function() { |
| 580 | + return baseHref; |
| 581 | + }; |
| 582 | + |
| 583 | + return $delegate; |
| 584 | + }); |
| 585 | + }); |
| 586 | + </file> |
| 587 | + |
| 588 | + |
| 589 | + <file name="addressBar.js"> |
| 590 | + angular.module('address-bar', []) |
| 591 | + .directive('ngAddressBar', function($browser, $timeout) { |
579 | 592 | return { |
580 | 593 | template: 'Address: <input id="addressBar" type="text" style="width: 400px" >', |
581 | 594 | link: function(scope, element, attrs){ |
582 | 595 | var input = element.children("input"), delay; |
583 | | - |
| 596 | + |
584 | 597 | input.on('keypress keyup keydown', function(event) { |
585 | 598 | delay = (!delay ? $timeout(fireUrlChange, 250) : null); |
586 | 599 | event.stopPropagation(); |
587 | 600 | }) |
588 | 601 | .val($browser.url()); |
589 | | - |
| 602 | + |
590 | 603 | $browser.url = function(url) { |
591 | 604 | return url ? input.val(url) : input.val(); |
592 | | - }; |
| 605 | + }; |
593 | 606 |
|
594 | 607 | function fireUrlChange() { |
595 | 608 | delay = null; |
596 | 609 | $browser.urlChange(input.val()); |
597 | 610 | } |
598 | 611 | } |
599 | 612 | }; |
600 | | - }) |
601 | | - |
602 | | - .config(function($locationProvider) { |
603 | | - $locationProvider.html5Mode(true).hashPrefix('!'); |
604 | | - }) |
605 | | - |
606 | | - .run(function($rootElement) { |
607 | | - $rootElement.on('click', function(e) { |
608 | | - e.stopPropagation(); |
609 | | - }); |
610 | 613 | }); |
611 | | - |
612 | 614 | </file> |
613 | 615 |
|
614 | 616 | <file name="protractor.js" type="protractor"> |
615 | | - //browser.ignoreSynchronization = true; |
616 | | - |
| 617 | + |
617 | 618 | var addressBar = element(by.css("#addressBar")), |
618 | 619 | url = 'http://www.example.com/base/index.html#!/path?a=b#h'; |
619 | | - |
| 620 | + |
620 | 621 | it("should show fake browser info on load", function(){ |
621 | | - browser.ignoreSynchronization = true; |
622 | 622 | expect(addressBar.getAttribute('value')).toBe(url); |
623 | 623 |
|
624 | 624 | expect(element(by.binding('$location.protocol')).getText()).toBe('http'); |
@@ -628,11 +628,9 @@ In these examples we use `<base href="/base/index.html" />` |
628 | 628 | expect(element(by.binding('$location.search')).getText()).toBe('{"a":"b"}'); |
629 | 629 | expect(element(by.binding('$location.hash')).getText()).toBe('h'); |
630 | 630 |
|
631 | | - browser.ignoreSynchronization = false; |
632 | 631 | }); |
633 | 632 |
|
634 | 633 | it("should change $location accordingly", function(){ |
635 | | - browser.ignoreSynchronization = true; |
636 | 634 | var navigation = element.all(by.css("#navigation a")); |
637 | 635 |
|
638 | 636 | navigation.get(0).click(); |
@@ -658,7 +656,6 @@ In these examples we use `<base href="/base/index.html" />` |
658 | 656 | expect(element(by.binding('$location.search')).getText()).toBe('{"flag":true}'); |
659 | 657 | expect(element(by.binding('$location.hash')).getText()).toBe('hash'); |
660 | 658 |
|
661 | | - browser.ignoreSynchronization = false; |
662 | 659 | }); |
663 | 660 | </file> |
664 | 661 |
|
|