EditSubjectView.js revision a500a8ecaba8363492aa1f4a62d5af939b2a91e6
208651a016b098f4fa1f6279559f104d70f1632dtakashi/**
556348850810d80a9e758b5a747982cfc6a3e731nd * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
938c687542a2f620cc7e781ddba4f3911dc1320dyoshiki *
1fc594205bd58583a2660536729eb684f5cb7932nd * Copyright (c) 2014 ForgeRock AS. All rights reserved.
938c687542a2f620cc7e781ddba4f3911dc1320dyoshiki *
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * The contents of this file are subject to the terms
031b91a62d25106ae69d4693475c79618dd5e884fielding * of the Common Development and Distribution License
031b91a62d25106ae69d4693475c79618dd5e884fielding * (the License). You may not use this file except in
031b91a62d25106ae69d4693475c79618dd5e884fielding * compliance with the License.
031b91a62d25106ae69d4693475c79618dd5e884fielding *
031b91a62d25106ae69d4693475c79618dd5e884fielding * You can obtain a copy of the License at
031b91a62d25106ae69d4693475c79618dd5e884fielding * http://forgerock.org/license/CDDLv1.0.html
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * See the License for the specific language governing
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * permission and limitations under the License.
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd *
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * When distributing Covered Code, include this CDDL
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * Header Notice in each file and include the License file
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * at http://forgerock.org/license/CDDLv1.0.html
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * If applicable, add the following below the CDDL Header,
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * with the fields enclosed by brackets [] replaced by
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * your own identifying information:
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * "Portions Copyrighted [year] [name of copyright owner]"
7db9f691a00ead175b03335457ca296a33ddf31bnd */
208651a016b098f4fa1f6279559f104d70f1632dtakashi
938c687542a2f620cc7e781ddba4f3911dc1320dyoshiki /**
938c687542a2f620cc7e781ddba4f3911dc1320dyoshiki * @author JKigwana
208651a016b098f4fa1f6279559f104d70f1632dtakashi */
208651a016b098f4fa1f6279559f104d70f1632dtakashi
938c687542a2f620cc7e781ddba4f3911dc1320dyoshiki/*global window, define, $, form2js, _, js2form, document, console */
208651a016b098f4fa1f6279559f104d70f1632dtakashi
938c687542a2f620cc7e781ddba4f3911dc1320dyoshikidefine("org/forgerock/openam/ui/policy/EditSubjectView", [
2b1ca4f3e09956e86d7f7c5b44f2c6f5351d54dbyoshiki "org/forgerock/commons/ui/common/main/AbstractView",
208651a016b098f4fa1f6279559f104d70f1632dtakashi "org/forgerock/commons/ui/common/util/UIUtils",
208651a016b098f4fa1f6279559f104d70f1632dtakashi "org/forgerock/commons/ui/common/main/EventManager",
208651a016b098f4fa1f6279559f104d70f1632dtakashi "org/forgerock/commons/ui/common/util/Constants",
2b1ca4f3e09956e86d7f7c5b44f2c6f5351d54dbyoshiki "org/forgerock/commons/ui/common/main/Configuration"
938c687542a2f620cc7e781ddba4f3911dc1320dyoshiki], function(AbstractView, uiUtils, eventManager, constants, conf) {
938c687542a2f620cc7e781ddba4f3911dc1320dyoshiki
var EditSubjectView = AbstractView.extend({
events: {
'change select#selection' : 'changeType',
'change .field-float-pattern input': 'changeInput'
},
data: {},
mode:'append',
render: function( schema, callback, element, itemID, itemData ) {
var self = this;
this.setElement(element);
this.data = $.extend(true, [], schema);
this.data.itemID = itemID;
this.$el.append(uiUtils.fillTemplateWithData("templates/policy/EditSubjectTemplate.html", this.data));
this.setElement('#subject_' + itemID );
this.delegateEvents();
if (itemData) {
// Temporay fix, the name attribute is being added by the server after the policy is created.
delete itemData.name;
this.$el.data('itemData',itemData);
this.$el.find('select#selection').val(itemData.type).trigger('change');
}
this.$el.find('select#selection').focus();
if (callback) {callback();}
},
createListItem: function(allSubjects, item){
item.focus(); // Required to trigger changeInput.
this.data.subjects = allSubjects;
var html = uiUtils.fillTemplateWithData("templates/policy/ListItem.html", {data:item.data().itemData});
item.find('.item-data').html(html);
this.setElement('#'+item.attr('id'));
this.delegateEvents();
},
changeInput: function(e) {
var label = $(e.currentTarget).prev('label').data().title;
this.$el.data().itemData[label] = e.currentTarget.value;
},
initSelectize: function() {
var self = this,
title = '';
this.$el.find('.selectize').each(function(){
$(this).selectize({
plugins: ['restore_on_backspace'],
delimiter: ',',
persist: false,
create: function(input) {
return {
value: input,
text: input
};
},
onChange: function(value){
title = this.$input.parent().find('label')[0].dataset.title;
if(title !== ''){
self.$el.data().itemData[title] = value;
}
}
});
});
},
changeType: function(e) {
e.stopPropagation();
var self = this,
itemData = {},
schema = {},
html = '',
selectize = false,
selectedType = e.target.value,
delay = self.$el.find('.field-float-pattern').length > 0 ? 500 : 0,
buildHTML = function(schemaProps) {
var count = 0,
returnVal = '<div class="no-float">';
_.map(schemaProps, function(value, key) {
if (value.type === 'string' ) {
returnVal += '\n'+ uiUtils.fillTemplateWithData("templates/policy/ConditionAttrString.html", {data:itemData[key], title:key, id:count});
} else if (value.type === 'array' ) {
returnVal += '\n'+ uiUtils.fillTemplateWithData("templates/policy/ConditionAttrArray.html", {data:itemData[key], title:key, id:count, tempData:self.weekdays});
} else {
console.error('Unexpected data type:',key,value);
}
count++;
});
returnVal += '</div>';
return returnVal;
};
schema = _.findWhere(this.data.subjects, {title: selectedType}) || {};
if (this.$el.data().itemData && this.$el.data().itemData.type === selectedType) {
itemData = this.$el.data().itemData;
} else {
itemData.type = schema.title;
_.map(schema.config.properties, function(value,key) {
switch (value.type) {
case 'string':
itemData[key] = '';
break;
case 'array':
itemData[key] = [];
break;
default:
console.error('Unexpected data type:',key,value);
break;
}
});
self.$el.data('itemData',itemData);
}
if (itemData) {
html = buildHTML(schema.config.properties);
this.$el.find('.no-float').fadeOut(500);
this.$el.find('.clear-left').fadeOut(500);
this.$el.find('.field-float-pattern input')
.addClass('placeholderText')
.prop('readonly', true)
.prev('label')
.removeClass('showLabel');
// setTimeout needed to delay transitions.
setTimeout( function() {
self.$el.find('.no-float').remove();
self.$el.find('.clear-left').remove();
self.$el.find('.field-float-select').after( html );
selectize = _.find(schema.config.properties, function(item){
return item.type === 'array';
});
if(selectize){
self.initSelectize();
}
setTimeout( function() {
self.$el.find('.field-float-pattern input')
.removeClass('placeholderText')
.prop('readonly', false)
.prev('label')
.addClass('showLabel');
self.delegateEvents();
}, 10);
}, delay);
}
}
});
return EditSubjectView;
});