1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-30 11:24:21 +01:00
fab-manager/app/assets/javascripts/directives/bs-jasny-fileinput.js

35 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-11-21 11:08:53 +01:00
'use strict';
2015-05-05 03:10:25 +02:00
2018-11-21 10:59:07 +01:00
Application.Directives.directive('bsJasnyFileinput', [function () {
2015-05-05 03:10:25 +02:00
return {
2018-11-21 10:59:07 +01:00
require: ['ngModel'],
link: function ($scope, elm, attrs, requiredCtrls) {
2018-11-21 11:08:53 +01:00
var ngModelCtrl = requiredCtrls[0];
var fileinput = elm.parents('[data-provides=fileinput]');
var filetypeRegex = attrs.bsJasnyFileinput;
2018-11-21 10:59:07 +01:00
fileinput.on('clear.bs.fileinput', function (e) {
if (ngModelCtrl) {
2018-11-21 11:08:53 +01:00
ngModelCtrl.$setViewValue(null);
ngModelCtrl.$setPristine();
$scope.$apply();
2015-05-05 03:10:25 +02:00
}
2018-11-21 11:08:53 +01:00
});
2018-11-21 10:59:07 +01:00
fileinput.on('change.bs.fileinput', function (e, files) {
if (ngModelCtrl) {
if (files) {
2018-11-21 11:08:53 +01:00
ngModelCtrl.$setViewValue(files.result);
2015-05-05 03:10:25 +02:00
} else {
2018-11-21 11:08:53 +01:00
ngModelCtrl.$setPristine();
2015-05-05 03:10:25 +02:00
}
// TODO: ne marche pas pour filetype
if (filetypeRegex) {
2018-11-21 11:08:53 +01:00
if (files && typeof files.type !== 'undefined' && files.type.match(new RegExp(filetypeRegex))) { ngModelCtrl.$setValidity('filetype', true); } else { ngModelCtrl.$setValidity('filetype', false); }
2016-03-23 18:39:41 +01:00
};
2015-05-05 03:10:25 +02:00
}
2018-11-21 11:08:53 +01:00
$scope.$apply();
});
2015-05-05 03:10:25 +02:00
}
2018-11-21 11:08:53 +01:00
};
}]);