Revision f7e51fc5 snf-cyclades-app/synnefo/ui/static/snf/js/ui/web/ui_main_view.js
b/snf-cyclades-app/synnefo/ui/static/snf/js/ui/web/ui_main_view.js | ||
---|---|---|
773 | 773 |
|
774 | 774 |
load_user_quotas: function() { |
775 | 775 |
var main_view = this; |
776 |
if (!snf.user.quota) { |
|
777 |
snf.user.quota = new snf.quota.Quota("cyclades"); |
|
778 |
main_view.init_quotas_handlers(); |
|
779 |
} |
|
780 |
|
|
776 | 781 |
snf.api.sync('read', undefined, { |
777 | 782 |
url: synnefo.config.quota_url, |
778 | 783 |
success: function(d) { |
779 |
snf.user.quotas = {}; |
|
780 |
snf.user.quotas['vms'] = d.vms_quota; |
|
781 |
snf.user.quotas['networks'] = d.networks_quota; |
|
782 |
if (!main_view.quota_handlers_initialized) { |
|
783 |
main_view.init_quotas_handlers(['vms','networks']); |
|
784 |
main_view.quota_handlers_initialized = true; |
|
785 |
} |
|
786 |
try { |
|
787 |
main_view.check_quotas('vms'); |
|
788 |
main_view.check_quotas('networks'); |
|
789 |
} catch (err) { |
|
790 |
console.error(err); |
|
791 |
} |
|
784 |
snf.user.quota.load(d); |
|
792 | 785 |
}, |
793 | 786 |
complete: function() { |
794 | 787 |
setTimeout(function(){ |
... | ... | |
801 | 794 |
check_quotas: function(type) { |
802 | 795 |
var storage = synnefo.storage[type]; |
803 | 796 |
var consumed = storage.length; |
797 |
var quotakey = { |
|
798 |
'networks': 'cyclades.network.private', |
|
799 |
'vms': 'cyclades.vm' |
|
800 |
} |
|
804 | 801 |
if (type == "networks") { |
805 | 802 |
consumed = storage.filter(function(net){ |
806 | 803 |
return !net.is_public() && !net.is_deleted(); |
807 | 804 |
}).length; |
808 | 805 |
} |
809 |
if (snf.user.quotas && consumed >= snf.user.quotas[type]) { |
|
806 |
|
|
807 |
var limit = snf.user.quota.get_limit(quotakey[type]); |
|
808 |
if (snf.user.quota && snf.user.quota.data && consumed >= limit) { |
|
810 | 809 |
storage.trigger("quota_reached"); |
811 | 810 |
} else { |
812 | 811 |
storage.trigger("quota_free"); |
813 | 812 |
} |
814 | 813 |
}, |
815 | 814 |
|
816 |
init_quotas_handlers: function(types) { |
|
817 |
var self = this; |
|
818 |
_.each(types, function(type) { |
|
819 |
var storage = synnefo.storage[type]; |
|
820 |
if (!storage) { return }; |
|
821 |
var check_quotas = function() { |
|
822 |
self.check_quotas(type); |
|
823 |
} |
|
824 |
storage.bind("add", check_quotas); |
|
825 |
storage.bind("remove", check_quotas); |
|
826 |
check_quotas(); |
|
827 |
}) |
|
815 |
init_quotas_handlers: function() { |
|
816 |
var self = this, event; |
|
817 |
snf.user.quota.bind("cyclades.vm.quota.changed", function() { |
|
818 |
this.check_quotas("vms"); |
|
819 |
}, this); |
|
820 |
|
|
821 |
var event = "cyclades.network.private.quota.changed"; |
|
822 |
snf.user.quota.bind(event, function() { |
|
823 |
this.check_quotas("networks"); |
|
824 |
}, this); |
|
828 | 825 |
}, |
829 | 826 |
|
830 | 827 |
// initial view based on user cookie |
... | ... | |
862 | 859 |
$("#createcontainer #create").attr("title", ""); |
863 | 860 |
}); |
864 | 861 |
|
865 |
this.check_quotas('vms'); |
|
866 | 862 |
}, |
867 | 863 |
|
868 | 864 |
check_empty: function() { |
Also available in: Unified diff