1 | /* ng-FitText.js v3.3.3
|
---|
2 | * https://github.com/patrickmarabeas/ng-FitText.js
|
---|
3 | *
|
---|
4 | * Original jQuery project: https://github.com/davatron5000/FitText.js
|
---|
5 | *
|
---|
6 | * Copyright 2015, Patrick Marabeas http://marabeas.io
|
---|
7 | * Released under the MIT license
|
---|
8 | * http://opensource.org/licenses/mit-license.php
|
---|
9 | *
|
---|
10 | * Date: 06/05/2015
|
---|
11 | */
|
---|
12 |
|
---|
13 | (function (window, document, angular, undefined) {
|
---|
14 |
|
---|
15 | 'use strict';
|
---|
16 |
|
---|
17 | angular.module('ngFitText', [])
|
---|
18 | .value('config', {
|
---|
19 | 'debounce': false,
|
---|
20 | 'delay': 250,
|
---|
21 | 'loadDelay': 10,
|
---|
22 | 'min': undefined,
|
---|
23 | 'max': undefined
|
---|
24 | })
|
---|
25 |
|
---|
26 | .directive('fittext', ['$timeout', 'config', 'fitTextConfig', function ($timeout, config, fitTextConfig) {
|
---|
27 | return {
|
---|
28 | restrict: 'A',
|
---|
29 | scope: true,
|
---|
30 | link: function (scope, element, attrs) {
|
---|
31 | angular.extend(config, fitTextConfig.config);
|
---|
32 |
|
---|
33 | element[0].style.display = 'inline-block';
|
---|
34 | element[0].style.lineHeight = '1';
|
---|
35 |
|
---|
36 | var parent = element.parent();
|
---|
37 | var compressor = attrs.fittext || 1;
|
---|
38 | var loadDelay = attrs.fittextLoadDelay || config.loadDelay;
|
---|
39 | var nl = element[0].querySelectorAll('[fittext-nl],[data-fittext-nl]').length || 1;
|
---|
40 | var minFontSize = attrs.fittextMin || config.min || Number.NEGATIVE_INFINITY;
|
---|
41 | var maxFontSize = attrs.fittextMax || config.max || Number.POSITIVE_INFINITY;
|
---|
42 |
|
---|
43 | var resizer = function () {
|
---|
44 | element[0].style.fontSize = '10px';
|
---|
45 | var ratio = element[0].offsetHeight / element[0].offsetWidth / nl;
|
---|
46 | element[0].style.fontSize = Math.max(
|
---|
47 | Math.min((parent[0].offsetWidth - 6) * ratio * compressor,
|
---|
48 | parseFloat(maxFontSize)
|
---|
49 | ),
|
---|
50 | parseFloat(minFontSize)
|
---|
51 | ) + 'px';
|
---|
52 | };
|
---|
53 |
|
---|
54 | $timeout(function () { resizer() }, loadDelay);
|
---|
55 |
|
---|
56 | scope.$watch(attrs.ngModel, function () { resizer() });
|
---|
57 |
|
---|
58 | config.debounce
|
---|
59 | ? angular.element(window).bind('resize', config.debounce(function () { scope.$apply(resizer) }, config.delay))
|
---|
60 | : angular.element(window).bind('resize', function () { scope.$apply(resizer) });
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }])
|
---|
64 |
|
---|
65 | .provider('fitTextConfig', function () {
|
---|
66 | var self = this;
|
---|
67 | this.config = {};
|
---|
68 | this.$get = function () {
|
---|
69 | var extend = {};
|
---|
70 | extend.config = self.config;
|
---|
71 | return extend;
|
---|
72 | };
|
---|
73 | return this;
|
---|
74 | });
|
---|
75 |
|
---|
76 | })(window, document, angular); |
---|