1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2010, 2011, 2012, 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) 2010, 2011, 2012, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates a new, empty filter rules controller.
 26  * @class
 27  * This class represents the filter rules controller. This controller manages
 28  * the filter rules page, which has a button toolbar and a list view of the rules.
 29  *
 30  * @author Conrad Damon
 31  *
 32  * @param {DwtShell}		container		the shell
 33  * @param {ZmPreferencesApp}	prefsApp		the preferences application
 34  * 
 35  * @extends		ZmController
 36  */
 37 ZmFilterController = function(container, prefsApp, prefsView, section, parent) {
 38 
 39 	ZmPrefController.call(this, container, prefsApp);
 40 
 41 	this._prefsView = prefsView;
 42 
 43 	this._parent = parent;
 44 	this._toolbar = parent._toolbar;
 45 
 46 	this._filterView = new ZmFilterPage(prefsView, section, this);
 47 	this._incomingFilterRulesController = new ZmFilterRulesController(container, prefsApp, this._filterView, this, false);
 48 	this._outgoingFilterRulesController = new ZmFilterRulesController(container, prefsApp, this._filterView, this, true);
 49 };
 50 
 51 ZmFilterController.prototype = new ZmPrefController;
 52 ZmFilterController.prototype.constructor = ZmFilterController;
 53 
 54 ZmFilterController.prototype.toString =
 55 function() {
 56 	return "ZmFilterController";
 57 };
 58 
 59 /**
 60  * Gets the filter rules view, which is comprised of a toolbar and a list view.
 61  * 
 62  * @return	{ZmFilterRulesView}		the filter rules view
 63  */
 64 ZmFilterController.prototype.getFilterView =
 65 function() {
 66 	return this._filterView;
 67 };
 68 
 69 ZmFilterController.prototype.getIncomingFilterRulesController =
 70 function() {
 71 	return this._incomingFilterRulesController;
 72 };
 73 
 74 ZmFilterController.prototype.getOutgoingFilterRulesController =
 75 function() {
 76 	return this._outgoingFilterRulesController;
 77 };
 78 /**
 79  * Initializes the controller.
 80  * 
 81  * @param	{ZmToolBar}	toolbar		the toolbar
 82  * @param	{ZmListView}	listView		the list view
 83  */
 84 ZmFilterController.prototype.initialize =
 85 function() {
 86 
 87 };
 88 
 89 ZmFilterController.prototype._getToolBarOps =
 90 function () {
 91 	return [];
 92 };
 93 
 94 ZmFilterController.prototype.hasOutgoingFiltersActive =
 95 function(callback) {
 96 	var rules = this._outgoingFilterRulesController.getRules();
 97 	if (!rules._initialized) {
 98 		rules.loadRules(false, new AjxCallback(this, this._handleLoadFilters, [callback]));
 99 	} else {
100 		var outgoingActive = rules.getActiveRules().size() > 0;
101 		if (callback)
102 			callback.run(outgoingActive);
103 		return outgoingActive;
104 	}
105 };
106 
107 ZmFilterController.prototype._handleLoadFilters =
108 function(callback) {
109 	var outgoingActive = this._outgoingFilterRulesController.getRules().getActiveRules().size() > 0;
110 	if (callback)
111 		callback.run(outgoingActive);
112 	return outgoingActive;
113 };
114 
115 ZmFilterController.prototype._stateChangeListener =
116 function (ev) {
117    var index, sel, rules = null;
118 
119    var listView = this._incomingFilterRulesController.getListView();
120    if (listView) {
121        sel = listView.getSelection()[0];
122        rules = this._incomingFilterRulesController.getRules();
123        index = sel ? rules.getIndexOfRule(sel) : null;
124        this._incomingFilterRulesController.resetListView(index);
125    }
126 
127    var outListView = this._outgoingFilterRulesController.getListView();
128    if (outListView) {
129        sel = outListView.getSelection()[0];
130        rules = this._outgoingFilterRulesController.getRules();
131        index = sel ? rules.getIndexOfRule(sel) : null;
132        this._outgoingFilterRulesController.resetListView(index);
133    }
134 
135 	ZmPrefController.prototype._stateChangeListener.apply(this, arguments);
136 };
137