├── .meteor ├── release ├── .gitignore └── packages ├── public └── loudev │ ├── switch.png │ ├── multi-select.css │ └── jquery.multi-select.min.js ├── smart.json ├── .gitignore ├── client ├── style.css ├── lib │ ├── jquery.serializeObject.js │ ├── jquery.browser.mobile.js │ └── yepnope.1.5.4-min.js ├── client.js ├── manage-roles.js └── client.html ├── LICENSE ├── server └── server.js └── README.md /.meteor/release: -------------------------------------------------------------------------------- 1 | 0.6.4.1 2 | -------------------------------------------------------------------------------- /.meteor/.gitignore: -------------------------------------------------------------------------------- 1 | local 2 | meteorite 3 | -------------------------------------------------------------------------------- /public/loudev/switch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alanning/meteor-modal-example/HEAD/public/loudev/switch.png -------------------------------------------------------------------------------- /smart.json: -------------------------------------------------------------------------------- 1 | { 2 | "meteor": { 3 | "tag": "release/0.6.4.1" 4 | }, 5 | "packages": { 6 | "bootboxjs": {}, 7 | "roles": {} 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | *.swp 10 | *.bak 11 | 12 | pids 13 | logs 14 | results 15 | 16 | smart.lock 17 | npm-debug.log 18 | -------------------------------------------------------------------------------- /.meteor/packages: -------------------------------------------------------------------------------- 1 | # Meteor packages used by this project, one per line. 2 | # 3 | # 'meteor add' and 'meteor remove' will edit this file for you, 4 | # but you can also edit it by hand. 5 | 6 | insecure 7 | preserve-inputs 8 | bootboxjs 9 | accounts-password 10 | roles 11 | -------------------------------------------------------------------------------- /client/style.css: -------------------------------------------------------------------------------- 1 | .user-list, 2 | .roles { 3 | list-style:none; 4 | } 5 | .user-list > li { 6 | padding:1em; 7 | } 8 | .name { 9 | font-size:1.4em; 10 | padding-bottom:0.1em; 11 | } 12 | .help { 13 | color:#555; 14 | padding: 0.4em 0; 15 | } 16 | .roles { 17 | cursor:pointer; 18 | } 19 | .roles li { 20 | border: 1px solid #667; 21 | border-radius:0.5em; 22 | background-color:#e2ffe7; 23 | display:inline-block; 24 | padding:0.2em 0.6em; 25 | } 26 | -------------------------------------------------------------------------------- /client/lib/jquery.serializeObject.js: -------------------------------------------------------------------------------- 1 | // object representation of HTML form 2 | // http://stackoverflow.com/questions/1184624/convert-form-data-to-js-object-with-jquery 3 | $.fn.serializeObject = function() 4 | { 5 | var o = {}, 6 | a = this.serializeArray(); 7 | 8 | $.each(a, function() { 9 | var name = this.name 10 | if (o[name] !== undefined) { 11 | if (!o[name].push) { 12 | o[name] = [o[name]]; 13 | } 14 | o[name].push(this.value || ''); 15 | } else { 16 | o[name] = this.value || ''; 17 | } 18 | }); 19 | 20 | return o; 21 | }; 22 | -------------------------------------------------------------------------------- /client/client.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | Meteor.startup(function () { 4 | Meteor.subscribe('users') 5 | }) 6 | 7 | 8 | Template.userList.events({ 9 | 'click .name a': manageRolesClicked, 10 | 'click .roles': manageRolesClicked 11 | }) 12 | 13 | Template.userList.helpers({ 14 | users: function () { 15 | return Meteor.users.find() 16 | }, 17 | mobile: function () { 18 | var profile = this.profile, 19 | mobile = '' 20 | 21 | if (profile) 22 | mobile = profile.mobile 23 | 24 | if ("null" === mobile || !mobile) 25 | mobile = '' 26 | 27 | return mobile 28 | }, 29 | emailAddress: function () { 30 | var emails = this.emails 31 | 32 | if (emails && emails.length > 0) { 33 | return emails[0].address 34 | } 35 | 36 | return "" 37 | } 38 | }) 39 | 40 | function manageRolesClicked (evt) { 41 | var $person, 42 | userId 43 | 44 | evt.preventDefault() 45 | 46 | $person = $(evt.target).closest('.person') 47 | userId = $person.attr('data-id') 48 | Session.set('selectedUserId', userId) 49 | $('#user-roles').modal() 50 | } 51 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Adrian Lanning 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /server/server.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | Meteor.startup(function () { 4 | var user = Meteor.users.findOne() 5 | if (!user) createUsers() 6 | 7 | Meteor.publish('users', function () { 8 | // profile is automatically published for 9 | // logged-in user only so need to publish 10 | // that too 11 | return Meteor.users.find({},{ 12 | fields:{ 13 | emails:1, 14 | profile:1, 15 | roles:1 16 | }, 17 | sort:{ 18 | "profile.lastname":1 19 | } 20 | }) 21 | }) 22 | 23 | Meteor.methods({ 24 | updateRoles: function (targetUserId, roles) { 25 | check(targetUserId, String) 26 | check(roles, [String]) 27 | 28 | // var loggedInUser = Meteor.user() 29 | // if (!Auth.authorized('updateRoles', loggedInUser)) 30 | // throw new Meteor.Error(400, "Not authorized") 31 | 32 | Meteor.users.update({_id:targetUserId}, { 33 | $set: { 34 | roles: roles 35 | } 36 | }) 37 | } 38 | }) 39 | 40 | 41 | }) // end Meteor.startup 42 | 43 | 44 | function createUsers () { 45 | var i = 0, 46 | id, 47 | ids = [] 48 | 49 | for (; i < 10; i++) { 50 | id = Accounts.createUser({ 51 | email: "user" + i + "@example.com", 52 | password: "password", 53 | profile: { 54 | firstname: "User", 55 | lastname: i 56 | } 57 | }) 58 | 59 | ids.push(id) 60 | } 61 | 62 | 63 | // add roles 64 | Roles.addUsersToRoles(ids, 'read') 65 | } 66 | 67 | 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | meteor-modal-example 2 | ==================== 3 | 4 | Example Meteor app illustrating: 5 | 6 | * modal dialogs ([bootboxjs][2]) 7 | * mobile detection ([detectmobilebrowser][5] + [yepnope][4]) 8 | * beautiful multi-select ([loudev jquery plugin][3]) 9 | 10 | 11 | ### How to run 12 | 13 | 1. Install [Meteorite][1] 14 | 15 | 2. Get the example code: 16 | ```bash 17 | git clone https://github.com/alanning/meteor-modal-example.git 18 | ``` 19 | 20 | 3. Run using Meteorite (rather than the normal `meteor`): 21 | ```bash 22 | cd meteor-modal-example 23 | mrt 24 | ``` 25 | 26 | 4. Point some browsers to `http://localhost:3000/` 27 | 28 | 29 | ### Live Demo 30 | 31 | http://modal-example.meteor.com/ 32 | 33 | 34 | ### Y U NO Package?! 35 | 36 | We do! This example uses the [bootboxjs][2] smart package available up on Atmosphere. 37 | 38 | Did you mean the awesome multiselect part? Oh, well in that case... 39 | 40 | The [loudev multiselect library][3], while fantastic, is not appropriate for mobile devices. Currently the Meteor packaging system is not able to optionally load files based on device type so it's better to stick it in the public directory and load it as needed. This example uses [yepnope][4] to dynamically load the loudev library when appropriate. 41 | 42 | 43 | 44 | 45 | [1]: https://github.com/oortcloud/meteorite "Meteorite" 46 | [2]: https://atmosphere.meteor.com/package/bootboxjs "bootboxjs" 47 | [3]: http://loudev.com/ "loudev" 48 | [4]: http://yepnopejs.com/ "yepnope" 49 | [5]: http://detectmobilebrowsers.com/ "detectmobilebrowsers" 50 | -------------------------------------------------------------------------------- /client/lib/jquery.browser.mobile.js: -------------------------------------------------------------------------------- 1 | /** 2 | * jQuery.browser.mobile (http://detectmobilebrowser.com/) 3 | * 4 | * jQuery.browser.mobile will be true if the browser is a mobile device 5 | * 6 | **/ 7 | (function(a){(jQuery.browser=jQuery.browser||{}).mobile=/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))})(navigator.userAgent||navigator.vendor||window.opera); -------------------------------------------------------------------------------- /public/loudev/multi-select.css: -------------------------------------------------------------------------------- 1 | .ms-container{ 2 | background: transparent url('switch.png') no-repeat 170px 80px; 3 | } 4 | 5 | .ms-container:after{ 6 | content: "."; display: block; height: 0; line-height: 0; font-size: 0; clear: both; min-height: 0; visibility: hidden; 7 | } 8 | 9 | .ms-container .ms-selectable, .ms-container .ms-selection{ 10 | 11 | background: #fff; 12 | color: #555555; 13 | float: left; 14 | } 15 | 16 | .ms-container .ms-list{ 17 | -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); 18 | -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); 19 | box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); 20 | -webkit-transition: border linear 0.2s, box-shadow linear 0.2s; 21 | -moz-transition: border linear 0.2s, box-shadow linear 0.2s; 22 | -ms-transition: border linear 0.2s, box-shadow linear 0.2s; 23 | -o-transition: border linear 0.2s, box-shadow linear 0.2s; 24 | transition: border linear 0.2s, box-shadow linear 0.2s; 25 | border: 1px solid #ccc; 26 | -webkit-border-radius: 3px; 27 | -moz-border-radius: 3px; 28 | border-radius: 3px; 29 | } 30 | 31 | 32 | .ms-selected{ 33 | display:none; 34 | } 35 | .ms-container .ms-selectable{ 36 | margin-right: 40px; 37 | } 38 | 39 | .ms-container .ms-list.ms-focus{ 40 | border-color: rgba(82, 168, 236, 0.8); 41 | -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6); 42 | -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6); 43 | box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6); 44 | outline: 0; 45 | outline: thin dotted \9; 46 | } 47 | 48 | .ms-container ul{ 49 | margin: 0; 50 | list-style-type: none; 51 | padding: 0; 52 | } 53 | 54 | .ms-container .ms-optgroup-container{ 55 | width: 100%; 56 | } 57 | 58 | .ms-container ul.ms-list{ 59 | width: 160px; 60 | height: 200px; 61 | padding: 0; 62 | overflow-y: auto; 63 | } 64 | 65 | .ms-container .ms-optgroup-label{ 66 | margin: 0; 67 | padding: 5px 0px 0px 5px; 68 | cursor: pointer; 69 | color: #999; 70 | } 71 | 72 | .ms-container .ms-selectable li.ms-elem-selectable, 73 | .ms-container .ms-selection li.ms-elem-selection{ 74 | border-bottom: 1px #eee solid; 75 | padding: 2px 10px; 76 | color: #555; 77 | font-size: 14px; 78 | } 79 | 80 | .ms-container .ms-selectable li.ms-hover, 81 | .ms-container .ms-selection li.ms-hover{ 82 | cursor: pointer; 83 | color: #fff; 84 | text-decoration: none; 85 | background-color: #08c; 86 | } 87 | 88 | .ms-container .ms-selectable li.disabled, 89 | .ms-container .ms-selection li.disabled{ 90 | background-color: #eee; 91 | color: #aaa; 92 | cursor: text; 93 | } 94 | -------------------------------------------------------------------------------- /client/manage-roles.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | //////////////////////////////////////////////////////////// 4 | // editRolesForm 5 | // 6 | 7 | Template.editRolesForm.rendered = function () { 8 | var userId = Session.get('selectedUserId'), 9 | user, 10 | $options 11 | 12 | if (!userId) return 13 | 14 | user = Meteor.users.findOne({_id: userId}) 15 | if (!user) return 16 | 17 | //console.log('form rendered:', user.profile.lastname) 18 | 19 | $options = $('option', '#roles') 20 | 21 | $options.each(function (index, option) { 22 | var $option = $(option), 23 | val = $option.val(), 24 | hasRole 25 | 26 | hasRole = _.contains(user.roles, val) 27 | 28 | if (hasRole) { 29 | $option.attr('selected', true) 30 | } else { 31 | $option.removeAttr('selected') 32 | } 33 | }) 34 | 35 | renderMultiSelect() 36 | } 37 | 38 | function renderMultiSelect () { 39 | var $roles, 40 | data, 41 | dataExists 42 | 43 | if (!jQuery.fn.multiSelect) return 44 | 45 | // loudev multiselect does not support calling 46 | // $().multiSelect() multiple times. List 47 | // elements and headers will be duplicated. 48 | // 49 | // to avoid this, we make sure to remove the 50 | // 'multiselect' data object if it exists 51 | // so a new, clean multiselect will be 52 | // created each time. 53 | 54 | $roles = $('#roles') 55 | data = $roles.data('multiselect') 56 | dataExists = data ? true : false 57 | 58 | if (dataExists) { 59 | // remove existing multiselect data 60 | $roles.data('multiselect', null) 61 | } 62 | 63 | //console.log('multiSelect re-rendered') 64 | $roles.multiSelect({ 65 | selectableHeader: "Available Roles:", 66 | selectionHeader: "Authorized to:" 67 | }) 68 | } 69 | 70 | Template.editRolesForm.events({ 71 | 'click #saveChanges': function (evt) { 72 | var $form = $('#manage-roles-form'), 73 | data, 74 | roles 75 | 76 | evt.preventDefault() 77 | 78 | data = $form.serializeObject() 79 | 80 | roles = data.roles || [] 81 | if (!_.isArray(roles)) { 82 | // ensure roles is an array 83 | roles = [roles] 84 | } 85 | 86 | Meteor.call('updateRoles', 87 | data._id, 88 | roles, 89 | function (error, result) { 90 | if (error) { 91 | alert(error) 92 | } else { 93 | bootbox.alert('Roles updated', function () { 94 | $('#user-roles').modal('hide') 95 | }) 96 | } 97 | }) 98 | } 99 | }) 100 | 101 | Template.editRolesForm.helpers({ 102 | user: function () { 103 | var userId = Session.get('selectedUserId'), 104 | user = Meteor.users.findOne({_id: userId}) 105 | 106 | //if (user && user.profile) { 107 | // console.log('current user:', user.profile.lastname) 108 | //} 109 | 110 | return user 111 | }, 112 | 113 | allRoles: function () { 114 | var roles = "read,modify,delete,create".split(',') 115 | 116 | return roles 117 | } 118 | }) 119 | -------------------------------------------------------------------------------- /client/client.html: -------------------------------------------------------------------------------- 1 | 2 | meteor-modal-example 3 | 4 | 20 | 21 | 22 | 23 | 24 | Fork me on GitHub 25 | 26 | 27 | {{> userList}} 28 | 29 | 30 | 73 | 74 | 75 | 108 | -------------------------------------------------------------------------------- /client/lib/yepnope.1.5.4-min.js: -------------------------------------------------------------------------------- 1 | /*yepnope1.5.x|WTFPL*/ 2 | (function(a,b,c){function d(a){return"[object Function]"==o.call(a)}function e(a){return"string"==typeof a}function f(){}function g(a){return!a||"loaded"==a||"complete"==a||"uninitialized"==a}function h(){var a=p.shift();q=1,a?a.t?m(function(){("c"==a.t?B.injectCss:B.injectJs)(a.s,0,a.a,a.x,a.e,1)},0):(a(),h()):q=0}function i(a,c,d,e,f,i,j){function k(b){if(!o&&g(l.readyState)&&(u.r=o=1,!q&&h(),l.onload=l.onreadystatechange=null,b)){"img"!=a&&m(function(){t.removeChild(l)},50);for(var d in y[c])y[c].hasOwnProperty(d)&&y[c][d].onload()}}var j=j||B.errorTimeout,l=b.createElement(a),o=0,r=0,u={t:d,s:c,e:f,a:i,x:j};1===y[c]&&(r=1,y[c]=[]),"object"==a?l.data=c:(l.src=c,l.type=a),l.width=l.height="0",l.onerror=l.onload=l.onreadystatechange=function(){k.call(this,r)},p.splice(e,0,u),"img"!=a&&(r||2===y[c]?(t.insertBefore(l,s?null:n),m(k,j)):y[c].push(l))}function j(a,b,c,d,f){return q=0,b=b||"j",e(a)?i("c"==b?v:u,a,b,this.i++,c,d,f):(p.splice(this.i++,0,a),1==p.length&&h()),this}function k(){var a=B;return a.loader={load:j,i:0},a}var l=b.documentElement,m=a.setTimeout,n=b.getElementsByTagName("script")[0],o={}.toString,p=[],q=0,r="MozAppearance"in l.style,s=r&&!!b.createRange().compareNode,t=s?l:n.parentNode,l=a.opera&&"[object Opera]"==o.call(a.opera),l=!!b.attachEvent&&!l,u=r?"object":l?"script":"img",v=l?"script":u,w=Array.isArray||function(a){return"[object Array]"==o.call(a)},x=[],y={},z={timeout:function(a,b){return b.length&&(a.timeout=b[0]),a}},A,B;B=function(a){function b(a){var a=a.split("!"),b=x.length,c=a.pop(),d=a.length,c={url:c,origUrl:c,prefixes:a},e,f,g;for(f=0;f');this.$selectableContainer=$('
');this.$selectionContainer=$('
');this.$selectableUl=$('');this.$selectionUl=$('');this.scrollTo=0};MultiSelect.prototype={constructor:MultiSelect,init:function(){var that=this,ms=this.$element;if(ms.next(".ms-container").length==0){ms.css({position:"absolute",left:"-9999px"});ms.attr("id",ms.attr("id")?ms.attr("id"):"ms-"+Math.ceil(Math.random()*1e3));var optgroupLabel=null,optgroupId=null,optgroupCpt=0,scroll=0,optgroupContainerTemplate='
  • ',optgroupUlTemplate='',optgroupLiTemplate='
  • ';ms.find("optgroup, option").each(function(){if($(this).is("optgroup")){optgroupLabel=""+$(this).attr("label")+"";optgroupId="ms-"+ms.attr("id")+"-optgroup-"+optgroupCpt;var optgroup=$(this),optgroupSelectable=$(optgroupContainerTemplate),optgroupSelection=$(optgroupContainerTemplate),optgroupSelectionLi=$(optgroupLiTemplate),optgroupSelectableLi=$(optgroupLiTemplate);if(that.options.selectableOptgroup){optgroupSelectableLi.on("click",function(){var values=optgroup.children(":not(:selected)").map(function(){return $(this).val()}).get();that.select(values)});optgroupSelectionLi.on("click",function(){var values=optgroup.children(":selected").map(function(){return $(this).val()}).get();that.deselect(values)})}optgroupSelectableLi.html(optgroupLabel);optgroupSelectable.attr("id",optgroupId+"-selectable").append($(optgroupUlTemplate).append(optgroupSelectableLi));that.$selectableUl.append(optgroupSelectable);optgroupSelectionLi.html(optgroupLabel);optgroupSelection.attr("id",optgroupId+"-selection").append($(optgroupUlTemplate).append(optgroupSelectionLi));that.$selectionUl.append(optgroupSelection);optgroupCpt++}else{var attributes="";for(var cpt=0;cpt"+$(this).text()+""),selectedLi=selectableLi.clone();var value=$(this).val(),msId=that.sanitize(value);selectableLi.data("ms-value",value).addClass("ms-elem-selectable").attr("id",msId+"-selectable");selectedLi.data("ms-value",value).addClass("ms-elem-selection").attr("id",msId+"-selection").hide();that.$selectionUl.find(".ms-optgroup-label").hide();if($(this).prop("disabled")||ms.prop("disabled")){if(this.selected){selectedLi.prop("disabled",true);selectedLi.addClass(that.options.disabledClass)}else{selectableLi.prop("disabled",true);selectableLi.addClass(that.options.disabledClass)}}if(optgroupId){that.$selectableUl.children("#"+optgroupId+"-selectable").find("ul").first().append(selectableLi);that.$selectionUl.children("#"+optgroupId+"-selection").find("ul").first().append(selectedLi)}else{that.$selectableUl.append(selectableLi);that.$selectionUl.append(selectedLi)}}});if(that.options.selectableHeader)that.$selectableContainer.append(that.options.selectableHeader);that.$selectableContainer.append(that.$selectableUl);if(that.options.selectableFooter)that.$selectableContainer.append(that.options.selectableFooter);if(that.options.selectionHeader)that.$selectionContainer.append(that.options.selectionHeader);that.$selectionContainer.append(that.$selectionUl);if(that.options.selectionFooter)that.$selectionContainer.append(that.options.selectionFooter);that.$container.append(that.$selectableContainer);that.$container.append(that.$selectionContainer);ms.after(that.$container);that.$selectableUl.on("mouseenter",".ms-elem-selectable",function(){$("li",that.$container).removeClass("ms-hover");$(this).addClass("ms-hover")}).on("mouseleave",function(){$("li",that.$container).removeClass("ms-hover")});if(that.options.dblClick){that.$selectableUl.on("dblclick",".ms-elem-selectable",function(){that.select($(this).data("ms-value"))});that.$selectionUl.on("dblclick",".ms-elem-selection",function(){that.deselect($(this).data("ms-value"))})}else{that.$selectableUl.on("click",".ms-elem-selectable",function(){that.select($(this).data("ms-value"))});that.$selectionUl.on("click",".ms-elem-selection",function(){that.deselect($(this).data("ms-value"))})}that.$selectionUl.on("mouseenter",".ms-elem-selection",function(){$("li",that.$selectionUl).removeClass("ms-hover");$(this).addClass("ms-hover")}).on("mouseleave",function(){$("li",that.$selectionUl).removeClass("ms-hover")});that.$selectableUl.on("focusin",function(){$(this).addClass("ms-focus");that.$selectionUl.focusout()}).on("focusout",function(){$(this).removeClass("ms-focus");$("li",that.$container).removeClass("ms-hover")});that.$selectionUl.on("focusin",function(){$(this).addClass("ms-focus")}).on("focusout",function(){$(this).removeClass("ms-focus");$("li",that.$container).removeClass("ms-hover")});ms.on("focusin",function(){ms.focusout();that.$selectableUl.focusin()}).on("focusout",function(){that.$selectableUl.removeClass("ms-focus");that.$selectionUl.removeClass("ms-focus")});ms.onKeyDown=function(e,keyContainer){var ul=that.$container.find("."+keyContainer).find(".ms-list"),lis=ul.find("li:visible:not(.ms-optgroup-label, .ms-optgroup-container)"),lisNumber=lis.length,liFocused=ul.find("li.ms-hover"),liFocusedIndex=liFocused.length>0?lis.index(liFocused):-1,ulHeight=ul.innerHeight(),liHeight=lis.first().outerHeight(true),numberOfLisDisplayed=Math.floor(ulHeight/liHeight);if(e.keyCode==32){if(liFocused.length>0){var method=keyContainer=="ms-selectable"?"select":"deselect";if(keyContainer=="ms-selectable"){that.select(liFocused.data("ms-value"))}else{that.deselect(liFocused.data("ms-value"))}lis.removeClass("ms-hover");that.scrollTo=0;ul.scrollTop(that.scrollTo)}}else if(e.keyCode==40){if(lis.length>0){var nextLiIndex=liFocusedIndex+1,nextLi=lisNumber!=nextLiIndex?lis.eq(nextLiIndex):lis.first(),ulPosition=ul.position().top,nextLiPosition=nextLi.position().top;lis.removeClass("ms-hover");nextLi.addClass("ms-hover");if(lisNumber==nextLiIndex){that.scrollTo=0}else if(nextLiPosition>=ulPosition+numberOfLisDisplayed*liHeight){that.scrollTo+=liHeight}ul.scrollTop(that.scrollTo)}}else if(e.keyCode==38){if(lis.length>0){var prevLiIndex=Math.max(liFocusedIndex-1,-1),prevLi=lis.eq(prevLiIndex),ulPosition=ul.position().top,prevLiPosition=prevLi.position().top;lis.removeClass("ms-hover");prevLi.addClass("ms-hover");if(prevLiPosition<=ulPosition){that.scrollTo-=liHeight}else if(prevLiIndex<0){that.scrollTo=(lisNumber-numberOfLisDisplayed)*liHeight}ul.scrollTop(that.scrollTo)}}else if(e.keyCode==37||e.keyCode==39){if(that.$selectableUl.hasClass("ms-focus")){that.$selectableUl.focusout();that.$selectionUl.focusin()}else{that.$selectableUl.focusin();that.$selectionUl.focusout()}}};ms.on("keydown",function(e){if(ms.is(":focus")){var keyContainer=that.$selectableUl.hasClass("ms-focus")?"ms-selectable":"ms-selection";ms.onKeyDown(e,keyContainer)}})}var selectedValues=ms.find("option:selected").map(function(){return $(this).val()}).get();that.select(selectedValues,"init");if(typeof that.options.afterInit=="function"){that.options.afterInit.call(this,this.$container)}},refresh:function(){$("#ms-"+this.$element.attr("id")).remove();this.init(this.options)},select:function(value,method){if(typeof value=="string")value=[value];var that=this,ms=this.$element,msIds=$.map(value,function(val,index){return that.sanitize(val)}),selectables=this.$selectableUl.find("#"+msIds.join("-selectable, #")+"-selectable").filter(":not(."+that.options.disabledClass+")"),selections=this.$selectionUl.find("#"+msIds.join("-selection, #")+"-selection"),options=ms.find("option").filter(function(index){return $.inArray(this.value,value)>-1});if(selectables.length>0){selectables.addClass("ms-selected").hide();selections.addClass("ms-selected").show();options.prop("selected",true);var selectableOptgroups=that.$selectableUl.children(".ms-optgroup-container");if(selectableOptgroups.length>0){selectableOptgroups.each(function(){var selectablesLi=$(this).find(".ms-elem-selectable");if(selectablesLi.length==selectablesLi.filter(".ms-selected").length){$(this).find(".ms-optgroup-label").hide()}});var selectionOptgroups=that.$selectionUl.children(".ms-optgroup-container");selectionOptgroups.each(function(){var selectionsLi=$(this).find(".ms-elem-selection");if(selectionsLi.filter(".ms-selected").length>0){$(this).find(".ms-optgroup-label").show()}})}if(method!="init"){that.$selectionUl.focusout();that.$selectableUl.focusin();ms.trigger("change");if(typeof that.options.afterSelect=="function"){that.options.afterSelect.call(this,value)}}}},deselect:function(value){if(typeof value=="string")value=[value];var that=this,ms=this.$element,msIds=$.map(value,function(val,index){return that.sanitize(val)}),selectables=this.$selectableUl.find("#"+msIds.join("-selectable, #")+"-selectable"),selections=this.$selectionUl.find("#"+msIds.join("-selection, #")+"-selection").filter(".ms-selected"),options=ms.find("option").filter(function(index){return $.inArray(this.value,value)>-1});if(selections.length>0){selectables.removeClass("ms-selected").show();selections.removeClass("ms-selected").hide();options.prop("selected",false);var selectableOptgroups=that.$selectableUl.children(".ms-optgroup-container");if(selectableOptgroups.length>0){selectableOptgroups.each(function(){var selectablesLi=$(this).find(".ms-elem-selectable");if(selectablesLi.filter(":not(.ms-selected)").length>0){$(this).find(".ms-optgroup-label").show()}});var selectionOptgroups=that.$selectionUl.children(".ms-optgroup-container");selectionOptgroups.each(function(){var selectionsLi=$(this).find(".ms-elem-selection");if(selectionsLi.filter(".ms-selected").length==0){$(this).find(".ms-optgroup-label").hide()}})}this.$selectableUl.focusout();this.$selectionUl.focusin();ms.trigger("change");if(typeof that.options.afterDeselect=="function"){that.options.afterDeselect.call(this,value)}}},select_all:function(){var ms=this.$element,values=ms.val();ms.find("option").prop("selected",true);this.$selectableUl.find(".ms-elem-selectable").addClass("ms-selected").hide();this.$selectionUl.find(".ms-optgroup-label").show();this.$selectableUl.find(".ms-optgroup-label").hide();this.$selectionUl.find(".ms-elem-selection").addClass("ms-selected").show();this.$selectionUl.focusin();this.$selectableUl.focusout();ms.trigger("change");if(typeof this.options.afterSelect=="function"){var selectedValues=$.grep(ms.val(),function(item){return $.inArray(item,values)<0});this.options.afterSelect.call(this,selectedValues)}},deselect_all:function(){var ms=this.$element,values=ms.val();ms.find("option").prop("selected",false);this.$selectableUl.find(".ms-elem-selectable").removeClass("ms-selected").show();this.$selectionUl.find(".ms-optgroup-label").hide();this.$selectableUl.find(".ms-optgroup-label").show();this.$selectionUl.find(".ms-elem-selection").removeClass("ms-selected").hide();this.$selectableUl.focusin();this.$selectionUl.focusout();ms.trigger("change");if(typeof this.options.afterDeselect=="function"){this.options.afterDeselect.call(this,values)}},isDomNode:function(attr){return attr&&typeof attr==="object"&&typeof attr.nodeType==="number"&&typeof attr.nodeName==="string"},sanitize:function(value){return value.replace(/[^A-Za-z0-9]*/gi,"_")}};$.fn.multiSelect=function(){var option=arguments[0],args=arguments;return this.each(function(){var $this=$(this),data=$this.data("multiselect"),options=$.extend({},$.fn.multiSelect.defaults,$this.data(),typeof option=="object"&&option);if(!data)$this.data("multiselect",data=new MultiSelect(this,options));if(typeof option=="string"){data[option](args[1])}else{data.init()}})};$.fn.multiSelect.defaults={selectableOptgroup:false,disabledClass:"disabled",dblClick:false};$.fn.multiSelect.Constructor=MultiSelect}(window.jQuery); 12 | --------------------------------------------------------------------------------