1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * 
 26  * 
 27  * @private
 28  */
 29 XFormChoices = function(choiceObject, type, valueProperty, labelProperty) {
 30 	if (arguments.length == 0) return;
 31 	
 32 	if (choiceObject != null) this._choiceObject = choiceObject;
 33 	if (type != null) this._type = type;
 34 	if (valueProperty != null) this._valueProperty = valueProperty;
 35 	if (labelProperty != null) this._labelProperty = labelProperty;
 36 	
 37 	this._choiceChangeTime = new Date().getTime();
 38 	this._lastNormalizeTime = 0;
 39 	
 40 	if (this._type == XFormChoices.AUTO) this.autoDetermineType();
 41 	
 42 	this._eventMgr = new AjxEventMgr();
 43 }
 44 XFormChoices.prototype = new Object();
 45 XFormChoices.prototype.constructor = XFormChoices;
 46 
 47 
 48 //
 49 //	static methods
 50 //
 51 XFormChoices.normalizeChoices = function (choices, type, valueProperty, labelProperty) {
 52 	var values;
 53 	var labels;
 54     var visible; //indicate if the menu item choice is visible
 55     var totalInvisibleChoices = 0;
 56     
 57     switch (type) {
 58 		case XFormChoices.SIMPLE_LIST:
 59 			values = [].concat(choices)
 60 			labels = [].concat(choices)
 61 
 62 			break;
 63 
 64 
 65 		case XFormChoices.OBJECT_LIST:
 66 			values = []; labels = []; visible = [];
 67 			if (valueProperty == null) valueProperty = "value";
 68 			if (labelProperty == null) labelProperty = "label";
 69 
 70             var cnt = choices.length;
 71 			for (var i = 0; i < cnt; i++) {
 72 				if(choices[i]) {				
 73 					values.push(choices[i][valueProperty]);
 74 					labels.push(choices[i][labelProperty]);
 75                     if (choices[i]["visible"] == false) { //by default, the choice should be visible unless specified as false
 76                         visible.push(false) ;
 77                         totalInvisibleChoices ++ ;
 78                     }else{
 79                         visible.push(true) ;
 80                     }
 81                 }
 82 			}
 83 		
 84 			break;
 85 		case XFormChoices.OBJECT_REFERENCE_LIST:
 86 			values = []; labels = [];
 87 			if (labelProperty == null) labelProperty = "label";
 88 			var cnt = choices.length;
 89 			for (var i = 0; i < cnt; i++) {
 90 				if(choices[i]) {
 91 					values.push(choices[i]);
 92 					labels.push(choices[i][labelProperty]);
 93 				}
 94 			}		
 95 			break;	
 96 
 97 		case XFormChoices.HASH:
 98 			values = []; labels = [];
 99 			for (var prop in choices) {
100 				values.push(prop);
101 				labels.push(choices[prop]);
102 			}
103 		
104 			break;
105 	}
106 	return {values:values, labels:labels, visible:visible, totalInvisibleChoices: totalInvisibleChoices };
107 }
108 
109 
110 // constants
111 XFormChoices.AUTO = "auto";
112 XFormChoices.SIMPLE_LIST = "list";
113 XFormChoices.HASH = "hash";
114 XFormChoices.OBJECT_LIST = "object";
115 XFormChoices.OBJECT_REFERENCE_LIST = "object_reference_list";
116 
117 // type defaults
118 XFormChoices.prototype._type = XFormChoices.AUTO;
119 XFormChoices.prototype._valueProperty = "value";
120 XFormChoices.prototype._labelProperty = "label";
121 XFormChoices.prototype._visibleProperty = "visible" ;
122 
123 
124 XFormChoices.prototype.getChoiceObject = 
125 function () {
126 	return this._choiceObject;
127 }
128 
129 XFormChoices.prototype.autoDetermineType = function () {
130 	var type;
131 
132 	var choices = this._choiceObject;
133 	if (choices) {
134 		if (AjxUtil.isArray(choices)) {
135 			var firstChoice = choices[0];
136 			if (AjxUtil.isObject(firstChoice)) {
137 				type = XFormChoices.OBJECT_LIST;
138 			} else {
139 				type = XFormChoices.SIMPLE_LIST;
140 			}
141 		} else if (AjxUtil.isObject(choices)) {
142 			type = XFormChoices.HASH;
143 		}
144 	}
145 	
146 	if (type == null) type = XFormChoices.SIMPLE_LIST;
147 	this._type = type;
148 }
149 
150 XFormChoices.prototype.setChoices = function (choiceObject) {
151 	this._choiceObject = choiceObject;
152 }
153 
154 XFormChoices.prototype.getChoices = function () {
155 	// only normalize if dirty
156 	if (this._lastNormalizeTime == this._choiceChangeTime && this.$normalizedChoices) {
157 		return this.$normalizedChoices;
158 	}
159 	this._lastNormalizeTime = this._choiceChangeTime;
160 
161 	this.$normalizedChoices = XFormChoices.normalizeChoices(this._choiceObject, this._type, this._valueProperty, this._labelProperty);
162 	return this.$normalizedChoices;
163 }
164 
165 XFormChoices.prototype.getChoiceByValue = function(value) {
166 	switch (this._type) {
167 		case XFormChoices.SIMPLE_LIST: 
168 			return value;
169 			break;
170 		
171 		case XFormChoices.OBJECT_LIST: 
172 			var valueProperty = this._valueProperty || "value";
173 			for (var i = 0; i < this._choiceObject.length; i++) {
174 				if (this._choiceObject[i][valueProperty] == value) {
175 					return this._choiceObject[i];
176 				}
177 			}
178 			break;
179 		
180 		case XFormChoices.OBJECT_REFERENCE_LIST:
181 			for (var i = 0; i < this._choiceObject.length; i++) {
182 				if (this._choiceObject[i] == value) {
183 					return this._choiceObject[i];
184 				}
185 			}
186 			break;
187 		case XFormChoices.HASH: 
188 			return this._choiceObject[value];
189 		break;
190 	}
191 	return null;
192 }
193 
194 XFormChoices.prototype.dirtyChoices = function () {
195 	this._choiceChangeTime = new Date().getTime();
196 	this.notifyListeners(DwtEvent.XFORMS_CHOICES_CHANGED, {});
197 }
198 
199 
200 
201 
202 
203 
204 //
205 //	listening -- these are from DwtControl  -- make an installable interface?
206 //
207 XFormChoices.prototype.addListener = function(eventType, listener) {
208 	return this._eventMgr.addListener(eventType, listener); 	
209 }
210 
211 XFormChoices.prototype.notifyListeners = function(eventType, event) {
212 	return this._eventMgr.notifyListeners(eventType, event);
213 }
214 
215 XFormChoices.prototype.isListenerRegistered = function(eventType) {
216 	return this._eventMgr.isListenerRegistered(eventType);
217 }
218 
219 XFormChoices.prototype.removeListener =  function(eventType, listener) {
220 	return this._eventMgr.removeListener(eventType, listener);
221 }
222