|
| 1 | +import 'package:angular/angular.dart'; |
| 2 | +import 'package:angular/application_factory.dart'; |
| 3 | +import 'dart:math'; |
| 4 | + |
| 5 | +@Controller( |
| 6 | + selector: '[form-controller]', |
| 7 | + publishAs: 'form_ctrl') |
| 8 | +class FormCtrl { |
| 9 | + |
| 10 | + static const String COLOR_HEX = "hex"; |
| 11 | + static const String COLOR_HSL = "hsl"; |
| 12 | + static const String COLOR_RGB = "rgb"; |
| 13 | + static const String COLOR_NAME = "name"; |
| 14 | + |
| 15 | + Scope scope; |
| 16 | + NgForm form; |
| 17 | + List colors = []; |
| 18 | + List lastFormattedColors = []; |
| 19 | + |
| 20 | + FormCtrl(Scope this.scope, NgForm this.form) { |
| 21 | + newColor(COLOR_HEX, '#222'); |
| 22 | + newColor(COLOR_HEX, '#444'); |
| 23 | + newColor(COLOR_HEX, '#000'); |
| 24 | + } |
| 25 | + |
| 26 | + get color_types => [COLOR_RGB, COLOR_HSL, COLOR_HEX, COLOR_NAME]; |
| 27 | + get resolutions => [ |
| 28 | + '1024x600', |
| 29 | + '1280x800', |
| 30 | + '1366x768', |
| 31 | + '1440x900', |
| 32 | + '1600x900', |
| 33 | + '1680x1050', |
| 34 | + '1920x1080', |
| 35 | + '1920x1200', |
| 36 | + '2560x1440', |
| 37 | + '2560x1600' |
| 38 | + ]; |
| 39 | + |
| 40 | + submit() { |
| 41 | + this.form.reset(); |
| 42 | + } |
| 43 | + |
| 44 | + getTotalSquares(value) { |
| 45 | + int defaultValue = 4; |
| 46 | + if(value != null) { |
| 47 | + try { |
| 48 | + value = double.parse(value.toString()); |
| 49 | + } catch(e) { |
| 50 | + value = defaultValue; |
| 51 | + } |
| 52 | + } else { |
| 53 | + value = defaultValue; |
| 54 | + } |
| 55 | + return (value * value).toInt(); |
| 56 | + } |
| 57 | + |
| 58 | + formatColors() { |
| 59 | + var formatted = []; |
| 60 | + colors.forEach((color) { |
| 61 | + var value = null; |
| 62 | + switch(color['type']) { |
| 63 | + case COLOR_HEX: |
| 64 | + value = color['hex']; |
| 65 | + break; |
| 66 | + case COLOR_HSL: |
| 67 | + var hue = color['hue']; |
| 68 | + var saturation = color['saturation']; |
| 69 | + var luminance = color['luminance']; |
| 70 | + if(hue != null && saturation != null && luminance != null) { |
| 71 | + value = "hsl($hue, $saturation%, $luminance%)"; |
| 72 | + } |
| 73 | + break; |
| 74 | + case COLOR_RGB: |
| 75 | + var red = color['red']; |
| 76 | + var blue = color['blue']; |
| 77 | + var green = color['green']; |
| 78 | + if(red != null && green != null && blue != null) { |
| 79 | + value = "rgb($red, $green, $blue)"; |
| 80 | + } |
| 81 | + break; |
| 82 | + default: //COLOR_NAME |
| 83 | + value = color['name']; |
| 84 | + break; |
| 85 | + } |
| 86 | + if(value != null) { |
| 87 | + formatted.add(value); |
| 88 | + } |
| 89 | + }); |
| 90 | + lastFormattedColors = formatted; |
| 91 | + return formatted; |
| 92 | + } |
| 93 | + |
| 94 | + newColor([String type = COLOR_HEX, String color]) { |
| 95 | + var data = { |
| 96 | + 'id' : colors.length, |
| 97 | + 'type' : type, |
| 98 | + 'hex' : '', |
| 99 | + 'hue' : '', |
| 100 | + 'saturation' : '', |
| 101 | + 'luminance' : '', |
| 102 | + 'red' : '', |
| 103 | + 'green' : '', |
| 104 | + 'blue': '', |
| 105 | + 'name': '' |
| 106 | + }; |
| 107 | + if(type == COLOR_HEX) { |
| 108 | + data['hex'] = color; |
| 109 | + } |
| 110 | + colors.add(data); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +@Controller( |
| 115 | + selector: '[preview-controller]', |
| 116 | + publishAs: 'preview') |
| 117 | +class PreviewCtrl { |
| 118 | + |
| 119 | + static const DEFAULT_COLOR = '#555'; |
| 120 | + |
| 121 | + List _collection = []; |
| 122 | + |
| 123 | + expandList(items, limit) { |
| 124 | + _collection.clear(); |
| 125 | + if(items != null && items.length > 0) { |
| 126 | + for (var i = 0; i < limit; i++) { |
| 127 | + var x = i % items.length; |
| 128 | + _collection.add(items[x]); |
| 129 | + } |
| 130 | + } |
| 131 | + return _collection; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +class FormModule extends Module { |
| 136 | + FormModule() { |
| 137 | + type(FormCtrl); |
| 138 | + type(PreviewCtrl); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +main() { |
| 143 | + applicationFactory().addModule(new FormModule()).run(); |
| 144 | +} |
0 commit comments