forked from nutritionix/nutrition-label
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nutritionLabel.js
2701 lines (2358 loc) · 131 KB
/
nutritionLabel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
***************************************************************************************************************************************************+
* NUTRITIONIX.com |
* |
* This plugin allows you to create a fully customizable nutrition label |
* |
* @authors Leo Joseph Gajitos <leejay22@gmail.com>, Rommel Malang <genesis23rd@gmail.com> and Yurko Fedoriv <yurko.fedoriv@gmail.com> |
* @copyright Copyright (c) 2017 Nutritionix. |
* @license This Nutritionix jQuery Nutrition Label is dual licensed under the MIT and GPL licenses. |
* @link http://www.nutritionix.com |
* @github http://github.com/nutritionix/nutrition-label |
* @current version 8.1.0 |
* @stable version 8.0.5 |
* @supported browser Firefox, Chrome, IE8+ |
* @description To be able to create a FDA-style nutrition label with any nutrition data source
* @description modified to include additional vitamins and calculation options
* |
***************************************************************************************************************************************************+
*/
;(function($){
$.fn.nutritionLabel = function(option, settings){
if (typeof option === 'object'){
settings = option;
init( settings, $(this) );
}else if (typeof option === 'string' && option !== ''){
//destroy the nutrition label's html code
if (option === 'destroy'){
new NutritionLabel().destroy( $(this) );
//allows the user to hide the nutrition value
}else if (option === 'hide'){
new NutritionLabel().hide( $(this) );
//allows the user to show the nutrition value
}else if (option === 'show'){
new NutritionLabel().show( $(this) );
}else{
var values = [];
var elements = this.each(function(){
var data = $(this).data('_nutritionLabel');
if (data){
if ($.fn.nutritionLabel.defaultSettings[option] !== undefined){
if (settings !== undefined){
//set the option and create the nutrition label
data.settings[option] = settings;
init( data.settings, $(this) );
}else{
//return the value of a setting - can only be used after the label is created / initiated
values.push(data.settings[option]);
}
}
}else if ($.fn.nutritionLabel.defaultSettings[option] !== undefined){
//set the option and create the nutrition label
//this is a special case so the single value setting will still work even if the label hasn't been initiated yet
if (settings !== undefined){
$.fn.nutritionLabel.defaultSettings[option] = settings;
init( null, $(this) );
}
}
});
//return the value of a setting
if (values.length === 1){
return values[0];
}
//return the setting values or the elements
return values.length > 0 ? values : elements;
}
//end of => else if (typeof option === 'string' && option !== '')
}else if (typeof option === 'undefined' || option === ''){
//if no value / option is supplied, simply create the label using the default values
init( settings, $(this) );
}
};//end of => $.fn.nutritionLabel = function(option, settings)
$.fn.nutritionLabel.defaultSettings = {
//default fixedWidth of the nutrition label
width : 280,
//to allow custom width - usually needed for mobile sites
allowCustomWidth : false,
widthCustom : 'auto',
//to allow the label to have no border
allowNoBorder : false,
//to enable rounding of the nutritional values based on the FDA rounding rules http://goo.gl/RMD2O
allowFDARounding : false,
//2018 only. calculate vitamin DV percentage by mass, instead of calculating mass from percent DV
useMassForVitamins : false,
//to enabled the google analytics event logging
allowGoogleAnalyticsEventLog : false,
gooleAnalyticsFunctionName : 'ga',
//enable triggering of user function on quantity change: global function name
userFunctionNameOnQuantityChange: null,
//enable triggering of user function on quantity change: handler instance
userFunctionOnQuantityChange: null,
//when set to true, this will hide the values if they are not applicable
hideNotApplicableValues : false,
//when set to true, this will hide all the percent daily values
hidePercentDailyValues : false,
//the brand name of the item for this label (eg. just salad)
brandName : 'Brand where this item belongs to',
//to scroll the ingredients if the innerheight is > scrollHeightComparison
scrollLongIngredients : false,
scrollHeightComparison : 100,
//the height in px of the ingredients div
scrollHeightPixel : 95,
//this is to set how many decimal places will be shown on the nutrition values (calories, fat, protein, vitamin a, iron, etc)
decimalPlacesForNutrition : 1,
//this is to set how many decimal places will be shown for the "% daily values*"
decimalPlacesForDailyValues : 0,
//this is to set how many decimal places will be shown for the serving unit quantity textbox
decimalPlacesForQuantityTextbox : 1,
//to scroll the item name if the jQuery.height() is > scrollLongItemNamePixel
scrollLongItemName : true,
scrollLongItemNamePixel : 36,
scrollLongItemNamePixel2018Override : 34, //this is needed to fix some issues on the 2018 label as the layout of the label is very different than the legacy one
//show the customizable link at the bottom
showBottomLink : false,
//url for the customizable link at the bottom
urlBottomLink : 'http://www.nutritionix.com',
//link name for the customizable link at the bottom
nameBottomLink : 'Nutritionix',
//this value can be changed and the value of the nutritions will be affected directly
//the computation is "current nutrition value" * "serving unit quantity value" = "final nutrition value"
//this can't be less than zero, all values less than zero is converted to zero
//the textbox to change this value is visible / enabled by default
//if the initial value of the serving size unit quantity is less than or equal to zero, it is converted to 1.0
//when enabled, user can change this value by clicking the arrow or changing the value on the textbox and pressing enter. the value on the label will be updated automatically
//different scenarios and the result if this feature is enabled
//NOTE 1: [ ] => means a textbox will be shown
//NOTE 2: on all cases below showServingUnitQuantityTextbox == true AND showServingUnitQuantity == true
//if showServingUnitQuantity == false, the values that should be on the 'serving size div' are empty or null
//CASE 1a: valueServingSizeUnit != '' (AND NOT null) && valueServingUnitQuantity >= 0
//RESULT: textServingSize [valueServingUnitQuantity] valueServingSizeUnit
//NOTE 3: on all cases below showServingUnitQuantityTextbox == true AND showItemName == true
//if showItemName == false, the values that should be on the 'item name div' are empty or null
//CASE 1b: valueServingSizeUnit != '' (AND NOT null) && valueServingUnitQuantity <= 0
//RESULT: [valueServingUnitQuantity default to 1.0] itemName
//CASE 3a: valueServingSizeUnit == '' (OR null) && valueServingUnitQuantity > 0
//RESULT: [valueServingUnitQuantity] itemName
//CASE 3b: valueServingSizeUnit == '' (OR null) && valueServingUnitQuantity <= 0
//RESULT: [valueServingUnitQuantity default to 1.0] itemName
//NOTE 4: to see the different resulting labels, check the html/demo-texbox-case*.html files
valueServingUnitQuantity : 1.0,
valueServingSizeUnit : '',
showServingUnitQuantityTextbox : true,
//the name of the item for this label (eg. cheese burger or mayonnaise)
itemName : 'Item / Ingredient Name',
showServingUnitQuantity : true,
//allow hiding of the textbox arrows
hideTextboxArrows : false,
//these 2 settings are used internally.
//this is just added here instead of a global variable to prevent a bug when there are multiple instances of the plugin like on the demo pages
originalServingUnitQuantity : 0,
//this is used to fix the computation issue on the textbox
nutritionValueMultiplier : 1,
//this is used for the computation of the servings per container
totalContainerQuantity : 1,
//default calorie intake
calorieIntake : 2000,
//these are the recommended daily intake values
dailyValueTotalFat : 65,
dailyValueSatFat : 20,
dailyValueCholesterol : 300,
dailyValueSodium : 2400,
dailyValuePotassium : 3500,
dailyValuePotassium_2018 : 4700,
dailyValueCarb : 300,
dailyValueFiber : 25,
dailyValueCalcium : 1300,
dailyValueIron : 18,
dailyValueVitaminD : 20,
dailyValueAddedSugar : 50,
dailyValueVitaminA_2018 : 5000,
dailyValueVitaminC_2018 : 60,
dailyValueVitaminE : 30,
dailyValueVitaminK : 80,
dailyValueThiamin : 1.5,
dailyValueRiboflavin : 1.7,
dailyValueNiacin : 20,
dailyValueVitaminB6 : 2,
dailyValueFolate : 400,
dailyValueVitaminB12 : 6,
dailyValueBiotin : 300,
dailyValuePantothenicAcid : 10,
dailyValuePhosphorus : 1000,
dailyValueIodine : 150,
dailyValueMagnesium : 400,
dailyValueZinc : 15,
dailyValueSelenium : 70,
dailyValueCopper : 2,
dailyValueManganese : 2,
dailyValueChromium : 120,
dailyValueMolybdenum : 75,
dailyValueChloride : 3400,
//these values can be change to hide some nutrition values
showCalories : true,
showFatCalories : true,
showTotalFat : true,
showSatFat : true,
showTransFat : true,
showPolyFat : false,
showMonoFat : false,
showCholesterol : true,
showSodium : true,
showPotassium: false, //this is for the legacy version, this is the only value that is default to be hidden
showPotassium_2018: true, //this is for the 2018 version
showTotalCarb : true,
showFibers : true,
showSugars : true,
showAddedSugars : true,
showSugarAlcohol : false,
showProteins : true,
showVitaminA : true,
showVitaminA_2018: false, // optional in 2018
showVitaminC : true,
showVitaminC_2018: false, // optional in 2018
showVitaminD : true,
showCalcium : true,
showIron : true,
// additional optional nutrients for 2018 version
// these can be included, but are not required by the FDA as of 2018
// disabled by default
showVitaminE: false,
showVitaminK: false,
showThiamin: false,
showRiboflavin: false,
showNiacin: false,
showVitaminB6: false,
showFolate: false,
showVitaminB12: false,
showBiotin: false,
showPantothenicAcid: false,
showPhosphorus: false,
showIodine: false,
showMagnesium: false,
showZinc: false,
showSelenium: false,
showCopper: false,
showManganese: false,
showChromium: false,
showMolybdenum: false,
showChloride: false,
//to show the 'amount per serving' text
showAmountPerServing : true,
//to show the 'servings per container' data and replace the default 'Serving Size' value (without unit and servings per container text and value)
showServingsPerContainer : false,
//to show the item name. there are special cases where the item name is replaced with 'servings per container' value
showItemName : true,
//show the brand where this item belongs to
showBrandName : false,
//to show the ingredients value or not
showIngredients : true,
//to show the calorie diet info at the bottom of the label
showCalorieDiet : false,
//to show the customizable footer which can contain html and js codes
showCustomFooter : false,
//to show the disclaimer text or not
showDisclaimer : false,
//the height in px of the disclaimer div
scrollDisclaimerHeightComparison : 100,
scrollDisclaimer : 95,
valueDisclaimer : 'Please note that these nutrition values are estimated based on our standard serving portions. ' +
'As food servings may have a slight variance each time you visit, please expect these values to be with in 10% +/- of your actual meal. ' +
'If you have any questions about our nutrition calculator, please contact Nutritionix.',
ingredientLabel : 'INGREDIENTS:',
valueCustomFooter : '',
//the are to set some values as 'not applicable'. this means that the nutrition label will appear but the value will be a 'gray dash'
naCalories : false,
naFatCalories : false,
naTotalFat : false,
naSatFat : false,
naTransFat : false,
naPolyFat : false,
naMonoFat : false,
naCholesterol : false,
naSodium : false,
naPotassium : false, //this is for the legacy version
naPotassium_2018 : false, //this is for the 2018 version
naTotalCarb : false,
naFibers : false,
naSugars : false,
naAddedSugars : false,
naSugarAlcohol : false,
naProteins : false,
naVitaminA : false,
naVitaminA_2018: false,
naVitaminC : false,
naVitaminC_2018: false,
naVitaminD : false,
naCalcium : false,
naIron : false,
naVitaminE: false,
naVitaminK: false,
naThiamin: false,
naRiboflavin: false,
naNiacin: false,
naVitaminB6: false,
naFolate: false,
naVitaminB12: false,
naBiotin: false,
naPantothenicAcid: false,
naPhosphorus: false,
naIodine: false,
naMagnesium: false,
naZinc: false,
naSelenium: false,
naCopper: false,
naManganese: false,
naChromium: false,
naMolybdenum: false,
naChloride: false,
//these are the default values for the nutrition info
valueServingWeightGrams : 0,
valueServingPerContainer : 1,
valueCalories : 0,
valueFatCalories : 0,
valueTotalFat : 0,
valueSatFat : 0,
valueTransFat : 0,
valuePolyFat : 0,
valueMonoFat : 0,
valueCholesterol : 0,
valueSodium : 0,
valuePotassium : 0, //this is for the legacy version
valuePotassium_2018 : 0, //this is for the 2018 version
valueTotalCarb : 0,
valueFibers : 0,
valueSugars : 0,
valueAddedSugars : 0,
valueSugarAlcohol : 0,
valueProteins : 0,
valueVitaminA : 0, //this is for the legacy version
valueVitaminA_2018: 0, //this is for the 2018 version, optional
valueVitaminC : 0, //this is for the legacy version
valueVitaminC_2018: 0, //this is for the 2018 version, optional
valueVitaminD : 0,
valueCalcium : 0,
valueIron : 0,
// optional nutrients 2018 only
valueVitaminE: 0,
valueVitaminK: 0,
valueThiamin: 0,
valueRiboflavin: 0,
valueNiacin: 0,
valueVitaminB6: 0,
valueFolate: 0,
valueVitaminB12: 0,
valueBiotin: 0,
valuePantothenicAcid: 0,
valuePhosphorus: 0,
valueIodine: 0,
valueMagnesium: 0,
valueZinc: 0,
valueSelenium: 0,
valueCopper: 0,
valueManganese: 0,
valueChromium: 0,
valueMolybdenum: 0,
valueChloride: 0,
//customizable units for the values
unitCalories : '',
unitFatCalories : '',
unitTotalFat : '<span aria-hidden="true">g</span><span class="sr-only"> grams</span>',
unitSatFat : '<span aria-hidden="true">g</span><span class="sr-only"> grams</span>',
unitTransFat : '<span aria-hidden="true">g</span><span class="sr-only"> grams</span>',
unitPolyFat : '<span aria-hidden="true">g</span><span class="sr-only"> grams</span>',
unitMonoFat : '<span aria-hidden="true">g</span><span class="sr-only"> grams</span>',
unitCholesterol : '<span aria-hidden="true">mg</span><span class="sr-only"> milligrams</span>',
unitSodium : '<span aria-hidden="true">mg</span><span class="sr-only"> milligrams</span>',
unitPotassium : '<span aria-hidden="true">mg</span><span class="sr-only"> milligrams</span>', //this is for the legacy version
unitPotassium_base : '<span aria-hidden="true">mg</span><span class="sr-only"> milligrams</span>', //this is for the 2018 version
unitPotassium_percent : '%', //this is for the 2018 version
unitTotalCarb : '<span aria-hidden="true">g</span><span class="sr-only"> grams</span>',
unitFibers : '<span aria-hidden="true">g</span><span class="sr-only"> grams</span>',
unitSugars : '<span aria-hidden="true">g</span><span class="sr-only"> grams</span>',
unitAddedSugars : '<span aria-hidden="true">g</span><span class="sr-only"> grams</span>',
unitSugarAlcohol : '<span aria-hidden="true">g</span><span class="sr-only"> grams</span>',
unitProteins : '<span aria-hidden="true">g</span><span class="sr-only"> grams</span>',
unitVitaminA : '%',
unitVitaminA_base: '<span aria-hidden="true"> IU</span><span class="sr-only"> international units</span>', //this is for the 2018 version
unitVitaminA_percent: "%", //this is for the 2018 version
unitVitaminC : '%',
unitVitaminC_base: '<span aria-hidden="true">mg</span><span class="sr-only"> milligrams</span>', //this is for the 2018 version
unitVitaminC_percent: "%", //this is for the 2018 version
unitVitaminD_base : '<span aria-hidden="true">mcg</span><span class="sr-only"> micrograms</span>', //this is for the 2018 version
unitVitaminD_percent : '%', //this is for the 2018 version
unitCalcium : '%',
unitCalcium_base : '<span aria-hidden="true">mg</span><span class="sr-only"> milligrams</span>', //this is for the 2018 version
unitCalcium_percent : '%', //this is for the 2018 version
unitIron : '%',
unitIron_base : '<span aria-hidden="true">mg</span><span class="sr-only"> milligrams</span>', //this is for the 2018 version
unitIron_percent : '%', //this is for the 2018 version
// optional nutrients 2018 only
unitVitaminE_base: '<span aria-hidden="true"> IU</span><span class="sr-only"> international units</span>',
unitVitaminE_percent: "%",
unitVitaminK_base: '<span aria-hidden="true"> IU</span><span class="sr-only"> international units</span>',
unitVitaminK_percent: "%",
unitThiamin_base: '<span aria-hidden="true">mg</span><span class="sr-only"> milligrams</span>',
unitThiamin_percent: "%",
unitRiboflavin_base: '<span aria-hidden="true">mg</span><span class="sr-only"> milligrams</span>',
unitRiboflavin_percent: "%",
unitNiacin_base: '<span aria-hidden="true">mg</span><span class="sr-only"> milligrams</span>',
unitNiacin_percent: "%",
unitVitaminB6_base: '<span aria-hidden="true">mg</span><span class="sr-only"> milligrams</span>',
unitVitaminB6_percent: "%",
unitFolate_base: '<span aria-hidden="true">µg</span><span class="sr-only"> micrograms</span>',
unitFolate_percent: "%",
unitVitaminB12_base: '<span aria-hidden="true">µg</span><span class="sr-only"> micrograms</span>',
unitVitaminB12_percent: "%",
unitBiotin_base: '<span aria-hidden="true">µg</span><span class="sr-only"> micrograms</span>',
unitBiotin_percent: "%",
unitPantothenicAcid_base: '<span aria-hidden="true">mg</span><span class="sr-only"> micrograms</span>',
unitPantothenicAcid_percent: "%",
unitPhosphorus_base: '<span aria-hidden="true">mg</span><span class="sr-only"> micrograms</span>',
unitPhosphorus_percent: "%",
unitIodine_base: '<span aria-hidden="true">µg</span><span class="sr-only"> micrograms</span>',
unitIodine_percent: "%",
unitMagnesium_base: '<span aria-hidden="true">mg</span><span class="sr-only"> micrograms</span>',
unitMagnesium_percent: "%",
unitZinc_base: '<span aria-hidden="true">mg</span><span class="sr-only"> micrograms</span>',
unitZinc_percent: "%",
unitSelenium_base: '<span aria-hidden="true">µg</span><span class="sr-only"> micrograms</span>',
unitSelenium_percent: "%",
unitCopper_base: '<span aria-hidden="true">mg</span><span class="sr-only"> micrograms</span>',
unitCopper_percent: "%",
unitManganese_base: '<span aria-hidden="true">mg</span><span class="sr-only"> micrograms</span>',
unitManganese_percent: "%",
unitChromium_base: '<span aria-hidden="true">µg</span><span class="sr-only"> micrograms</span>',
unitChromium_percent: "%",
unitMolybdenum_base: '<span aria-hidden="true">µg</span><span class="sr-only"> micrograms</span>',
unitMolybdenum_percent: "%",
unitChloride_base: '<span aria-hidden="true">mg</span><span class="sr-only"> micrograms</span>',
unitChloride_percent: "%",
//these are the values for the optional calorie diet
valueCol1CalorieDiet : 2000,
valueCol2CalorieDiet : 2500,
valueCol1DietaryTotalFat : 0,
valueCol2DietaryTotalFat : 0,
valueCol1DietarySatFat : 0,
valueCol2DietarySatFat : 0,
valueCol1DietaryCholesterol : 0,
valueCol2DietaryCholesterol : 0,
valueCol1DietarySodium : 0,
valueCol2DietarySodium : 0,
valueCol1DietaryPotassium : 0,
valueCol2DietaryPotassium : 0,
valueCol1DietaryTotalCarb : 0,
valueCol2DietaryTotalCarb : 0,
valueCol1Dietary : 0,
valueCol2Dietary : 0,
//these text settings is so you can create nutrition labels in different languages or to simply change them to your need
textNutritionFacts : 'Nutrition Facts',
textDailyValues : 'Daily Value',
textServingSize : 'Serving Size:',
textServingsPerContainer : 'Servings Per Container',
textAmountPerServing : 'Amount Per Serving',
textCalories : 'Calories',
textFatCalories : 'Calories from Fat',
textTotalFat : 'Total Fat',
textSatFat : 'Saturated Fat',
textTransFat : '<em>Trans</em> Fat',
textPolyFat : 'Polyunsaturated Fat',
textMonoFat : 'Monounsaturated Fat',
textCholesterol : 'Cholesterol',
textSodium : 'Sodium',
textPotassium : 'Potassium',
textTotalCarb : 'Total Carbohydrates',
textFibers : 'Dietary Fiber',
textSugars : 'Sugars',
textAddedSugars1 : 'Includes ',
textAddedSugars2 : ' Added Sugars',
textSugarAlcohol : 'Sugar Alcohol',
textProteins : 'Protein',
textVitaminA : 'Vitamin A',
textVitaminC : 'Vitamin C',
textVitaminD : 'Vitamin D',
textCalcium : 'Calcium',
textIron : 'Iron',
textVitaminE: "Vitamin E",
textVitaminK: "Vitamin K",
textThiamin: "Thiamin",
textRiboflavin: "Riboflavin",
textNiacin: "Niacin",
textVitaminB6: "Vitamin B<sub>6</sub>",
textFolate: "Folate",
textVitaminB12: "Vitamin B<sub>12</sub>",
textBiotin: "Biotin",
textPantothenicAcid: "Pantothenic Acid",
textPhosphorus: "Phosphorus",
textIodine: "Iodine",
textMagnesium: "Magnesium",
textZinc: "Zinc",
textSelenium: "Selenium",
textCopper: "Copper",
textManganese: "Manganese",
textChromium: "Chromium",
textMolybdenum: "Molybdenum",
textChloride: "Chloride",
textNotApplicable : '-',
ingredientList : 'None',
textPercentDailyPart1 : 'Percent Daily Values are based on a',
textPercentDailyPart2 : 'calorie diet',
textPercentDaily2018VersionPart1 : 'The % Daily Value (DV) tells you how much a nutrient in a serving of food contributes to a daily diet. ',
textPercentDaily2018VersionPart2 : ' calories a day is used for general nutrition advice.',
textGoogleAnalyticsEventCategory : 'Nutrition Label',
textGoogleAnalyticsEventActionUpArrow : 'Quantity Up Arrow Clicked',
textGoogleAnalyticsEventActionDownArrow : 'Quantity Down Arrow Clicked',
textGoogleAnalyticsEventActionTextbox : 'Quantity Textbox Changed',
showLegacyVersion : true,
//more details here https://github.com/nutritionix/nutrition-label/issues/77#issuecomment-323510972
legacyVersion: 1
};//end of => $.fn.nutritionLabel.defaultSettings
//this will store the unique individual properties for each instance of the plugin
function NutritionLabel(settings, $elem){
this.nutritionLabel = null;
this.settings = settings;
this.$elem = $elem;
return this;
}
function cleanSettings(settings){
var numericIndex = [
'width', 'scrollHeightComparison', 'scrollHeightPixel', 'decimalPlacesForNutrition', 'decimalPlacesForDailyValues', 'calorieIntake', 'dailyValueTotalFat', 'dailyValueSatFat',
'dailyValueCholesterol', 'dailyValueSodium', 'dailyValuePotassium', 'dailyValueCarb', 'dailyValueFiber','valueServingSize', 'valueServingWeightGrams', 'valueServingPerContainer',
'valueCalories', 'valueFatCalories', 'valueTotalFat', 'valueSatFat', 'valueTransFat', 'valuePolyFat', 'valueMonoFat', 'valueCholesterol', 'valueSodium', 'valuePotassium', 'valueTotalCarb',
'valueFibers', 'valueSugars', 'valueProteins', 'valueVitaminA', 'valueVitaminC', 'valueCalcium', 'valueIron', 'valueCol1CalorieDiet', 'valueCol2CalorieDiet', 'valueCol1DietaryTotalFat',
'valueCol2DietaryTotalFat', 'valueCol1DietarySatFat', 'valueCol2DietarySatFat', 'valueCol1DietaryCholesterol', 'valueCol2DietaryCholesterol', 'valueCol1DietarySodium',
'valueCol2DietarySodium', 'valueCol1DietaryPotassium', 'valueCol2DietaryPotassium', 'valueCol1DietaryTotalCarb', 'valueCol2DietaryTotalCarb', 'valueCol1Dietary', 'valueCol2Dietary',
'valueServingUnitQuantity', 'scrollLongItemNamePixel', 'scrollLongItemNamePixel2018Override', 'decimalPlacesForQuantityTextbox', 'valueAddedSugars', 'dailyValueVitaminD',
'dailyValueCalcium', 'dailyValueIron', 'valueVitaminD', 'valueSugarAlcohol'
];
$.each(settings, function(index, value){
if (jQuery.inArray(index, numericIndex) !== -1){
settings[index] = parseFloat(settings[index]);
if (isNaN(settings[index]) || settings[index] === undefined){
settings[index] = 0;
}
}
});
if (settings['valueServingUnitQuantity'] < 0){
settings['valueServingUnitQuantity'] = 0;
}
return settings;
}
function updateNutritionValueWithMultiplier(settings){
var nutritionIndex = [
'valueCalories', 'valueFatCalories', 'valueTotalFat', 'valueSatFat', 'valueTransFat', 'valuePolyFat', 'valueMonoFat', 'valueCholesterol', 'valueSodium', 'valuePotassium', 'valueTotalCarb',
'valueFibers','valueSugars','valueProteins', 'valueVitaminA', 'valueVitaminC', 'valueCalcium', 'valueIron', 'valueServingWeightGrams', 'valueAddedSugars', 'valueVitaminD',
'valuePotassium_2018', 'valueSugarAlcohol'
];
$.each(settings, function(index, value){
if (jQuery.inArray(index, nutritionIndex) !== -1){
settings[index] = parseFloat(settings[index]);
if (isNaN(settings[index]) || settings[index] === undefined){
settings[index] = 0;
}
settings[index] =
parseFloat(settings[index]) *
parseFloat(settings['valueServingUnitQuantity']) *
parseFloat(settings['nutritionValueMultiplier']);
}
});
if (parseFloat(settings['valueServingUnitQuantity']) == 0){
settings['valueServingPerContainer'] = 0;
}else if (!isNaN(settings['valueServingPerContainer']) && settings['valueServingPerContainer'] != undefined){
settings['valueServingPerContainer'] = parseFloat(settings.totalContainerQuantity) / parseFloat(settings['valueServingUnitQuantity']);
}
return settings;
}
function init(settings, $elem){
//merge the default settins with the user supplied settings
var $settings = $.extend( {}, $.fn.nutritionLabel.defaultSettings, settings || {} );
$settings.totalContainerQuantity = parseFloat($settings.valueServingPerContainer) * parseFloat($settings['valueServingUnitQuantity']);
var $originalCleanSettings = cleanSettings( $.extend( {}, $.fn.nutritionLabel.defaultSettings, settings || {} ) );
$originalCleanSettings.totalContainerQuantity = parseFloat($originalCleanSettings.valueServingPerContainer) * parseFloat($originalCleanSettings['valueServingUnitQuantity']);
//clean the settings and make sure that all numeric settings are really numeric, if not, force them to be
$settings = cleanSettings($settings);
$originalCleanSettings = cleanSettings($originalCleanSettings);
$settings.nutritionValueMultiplier = $settings.valueServingUnitQuantity <= 0 ? 1 : 1 / $settings.valueServingUnitQuantity;
//update the nutrition values with the multiplier
var $updatedsettings = updateNutritionValueWithMultiplier($settings);
$settings.originalServingUnitQuantity = $updatedsettings.valueServingUnitQuantity;
//if the original value is <= 0, set it to 1.0
if ($updatedsettings.valueServingUnitQuantity <= 0){
$originalCleanSettings.valueServingUnitQuantity = 1;
$updatedsettings = updateNutritionValueWithMultiplier($originalCleanSettings);
$updatedsettings.valueServingUnitQuantity = 1;
}
//initalize the nutrition label and create / recreate it
var nutritionLabel = new NutritionLabel($updatedsettings, $elem);
if ($updatedsettings.showLegacyVersion){
//updateValuesAfterAQuantityChanged($localSettings, nutritionLabel, $elem, forLegacyLabel, forInitialization)
updateValuesAfterAQuantityChanged($settings, nutritionLabel, $elem, true, true);
//if the text box for the unit quantity is shown
if ($settings.showServingUnitQuantityTextbox){
//increase the unit quantity by clicking the up arrow
$('#' + $elem.attr('id') ).delegate('.unitQuantityUp', 'click', function(e){
e.preventDefault();
changeQuantityByArrow($(this), 1, updateTheSettingsAfterAnEvent($settings, settings), nutritionLabel, $elem, true);
});
//decrease the unit quantity by clicking the down arrow
$('#' + $elem.attr('id') ).delegate('.unitQuantityDown', 'click', function(e){
e.preventDefault();
changeQuantityByArrow($(this), -1, updateTheSettingsAfterAnEvent($settings, settings), nutritionLabel, $elem, true);
});
//the textbox unit quantity value is changed
$('#' + $elem.attr('id') ).delegate('.unitQuantityBox', 'change', function(e){
e.preventDefault();
changeQuantityTextbox($(this), updateTheSettingsAfterAnEvent($settings, settings), nutritionLabel, $elem, true);
});
//the textbox unit quantity value is changed
$('#' + $elem.attr('id') ).delegate('.unitQuantityBox', 'keydown', function(e){
if (e.keyCode == 13){
e.preventDefault();
changeQuantityTextbox($(this), updateTheSettingsAfterAnEvent($settings, settings), nutritionLabel, $elem, true);
}
});
}//end of => if ($settings.showServingUnitQuantityTextbox)
//end of => if ($updatedsettings.showLegacyVersion)
}else{
//this part is for the 2018 version
//updateValuesAfterAQuantityChanged($localSettings, nutritionLabel, $elem, forLegacyLabel, forInitialization)
updateValuesAfterAQuantityChanged($settings, nutritionLabel, $elem, false, true);
//if the text box for the unit quantity is shown
if ($settings.showServingUnitQuantityTextbox){
//increase the unit quantity by clicking the up arrow
$('#' + $elem.attr('id') ).delegate('div.nf-unitQuantityUp', 'click', function(e){
e.preventDefault();
changeQuantityByArrow($(this), 1, updateTheSettingsAfterAnEvent($settings, settings), nutritionLabel, $elem, false);
});
//decrease the unit quantity by clicking the down arrow
$('#' + $elem.attr('id') ).delegate('div.nf-unitQuantityDown', 'click', function(e){
e.preventDefault();
changeQuantityByArrow($(this), -1, updateTheSettingsAfterAnEvent($settings, settings), nutritionLabel, $elem, false);
});
//the textbox unit quantity value is changed
$('#' + $elem.attr('id') ).delegate('.nf-unitQuantityBox', 'change', function(e){
e.preventDefault();
changeQuantityTextbox($(this), updateTheSettingsAfterAnEvent($settings, settings), nutritionLabel, $elem, false);
});
//the textbox unit quantity value is changed
$('#' + $elem.attr('id') ).delegate('.nf-unitQuantityBox', 'keydown', function(e){
if (e.keyCode == 13){
e.preventDefault();
changeQuantityTextbox($(this), updateTheSettingsAfterAnEvent($settings, settings), nutritionLabel, $elem, false);
}
});
}//end of => if ($settings.showServingUnitQuantityTextbox)
}//end of else => => if ($updatedsettings.showLegacyVersion)
//store the object for later reference
$elem.data('_nutritionLabel', nutritionLabel);
}//end of => function init(settings, $elem)
function updateTheSettingsAfterAnEvent($localSettings, localSettings){
var $localSettingsHolder = cleanSettings( $.extend( {}, $.fn.nutritionLabel.defaultSettings, localSettings || {} ) );
$localSettingsHolder.originalServingUnitQuantity = $localSettings.originalServingUnitQuantity;
$localSettingsHolder.totalContainerQuantity = $localSettings.totalContainerQuantity;
$localSettingsHolder.nutritionValueMultiplier = $localSettingsHolder.valueServingUnitQuantity <= 0 ? 1 : 1 / $localSettingsHolder.valueServingUnitQuantity;
return $localSettingsHolder;
}
function addScrollToItemDiv($elem, $settings, localNameClass, forLegacyLabel){
var local_scrollLongItemNamePixel = parseInt($settings.scrollLongItemNamePixel);
if (!forLegacyLabel){
local_scrollLongItemNamePixel = parseInt($settings.scrollLongItemNamePixel2018Override);
}
//as of 05142017 inline class only appears on the legacy version
if ( $('#' + $elem.attr('id') + ' .' + localNameClass + '.inline').val() != undefined ){
if ($('#' + $elem.attr('id') + ' .' + localNameClass + '.inline').height() > local_scrollLongItemNamePixel + 1){
$('#' +$elem.attr('id') + ' .' + localNameClass + '.inline').css({
'margin-left' : '3.90em',
'height' : local_scrollLongItemNamePixel + 'px',
'overflow-y' : 'auto'
});
}
}else{
if (forLegacyLabel){
if ($('#' + $elem.attr('id') + ' .' + localNameClass).height() > local_scrollLongItemNamePixel + 1){
$('#' + $elem.attr('id') + ' .' + localNameClass).css({
'height' : local_scrollLongItemNamePixel + 'px',
'overflow-y' : 'auto'
});
}
}else{
if ($('#' + $elem.attr('id') + ' .' + localNameClass + ' div').height() >= local_scrollLongItemNamePixel + 1){
$('#' + $elem.attr('id') + ' .' + localNameClass + ' div').css({
'height' : local_scrollLongItemNamePixel + 'px',
'overflow-y' : 'auto'
});
}
}
}
}
function notApplicableHover($elem){
//this code is for pages with multiple nutrition labels generated by the plugin like the demo page
if ($elem.attr('id') !== undefined && $elem.attr('id') !== ''){
$('#' + $elem.attr('id') + ' .notApplicable').hover(
function(){
$('#' + $elem.attr('id') + ' .naTooltip').css({
'top' : $(this).position().top + 'px',
'left' : $(this).position().left+ 10 + 'px'
}).show();
},
function(){
$('#' + $elem.attr('id') + ' .naTooltip').hide();
}
);
}else{
$('#' + $elem.attr('id') + ' .notApplicable').hover(
function(){
$('.naTooltip').css({
'top' : $(this).position().top + 'px',
'left' : $(this).position().left+ 10 + 'px'
}).show();
},
function(){
$('.naTooltip').hide();
}
);
}
}
function updateScrollingFeature($localElem, $localSettings, localIDToScroll, localScrollHeightComparison, localScrollHeight){
if ($localElem.attr('id') !== undefined && $localElem.attr('id') !== ''){
//this code is for pages with multiple nutrition labels generated by the plugin like the demo page
$parentElement = $('#' + $localElem.attr('id') + ' #' + localIDToScroll).parent();
}else{
$parentElement = $('#' + localIDToScroll).parent();
}
if ($parentElement.innerHeight() > localScrollHeightComparison){
$parentElement.addClass('scroll').css({
'height' : localScrollHeight + 'px'
});
}
}
function updateValuesAfterAQuantityChanged($localSettings, nutritionLabel, $elem, forLegacyLabel, forInitialization){
var ingredientListID = 'ingredientList';
var calcDisclaimerTextID = 'calcDisclaimerText';
var nameElementClass = 'name';
if (!forLegacyLabel){
ingredientListID = 'nf-ingredientList';
calcDisclaimerTextID = 'nf-calcDisclaimerText';
nameElementClass = 'nf-item-name';
}
if (!forInitialization){
$localSettings = updateNutritionValueWithMultiplier($localSettings);
nutritionLabel = new NutritionLabel($localSettings, $elem);
}
if (forLegacyLabel){
$elem.html( nutritionLabel.generateLegacy() );
}else{
$elem.html( nutritionLabel.generate2018() );
}
//scroll the ingredients of the innerheight is > $localSettings.scrollHeightComparison and the settings showIngredients and scrollLongIngredients are true
if ($localSettings.showIngredients && $localSettings.scrollLongIngredients){
//updateScrollingFeature($localElem, $localSettings, localIDToScroll, localScrollHeightComparison, localScrollHeight)
updateScrollingFeature($elem, $localSettings, ingredientListID, $localSettings.scrollHeightComparison, $localSettings.scrollHeightPixel);
}
//scroll the disclaimer if the height of the disclaimer div is greater than scrollDisclaimerHeightComparison
if ($localSettings.showDisclaimer){
//updateScrollingFeature($localElem, $localSettings, localIDToScroll, localScrollHeightComparison, localScrollHeight)
updateScrollingFeature($elem, $localSettings, calcDisclaimerTextID, $localSettings.scrollDisclaimerHeightComparison, $localSettings.scrollDisclaimer);
}
//this code is for pages with multiple nutrition labels generated by the plugin like the demo page
notApplicableHover($elem);
//add a scroll on long item names
if ($localSettings.scrollLongItemName){
addScrollToItemDiv($elem, $localSettings, nameElementClass, forLegacyLabel);
}
if (!forInitialization){
return $localSettings;
}
}//end of => updateValuesAfterAQuantityChanged($localSettings, $elem, ingredientListID, calcDisclaimerTextID, forLegacyLabel, forInitialization)
function handleQuantityChange($localSettings, source, previousValue, newValue) {
var handler;
if ($localSettings.userFunctionOnQuantityChange) {
handler = $localSettings.userFunctionOnQuantityChange;
} else if ($localSettings.userFunctionNameOnQuantityChange) {
handler = window[$localSettings.userFunctionNameOnQuantityChange];
}
if (typeof handler === 'function') {
handler(source, previousValue, newValue);
}
}
function changeQuantityTextbox($thisTextbox, $localSettings, nutritionLabel, $elem, forLegacyLabel){
var nixLabelBeforeQuantityID = 'nixLabelBeforeQuantity';
if (!forLegacyLabel){
nixLabelBeforeQuantityID = 'nf-nixLabelBeforeQuantity';
}
var previousValue = parseFloat( $('#' +$elem.attr('id') + ' #' + nixLabelBeforeQuantityID).val() );
textBoxValue = !regIsPosNumber( $thisTextbox.val() ) ? previousValue : parseFloat( $thisTextbox.val() );
$thisTextbox.val( textBoxValue.toFixed($localSettings.decimalPlacesForQuantityTextbox) );
$localSettings.valueServingUnitQuantity = textBoxValue;
$localSettings = updateValuesAfterAQuantityChanged($localSettings, nutritionLabel, $elem, forLegacyLabel, false);
if ($localSettings.allowGoogleAnalyticsEventLog){
window[$localSettings.gooleAnalyticsFunctionName](
'send',
'event',
$localSettings.textGoogleAnalyticsEventCategory,
$localSettings.textGoogleAnalyticsEventActionTextbox
);
}
handleQuantityChange(
$localSettings,
'textbox',
previousValue.toFixed($localSettings.decimalPlacesForQuantityTextbox),
textBoxValue.toFixed($localSettings.decimalPlacesForQuantityTextbox)
);
}//end of => function changeQuantityTextbox($thisTextbox, $localSettings, nutritionLabel, $elem, forLegacyLabel)
function changeQuantityByArrow($thisQuantity, changeValueBy, $localSettings, nutritionLabel, $elem, forLegacyLabel){
var unitQuantityBoxClass = 'unitQuantityBox';
if (!forLegacyLabel){
unitQuantityBoxClass = 'nf-unitQuantityBox';
}
//get the current user quantity of the item
var currentQuantity = parseFloat( $thisQuantity.parent().parent().find('input.' + unitQuantityBoxClass).val() );
if ( isNaN(currentQuantity) ){
currentQuantity = 1.0;
}
var beforeCurrentQuantityWasChanged = currentQuantity;
//see https://github.com/nutritionix/nutrition-label/issues/14 for an explanation on this part
if (currentQuantity <= 1 && changeValueBy == -1){
changeValueBy = -0.5;
currentQuantity += changeValueBy;
}else if (currentQuantity < 1 && changeValueBy == 1){
changeValueBy = 0.5;
currentQuantity += changeValueBy;
}else if (currentQuantity <= 2 && currentQuantity > 1 && changeValueBy == -1){
currentQuantity = 1;
}else{
currentQuantity += changeValueBy;
}
if (currentQuantity < 0){
currentQuantity = 0;
}
$thisQuantity.parent().parent().find('input.' + unitQuantityBoxClass).val(
currentQuantity.toFixed($localSettings.decimalPlacesForQuantityTextbox)
);
$localSettings.valueServingUnitQuantity = currentQuantity;
$localSettings = updateValuesAfterAQuantityChanged($localSettings, nutritionLabel, $elem, forLegacyLabel, false);
if ($localSettings.allowGoogleAnalyticsEventLog){
if (changeValueBy > 0){
window[$localSettings.gooleAnalyticsFunctionName](
'send',
'event',
$localSettings.textGoogleAnalyticsEventCategory,
$localSettings.textGoogleAnalyticsEventActionUpArrow
);
}else{
window[$localSettings.gooleAnalyticsFunctionName](
'send',
'event',
$localSettings.textGoogleAnalyticsEventCategory,
$localSettings.textGoogleAnalyticsEventActionDownArrow
);
}
}
handleQuantityChange(
$localSettings,
changeValueBy > 0 ? 'up arrow' : 'down arrow',
beforeCurrentQuantityWasChanged,
currentQuantity
);
}//end of => function changeQuantityByArrow($thisQuantity, changeValueBy, $localSettings, nutritionLabel, $elem, forLegacyLabel)
//round the value to the nearest number
function roundToNearestNum(input, nearest){
if (nearest < 0){
return Math.round(input * nearest) / nearest;
}else{
return Math.round(input / nearest) * nearest;
}
}
function roundCalories(toRound, decimalPlace){
toRound = roundCaloriesRule(toRound);
if (toRound > 0){
toRound = parseFloat( toRound.toFixed(decimalPlace) );
}
return toRound;
}
function roundFat(toRound, decimalPlace){
toRound = roundFatRule(toRound);
if (toRound > 0){
toRound = parseFloat( toRound.toFixed(decimalPlace) );
}
return toRound;
}
function roundSodium(toRound, decimalPlace){
toRound = roundSodiumRule(toRound);
if (toRound > 0){
toRound = parseFloat( toRound.toFixed(decimalPlace) );
}
return toRound;
}