added initial infrastructure for modals in angular

This commit is contained in:
hamza zia 2013-01-29 22:52:30 +05:00
parent 567d6f8179
commit 2a18278f43
6 changed files with 100 additions and 32 deletions

View File

@ -47,15 +47,17 @@
<script src="js/services/deps.js"></script> <script src="js/services/deps.js"></script>
<script src="js/services/base64.js"></script> <script src="js/services/base64.js"></script>
<script src="js/services/utils.js"></script> <script src="js/services/utils.js"></script>
<script src="js/services/modals.js"></script>
<script src="js/services/rpc/jsoncall.js"></script> <script src="js/services/rpc/jsoncall.js"></script>
<script src="js/services/rpc/sockcall.js"></script> <script src="js/services/rpc/sockcall.js"></script>
<script src="js/services/rpc/syscall.js"></script> <script src="js/services/rpc/syscall.js"></script>
<script src="js/services/rpc/rpc.js"></script> <script src="js/services/rpc/rpc.js"></script>
<script src="js/services/rpc/helpers.js"></script>
<script src="js/ctrls/nav.js"></script>
<script src="js/ctrls/modal.js"></script> <script src="js/ctrls/modal.js"></script>
<script src="js/ctrls/nav.js"></script>
<script src="js/ctrls/download.js"></script> <script src="js/ctrls/download.js"></script>
@ -87,7 +89,7 @@
</a> </a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li> <li>
<a href="#" data-toggle="modal" data-target=".modal-adduris"> <a href="#" ng-click="addUris()">
<i class="icon-download"></i> By URIs <i class="icon-download"></i> By URIs
</a> </a>
</li> </li>
@ -292,7 +294,7 @@
<div ng-controller="ModalCtrl"> <div ng-controller="ModalCtrl">
<!--{{{ add new Download template --> <!--{{{ add new Download template -->
<div class="modal hide modal-adduris"> <div class="modal hide modal-adduris" modal="getUris.shown">
<div class="modal-header"> <div class="modal-header">
<button class="close" data-dismiss="modal">x</button> <button class="close" data-dismiss="modal">x</button>
<h3>Add Downloads By URIs</h3> <h3>Add Downloads By URIs</h3>
@ -300,7 +302,7 @@
<form class="modal-body"> <form class="modal-body">
<fieldset> <fieldset>
<legend>Download URIs</legend> <legend>Download URIs</legend>
<textarea rows="6" ng-model="uris"></textarea> <textarea rows="6" ng-model="getUris.uris"></textarea>
<p class="help-block"> <p class="help-block">
{{ uris }} <br> {{ uris }} <br>
You can add multiple downloads (files) You can add multiple downloads (files)
@ -328,7 +330,7 @@ http://ex1.com/f2.mp4 http://ex2.com/f2.mp4
</fieldset> </fieldset>
</form> </form>
<div class="modal-footer"> <div class="modal-footer">
<button class="btn" ng-click="addUris()">Add Downloads</button> <button class="btn" ng-click="getUris.finish()">Add Downloads</button>
</div> </div>
</div> </div>
<!-- add new Download template end }}}--> <!-- add new Download template end }}}-->

View File

@ -1,31 +1,42 @@
angular angular
.module('webui.ctrls.modal', [ .module('webui.ctrls.modal', [
'webui.services.rpc', 'webui.services.deps' 'webui.services.rpc', 'webui.services.deps', 'webui.services.modals'
]) ])
.controller('ModalCtrl', ['$_', '$scope', '$rpc', function(_, scope, rpc) { .controller('ModalCtrl', [
scope.uris = ''; '$_', '$scope', '$rpc', '$modals',
scope.addUris = function() { function(_, scope, rpc, modals) {
console.log(scope.uris);
var cnt = 0;
var cb = function() {
cnt--;
if (!cnt) {
// close modal
console.log('closing modal');
}
};
_.chain(scope.uris.trim().split(/\n\r?/g))
.map(function(d) { return d.trim().split(/\s+/g) })
.filter(function(d) { return d.length })
.each(function(uris) {
cnt++;
// passing true to batch all the addUri calls
rpc.once('addUri', [uris], cb, true);
})
// now dispatch all addUri syscalls scope.getUris = {
rpc.forceUpdate(); cb: null,
uris: '',
shown: false,
parse: function() {
return _
.chain(this.uris.trim().split(/\n\r?/g))
.map(function(d) { return d.trim().split(/\s+/g) })
.filter(function(d) { return d.length })
.value();
},
finish: function() {
var uris = this.parse();
this.uris = '';
scope.uris = ''; if (this.cb) this.cb(uris);
this.cb = null;
this.shown = false;
}
}; };
modals.register('getUris', function(cb) {
if (scope.getUris.cb != null && scope.getUris.shown) {
// modal already shown, user is busy
// TODO: get a better method of passing this on
cb([]);
}
else {
scope.getUris.cb = cb;
scope.getUris.shown = true;
};
});
}]); }]);

View File

@ -1,8 +1,19 @@
angular angular
.module('webui.ctrls.nav', ['webui.services.constants', 'webui.services.modals']) .module('webui.ctrls.nav', [
.controller('NavCtrl', ['$scope', '$name', '$modals', function(scope, name, modals) { 'webui.services.constants', 'webui.services.modals',
'webui.services.rpc.helpers'
])
.controller('NavCtrl', [
'$scope', '$name', '$modals', '$rpchelpers',
function(scope, name, modals, rhelpers) {
scope.name = name; scope.name = name;
// initially collapsed in mobile resolution // initially collapsed in mobile resolution
scope.collapsed = true; scope.collapsed = true;
scope.addUris = function() {
modals.invoke('getUris', rhelpers.addUris);
};
}]); }]);

View File

@ -1,6 +1,6 @@
angular.module('webui', [ angular.module('webui', [
'webui.services.utils', 'webui.services.deps', 'webui.services.base64', 'webui.services.utils', 'webui.services.deps', 'webui.services.base64',
'webui.services.constants', 'webui.services.rpc', 'webui.services.constants', 'webui.services.rpc', 'webui.services.modals',
'webui.filters.bytes', 'webui.filters.path', 'webui.filters.bytes', 'webui.filters.path',
'webui.directives.chunkbar', 'webui.directives.dgraph', 'webui.directives.chunkbar', 'webui.directives.dgraph',
'webui.ctrls.download', 'webui.ctrls.nav', 'webui.ctrls.modal' 'webui.ctrls.download', 'webui.ctrls.nav', 'webui.ctrls.modal'
@ -10,6 +10,7 @@ $(function() {
angular.bootstrap(document, [ angular.bootstrap(document, [
// external deps // external deps
'ui.bootstrap.collapse', 'ui.bootstrap.dropdownToggle', 'ui.bootstrap.collapse', 'ui.bootstrap.dropdownToggle',
'ui.bootstrap.modal',
'webui' 'webui'
]) ])
}); });

18
js/services/modals.js Normal file
View File

@ -0,0 +1,18 @@
angular.module('webui.services.modals', [])
.factory('$modals', function() {
var modals = {};
return {
// register a new modal, cb is the function which
// will further recieve the args when called through
// invoke
register: function(name, cb) {
modals[name] = cb;
},
// invoke an already registered modal, false if not found
invoke: function(name, cb) {
if (!modals[name]) return false;
var args = Array.prototype.slice.call(arguments, 1);
return modals[name].apply({}, args);
}
};
});

View File

@ -0,0 +1,25 @@
angular.module('webui.services.rpc.helpers', [
'webui.services.deps', 'webui.services.rpc'
])
.factory('$rpchelpers', ['$_', '$rpc', function(_, rpc) {
return {
addUris: function(uris) {
var cnt = 0;
var cb = function() {
cnt--;
if (!cnt) {
// close modal
console.log('closing modal');
}
};
_.each(uris, function(uri) {
cnt++;
// passing true to batch all the addUri calls
rpc.once('addUri', [uris], cb, true);
});
// now dispatch all addUri syscalls
rpc.forceUpdate();
}
};
}]);