|
| 1 | +/* |
| 2 | + * weh - WebExtensions Helper |
| 3 | + * |
| 4 | + * @summary workflow and base code for developing WebExtensions browser add-ons |
| 5 | + * @author Michel Gutierrez |
| 6 | + * @link https://github.com/mi-g/weh |
| 7 | + * |
| 8 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 9 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 10 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 11 | + */ |
| 12 | + |
| 13 | +var weh_prefs_paramIndex = 1; |
| 14 | + |
| 15 | +var prefsApp = angular.module('weh.prefs',[]); |
| 16 | + |
| 17 | +prefsApp.controller('wehParams',["$scope",function($scope) { |
| 18 | + $scope.prefs = { |
| 19 | + values: weh.prefs.getAll(), |
| 20 | + saved: weh.prefs.getAll() |
| 21 | + }; |
| 22 | + |
| 23 | + function UpdateSpecs(specs) { |
| 24 | + $scope.prefs.specs = specs; |
| 25 | + for(var spec in specs) { |
| 26 | + if(typeof $scope.prefs.values[spec]=="undefined") |
| 27 | + $scope.prefs.values[spec] = specs[spec].defaultValue; |
| 28 | + if(typeof $scope.prefs.saved[spec]=="undefined") |
| 29 | + $scope.prefs.saved[spec] = specs[spec].defaultValue; |
| 30 | + } |
| 31 | + $scope.$apply("prefs"); |
| 32 | + } |
| 33 | + weh.prefs.on({specs:true,pack:true},UpdateSpecs); |
| 34 | + $scope.$on("$destroy",function() { |
| 35 | + weh.prefs.off(UpdateSpecs); |
| 36 | + }); |
| 37 | + |
| 38 | + function UpdatePrefs(prefs) { |
| 39 | + Object.assign($scope.prefs.values,prefs); |
| 40 | + Object.assign($scope.prefs.saved,prefs); |
| 41 | + $scope.$apply("prefs") |
| 42 | + } |
| 43 | + weh.prefs.on({pack:true},UpdatePrefs); |
| 44 | + $scope.$on("$destroy",function() { |
| 45 | + weh.prefs.off(UpdatePrefs); |
| 46 | + }); |
| 47 | + |
| 48 | + $scope.canCancel = function() { |
| 49 | + for(var param in $scope.prefs.values) { |
| 50 | + if($scope.prefs.values[param]!=$scope.prefs.saved[param]) |
| 51 | + return true; |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
| 55 | + $scope.canDefault = function() { |
| 56 | + if(!$scope.prefs.specs) |
| 57 | + return false; |
| 58 | + for(var param in $scope.prefs.values) { |
| 59 | + if($scope.prefs.values[param]!=$scope.prefs.specs[param].defaultValue) |
| 60 | + return true; |
| 61 | + } |
| 62 | + return false; |
| 63 | + } |
| 64 | + $scope.canSave = function() { |
| 65 | + for(var param in $scope.prefs.values) |
| 66 | + if(!weh.prefs.isValid(param,$scope.prefs.values[param])) |
| 67 | + return false; |
| 68 | + for(var param in $scope.prefs.values) { |
| 69 | + if($scope.prefs.values[param]!=$scope.prefs.saved[param]) |
| 70 | + return true; |
| 71 | + } |
| 72 | + return false; |
| 73 | + } |
| 74 | + $scope.handleCancel = function() { |
| 75 | + for(var param in $scope.prefs.values) |
| 76 | + $scope.prefs.values[param] = $scope.prefs.saved[param]; |
| 77 | + } |
| 78 | + $scope.handleDefault = function() { |
| 79 | + for(var param in $scope.prefs.values) |
| 80 | + $scope.prefs.values[param] = $scope.prefs.specs[param].defaultValue; |
| 81 | + } |
| 82 | + $scope.handleSave = function() { |
| 83 | + for(var param in $scope.prefs.values) { |
| 84 | + var value = $scope.prefs.values[param]; |
| 85 | + weh.prefs[param] = value; |
| 86 | + $scope.prefs.saved[param] = value; |
| 87 | + } |
| 88 | + } |
| 89 | +}]); |
| 90 | + |
| 91 | +prefsApp.directive('wehPrefButtons',function() { |
| 92 | + return { |
| 93 | + restrict: "E", |
| 94 | + scope: true, |
| 95 | + replace: true, |
| 96 | + template: function(elem,attr) { |
| 97 | + return ` |
| 98 | + <div class="text-center"> |
| 99 | + <br/> |
| 100 | + <div class="btn-toolbar" ng-style="{display: 'inline-block'}"> |
| 101 | + <button type="button" |
| 102 | + ng-click="handleCancel()" |
| 103 | + ng-class="{btn:1, 'btn-default':1, disabled: !canCancel()}"> |
| 104 | + {{_("cancel")}} |
| 105 | + </button> |
| 106 | + <button type="button" |
| 107 | + ng-click="handleDefault()" |
| 108 | + ng-class="{btn:1, 'btn-warning':1, disabled: !canDefault()}"> |
| 109 | + {{_("default")}} |
| 110 | + </button> |
| 111 | + <button type="button" |
| 112 | + ng-click="handleSave()" |
| 113 | + ng-class="{btn:1, 'btn-primary':1, disabled: !canSave()}"> |
| 114 | + {{_("save")}} |
| 115 | + </button> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + ` |
| 119 | + }, |
| 120 | + link: function(scope,element,attrs) { |
| 121 | + } |
| 122 | + } |
| 123 | +}); |
| 124 | + |
| 125 | +prefsApp.directive('wehParamSet',function() { |
| 126 | + return { |
| 127 | + restrict: "E", |
| 128 | + scope: true, |
| 129 | + replace: true, |
| 130 | + template: function(elem,attr) { |
| 131 | + return ` |
| 132 | + <form class="form-horizontal" role="form"> |
| 133 | + <weh-param |
| 134 | + ng-repeat='param in params' |
| 135 | + param="{{param}}" |
| 136 | + > |
| 137 | + </weh-param> |
| 138 | + </form> |
| 139 | + ` |
| 140 | + }, |
| 141 | + link: function(scope,element,attrs) { |
| 142 | + scope.params = attrs.params.split(","); |
| 143 | + } |
| 144 | + } |
| 145 | +}); |
| 146 | + |
| 147 | +prefsApp.directive('wehParam',function() { |
| 148 | + return { |
| 149 | + restrict: "E", |
| 150 | + scope: true, |
| 151 | + replace: true, |
| 152 | + require: "?^ngController", |
| 153 | + template: ` |
| 154 | + <div ng-class="formGroupClass()" ng-show="spec"> |
| 155 | + <label class="col-sm-4 control-label" for="weh-param-{{this.paramIndex}}"> |
| 156 | + {{spec.label}}</label> |
| 157 | + <div class="col-sm-8"> |
| 158 | +
|
| 159 | + <input |
| 160 | + ng-if="['string','integer','float'].indexOf(spec.type)>=0" |
| 161 | + ng-model="prefs.values[prefName]" |
| 162 | + class="form-control" |
| 163 | + maxLength="{{spec.maxLength || -1}}" |
| 164 | + id="weh-param-{{paramIndex}}" |
| 165 | + type="text" |
| 166 | + ng-style="{ width: getInputWidth() }"/> |
| 167 | +
|
| 168 | + <input |
| 169 | + ng-if="spec.type=='boolean'" |
| 170 | + class="form-control" |
| 171 | + ng-model="prefs.values[prefName]" |
| 172 | + id="weh-param-{{paramIndex}}" |
| 173 | + type="checkbox" |
| 174 | + ng-style="{width:'34px'}" |
| 175 | + /> |
| 176 | +
|
| 177 | + <select |
| 178 | + ng-if="spec.type=='choice' && spec.choices.length>0" |
| 179 | + ng-model="prefs.values[prefName]" |
| 180 | + class="form-control" |
| 181 | + id="weh-param-{{paramIndex}}" |
| 182 | + ng-style="{ width: getInputWidth() }"> |
| 183 | + <option |
| 184 | + ng-repeat="option in spec.choices" |
| 185 | + value="{{option.value}}"> |
| 186 | + {{ option.name }} |
| 187 | + </option> |
| 188 | + </select> |
| 189 | +
|
| 190 | + <div class="help-block" ng-show="spec.description"> |
| 191 | + {{ spec.description }} |
| 192 | + </div> |
| 193 | + </div> |
| 194 | + </div> |
| 195 | + `, |
| 196 | + link: function(scope,element,attrs,ctrl) { |
| 197 | + scope.paramIndex = weh_prefs_paramIndex++; |
| 198 | + scope.prefName = attrs.param; |
| 199 | + scope.spec = {}; |
| 200 | + function UpdateSpec(param,spec) { |
| 201 | + scope.spec = spec; |
| 202 | + scope.$apply("spec") |
| 203 | + } |
| 204 | + weh.prefs.on(attrs.param,{specs:true},UpdateSpec); |
| 205 | + scope.$on("$destroy",function() { |
| 206 | + weh.prefs.off(attrs.params,UpdateSpec); |
| 207 | + }); |
| 208 | + scope.formGroupClass = function() { |
| 209 | + var value = scope.prefs.values[scope.prefName]; |
| 210 | + var classes = ["form-group"]; |
| 211 | + if(!scope.spec || !weh.prefs.isValid(scope.prefName,value)) |
| 212 | + classes.push("has-error"); |
| 213 | + else if(value != scope.prefs.saved[scope.prefName]) |
| 214 | + classes.push("has-success"); |
| 215 | + else if(value != scope.spec.defaultValue) |
| 216 | + classes.push("has-warning"); |
| 217 | + |
| 218 | + return classes.join(" "); |
| 219 | + } |
| 220 | + scope.getInputWidth = function() { |
| 221 | + switch(scope.spec.type) { |
| 222 | + case "string": |
| 223 | + return scope.spec.width || "20em"; |
| 224 | + case "integer": |
| 225 | + case "float": |
| 226 | + return scope.spec.width || "8em"; |
| 227 | + case "boolean": |
| 228 | + return "34px"; |
| 229 | + case "choice": |
| 230 | + return scope.spec.width || "12em"; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + } |
| 235 | + } |
| 236 | +}); |
| 237 | + |
| 238 | +prefsApp.directive('wehVersion',function() { |
| 239 | + return { |
| 240 | + restrict: "E", |
| 241 | + scope: true, |
| 242 | + replace: true, |
| 243 | + template: function(elem,attr) { |
| 244 | + return ` |
| 245 | + <form class="form-horizontal"> |
| 246 | + <div class="form-group"> |
| 247 | + <label class="col-sm-4 control-label"> |
| 248 | + {{_("version")}} |
| 249 | + </label> |
| 250 | + <div class="col-sm-8"> |
| 251 | + <p class="form-control-static">{{versionName}}</p> |
| 252 | + </div> |
| 253 | + </div> |
| 254 | + </form> |
| 255 | + ` |
| 256 | + }, |
| 257 | + link: function(scope,element,attrs) { |
| 258 | + var manifest = browser.runtime.getManifest(); |
| 259 | + var version = manifest.version; |
| 260 | + scope.versionName = manifest.version_name; |
| 261 | + if(scope.versionName) |
| 262 | + if(version && version!=scope.versionName) { |
| 263 | + scope.versionName += " (" + version + ")"; |
| 264 | + } |
| 265 | + else |
| 266 | + scope.versionName = version; |
| 267 | + } |
| 268 | + } |
| 269 | +}); |
| 270 | + |
0 commit comments