Skip to content

Commit 0f4f009

Browse files
committed
fix(qr): Fix text attribute required error when value is set via a promise.
Taken from janantala#34.
1 parent 9d12141 commit 0f4f009

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/angular-qr.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
(function(QRCode){
1+
(function(QRCode) {
22
'use strict';
33

44
angular.module('ja.qr', [])
5-
.controller('QrCtrl', ['$scope', function($scope){
6-
$scope.getTypeNumeber = function(){
5+
.controller('QrCtrl', ['$scope', function($scope) {
6+
$scope.getTypeNumeber = function() {
77
return $scope.typeNumber || 0;
88
};
99

10-
$scope.getCorrection = function(){
10+
$scope.getCorrection = function() {
1111
var levels = {
1212
'L': 1,
1313
'M': 0,
@@ -19,25 +19,25 @@
1919
return levels[correctionLevel] || 0;
2020
};
2121

22-
$scope.getText = function(){
22+
$scope.getText = function() {
2323
return $scope.text || '';
2424
};
2525

26-
$scope.getSize = function(){
26+
$scope.getSize = function() {
2727
return $scope.size || 250;
2828
};
2929

30-
$scope.isNUMBER = function(text){
30+
$scope.isNUMBER = function(text) {
3131
var ALLOWEDCHARS = /^[0-9]*$/;
3232
return ALLOWEDCHARS.test(text);
3333
};
3434

35-
$scope.isALPHA_NUM = function(text){
35+
$scope.isALPHA_NUM = function(text) {
3636
var ALLOWEDCHARS = /^[0-9A-Z $%*+\-./:]*$/;
3737
return ALLOWEDCHARS.test(text);
3838
};
3939

40-
$scope.is8bit = function(text){
40+
$scope.is8bit = function(text) {
4141
for (var i = 0; i < text.length; i++) {
4242
var code = text.charCodeAt(i);
4343
if (code > 255) {
@@ -47,7 +47,7 @@
4747
return true;
4848
};
4949

50-
$scope.checkInputMode = function(inputMode, text){
50+
$scope.checkInputMode = function(inputMode, text) {
5151
if (inputMode === 'NUMBER' && !$scope.isNUMBER(text)) {
5252
throw new Error('The `NUMBER` input mode is invalid for text.');
5353
}
@@ -64,7 +64,7 @@
6464
return true;
6565
};
6666

67-
$scope.getInputMode = function(text){
67+
$scope.getInputMode = function(text) {
6868
var inputMode = $scope.inputMode;
6969
inputMode = inputMode || ($scope.isNUMBER(text) ? 'NUMBER' : undefined);
7070
inputMode = inputMode || ($scope.isALPHA_NUM(text) ? 'ALPHA_NUM' : undefined);
@@ -73,7 +73,7 @@
7373
return $scope.checkInputMode(inputMode, text) ? inputMode : '';
7474
};
7575
}])
76-
.directive('qr', ['$timeout', '$window', function($timeout, $window){
76+
.directive('qr', ['$timeout', '$window', function($timeout, $window) {
7777

7878
return {
7979
restrict: 'E',
@@ -87,9 +87,9 @@
8787
image: '='
8888
},
8989
controller: 'QrCtrl',
90-
link: function postlink(scope, element, attrs){
90+
link: function postlink(scope, element) {
9191

92-
if (scope.text === undefined) {
92+
if (!element[0].hasAttribute('text')) {
9393
throw new Error('The `text` attribute is required.');
9494
}
9595

@@ -103,7 +103,7 @@
103103
scope.INPUT_MODE = scope.getInputMode(scope.TEXT);
104104
scope.canvasImage = '';
105105

106-
var draw = function(context, qr, modules, tile){
106+
var draw = function(context, qr, modules, tile) {
107107
for (var row = 0; row < modules; row++) {
108108
for (var col = 0; col < modules; col++) {
109109
var w = (Math.ceil((col + 1) * tile) - Math.floor(col * tile)),
@@ -114,7 +114,7 @@
114114
}
115115
};
116116

117-
var render = function(canvas, value, typeNumber, correction, size, inputMode){
117+
var render = function(canvas, value, typeNumber, correction, size, inputMode) {
118118
var trim = /^\s+|\s+$/g;
119119
var text = value.replace(trim, '');
120120

@@ -136,37 +136,37 @@
136136

137137
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE);
138138

139-
$timeout(function(){
140-
scope.$watch('text', function(value, old){
139+
$timeout(function() {
140+
scope.$watch('text', function(value, old) {
141141
if (value !== old) {
142142
scope.TEXT = scope.getText();
143143
scope.INPUT_MODE = scope.getInputMode(scope.TEXT);
144144
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE);
145145
}
146146
});
147147

148-
scope.$watch('correctionLevel', function(value, old){
148+
scope.$watch('correctionLevel', function(value, old) {
149149
if (value !== old) {
150150
scope.CORRECTION = scope.getCorrection();
151151
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE);
152152
}
153153
});
154154

155-
scope.$watch('typeNumber', function(value, old){
155+
scope.$watch('typeNumber', function(value, old) {
156156
if (value !== old) {
157157
scope.TYPE_NUMBER = scope.getTypeNumeber();
158158
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE);
159159
}
160160
});
161161

162-
scope.$watch('size', function(value, old){
162+
scope.$watch('size', function(value, old) {
163163
if (value !== old) {
164164
scope.SIZE = scope.getSize();
165165
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE);
166166
}
167167
});
168168

169-
scope.$watch('inputMode', function(value, old){
169+
scope.$watch('inputMode', function(value, old) {
170170
if (value !== old) {
171171
scope.INPUT_MODE = scope.getInputMode(scope.TEXT);
172172
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE);

0 commit comments

Comments
 (0)