permissionList.js revision c5011f808e012f047208686132f02cd8bb1024ab
1N/A$.widget("ui.permissionList", {
1N/A
1N/A _create : function() {
1N/A var self = this, element = this.element;
1N/A
1N/A this.association = element.data('association')
1N/A this.scope = element.data('scope').split(",");
1N/A this.polymorphic = element.data('polymorphic')
1N/A this.model = this.toUnderscore(element.data('model'));
1N/A
1N/A // Tipsy tooltip
1N/A element.find("button.help").tipsy({
1N/A gravity : 'w'
1N/A }).click(function() {
1N/A element.find("input.autocomplete").focus();
1N/A });
1N/A
1N/A // Attach Autocomplete to inputs
1N/A this.autocomplete = element.find("input.autocomplete")
1N/A this.autocomplete.autocomplete({
1N/A minLength : 3,
1N/A source : function(request, response) {
1N/A return self.autocompleteSource(request, response);
1N/A },
1N/A select : function(event, ui) {
1N/A return self.autocompleteSelect(event, ui);
1N/A },
1N/A }).data( "autocomplete")._renderItem = this.autocompleteIcon;
1N/A
1N/A // Never submit the autocompletion form
1N/A element.on("submit", "form.add", function(event) {
1N/A event.preventDefault();
1N/A });
1N/A
1N/A // Removal of permissions
1N/A element.on("click", "a[rel=delete]", function() {
1N/A var li = $(this).closest("li");
1N/A
1N/A if(!confirm("really delete?"))
1N/A return;
1N/A
1N/A $.ajax({
1N/A type: 'POST',
1N/A url: li.data('uri'),
1N/A data: {_method: 'delete'},
1N/A success: function(){
1N/A li.fadeOut(function() {
1N/A li.remove();
1N/A })
1N/A },
1N/A error: function(xhr, status, error){
1N/A li.trigger('ajax:error', [xhr, status, error]);
1N/A }
1N/A });
1N/A });
1N/A
1N/A // Show / Hide edit form
1N/A element.on("click", "a[rel=edit]", function(event) {
1N/A event.preventDefault();
1N/A this.blur();
1N/A
1N/A var li = $(this).closest("li");
1N/A var form = li.find("form");
1N/A if(form.size() == 0) {
1N/A // show form
1N/A var random = Math.random().toString().split(".")[1];
1N/A
1N/A // clone template form
1N/A var form = element.find("form.editTemplate").clone().wrap("<div></div>").parent().html();
1N/A form = $(form.replace(/%RANDOM%/g, random)).removeClass("editTemplate").addClass("edit").attr("action", li.data('uri')).appendTo(li)
1N/A
1N/A // extend selector for more supported elements
1N/A form.find("input[type=checkbox], select").each(function() {
1N/A var name = $(this).attr("name")
1N/A if(!name)
1N/A return;
1N/A
1N/A // change 'model[name]' into 'name'
1N/A var match = name.match(/\w+\[(\w+)\]/)
1N/A if(match)
1N/A name = match[1]
1N/A var value = li.data(name);
1N/A var type = this.tagName.toLowerCase();
1N/A if(type=='input')
1N/A var type = this.type.toLowerCase();
1N/A
1N/A switch(type){
1N/A case 'checkbox':
1N/A this.checked = value == '1'
1N/A break;
1N/A case 'select':
1N/A $(this).val(value);
1N/A break;
1N/A // add here support for other input types, when needed
1N/A }
1N/A });
1N/A } else {
1N/A // hide form
1N/A form.remove();
1N/A }
1N/A });
1N/A
1N/A // Permission removal succeeded
1N/A element.on("ajax:success", "ul", function(event, data) {
1N/A var target = $(event.target)
1N/A var li = target.closest("li");
1N/A li.replaceWith(data);
1N/A });
1N/A
1N/A // AJAX Actions failed
1N/A element.on("ajax:error", function(xhr, status, error) {
1N/A alert(status.responseText);
1N/A });
1N/A },
1N/A // collects elements from list for exclusion
1N/A excludeMap : function() {
1N/A var self = this;
1N/A var map = {}
1N/A
1N/A this.element.find("ul").children().each(function() {
1N/A var $this = $(this);
1N/A var type = $this.data('type') || self.scope[0];
1N/A var key = "exclude[" + type + "]"
1N/A var id = $this.data('id');
1N/A if(map[key])
1N/A map[key] += "," + id;
1N/A else
1N/A map[key] = id;
1N/A });
1N/A return map;
1N/A },
1N/A // source for autcomplete
1N/A autocompleteSource : function(request, response) {
1N/A
1N/A var params = $.extend(this.excludeMap(), {
1N/A term : request.term,
1N/A scope : this.scope.join(",")
1N/A });
1N/A
1N/A $.ajax({
1N/A url : '/autocomplete',
1N/A data : params,
1N/A success : function(data) {
1N/A response(data)
1N/A }
1N/A })
1N/A },
1N/A
1N/A // autocomplete select-handler
1N/A autocompleteSelect : function(event, ui) {
1N/A var input = $(this);
1N/A var list = this.element.find("ul");
1N/A var params = {}
1N/A
1N/A params[this.model + '[' + this.association + '_id]'] = ui.item.id;
1N/A if(this.polymorphic){
1N/A params[this.model + '[' + this.association + '_type]'] = ui.item.type;
1N/A }
1N/A
1N/A // Create the permission
1N/A $.post(this.element.data('uri'), params, function(data) {
1N/A data = $(data).hide();
1N/A list.append(data);
1N/A data.fadeIn('slow');
1N/A });
1N/A
1N/A this.autocomplete.val('');
1N/A return false;
1N/A },
1N/A
1N/A autocompleteIcon: function( ul, item ) {
1N/A return $( "<li></li>" )
1N/A .data( "item.autocomplete", item )
1N/A .append( "<a data-type='"+item.type+"'>" + item.label + "</a>" )
1N/A .appendTo( ul );
1N/A },
1N/A
1N/A // camelCase to under_score
1N/A toUnderscore: function(value){
1N/A value = value.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
1N/A if(value.indexOf('_')===0)
1N/A value = value.substr(1)
1N/A return value;
1N/A }
1N/A
1N/A});
1N/A