
/*

  SmartClient Ajax RIA system
  Version v9.0p_2021-05-01/LGPL Deployment (2021-05-01)

  Copyright 2000 and beyond Isomorphic Software, Inc. All rights reserved.
  "SmartClient" is a trademark of Isomorphic Software, Inc.

  LICENSE NOTICE
     INSTALLATION OR USE OF THIS SOFTWARE INDICATES YOUR ACCEPTANCE OF
     ISOMORPHIC SOFTWARE LICENSE TERMS. If you have received this file
     without an accompanying Isomorphic Software license file, please
     contact licensing@isomorphic.com for details. Unauthorized copying and
     use of this software is a violation of international copyright law.

  DEVELOPMENT ONLY - DO NOT DEPLOY
     This software is provided for evaluation, training, and development
     purposes only. It may include supplementary components that are not
     licensed for deployment. The separate DEPLOY package for this release
     contains SmartClient components that are licensed for deployment.

  PROPRIETARY & PROTECTED MATERIAL
     This software contains proprietary materials that are protected by
     contract and intellectual property law. You are expressly prohibited
     from attempting to reverse engineer this software or modify this
     software for human readability.

  CONTACT ISOMORPHIC
     For more information regarding license rights and restrictions, or to
     report possible license violations, please contact Isomorphic Software
     by email (licensing@isomorphic.com) or web (www.isomorphic.com).

*/
// Class will not work without the ListGrid
if (isc.ListGrid) {





//> @class PresetCriteriaItem
// A FormItem for use with the +link{FilterBuilder}, allows the user to pick from a set of 
// pre-configured search criteria such as specific ranges in numeric or date data, and provide 
// user friendly titles for such criteria, such as "within the next two weeks" or 
// "High (0.75 - 0.99)".
// 
// @visibility external
//<
isc.defineClass("PresetCriteriaItem", "SelectItem");

isc.PresetCriteriaItem.addClassMethods({

});


isc.PresetCriteriaItem.addProperties({
    height: 20,
    cellHeight: 20,
    canFocus: true,

    //> @attr presetCriteriaItem.valueMap (Object : null : IR)
    // This attribute is not applicable to the PresetCriteriaItem.  See 
    // +link{presetCriteriaItem.options} instead.
    // 
    // @visibility external
    //<

    //> @attr presetCriteriaItem.options (Object : null : IR)
    // An object whose properties are user-visible titles for the preset ranges, and whose 
    // values are Criteria / AdvancedCriteria objects representing the criteria to be used if 
    // the user selects that choice.
    // 
    // @visibility external
    //<

    // i18n attributes

    //> @attr presetCriteriaItem.showCustomOption (boolean : false : IR)
    // If set, an additional option will be shown with the title +link{customOptionTitle}, 
    // which will cause +link{getCustomCriteria()} to be called.
    // 
    // @visibility external
    //<

    //> @attr presetCriteriaItem.customOptionTitle (String : "Custom..." : IR)
    // The title to show for the +link{presetCriteriaItem.showCustomOption, custom option}.
    //
    // @visibility external
    // @group i18nMessages
    //<
    customOptionTitle: "Custom...",

    shouldSaveValue: true

});

isc.PresetCriteriaItem.addMethods({
    
    init : function () {
        if (!this.options) this.options = {};

        this.valueMap = {};
        for (var key in this.options) {
            this.valueMap[key] = key;
        }

        if (this.showCustomOption) {
            this.options["customOption"] = null;
            this.valueMap["customOption"] = this.customOptionTitle; 
        }

        this.Super("init", arguments);
    },

    changed : function (form, item, value) {
        //this.Super("changed", arguments);
        if (value == "customOption") {
            if (this.getCustomCriteria && isc.isA.Function(this.getCustomCriteria))
                this.getCustomCriteria(this.getID()+".getCustomCriteriaReply(criteria,title)");
        }
    }
    
    ,

    //> @method presetCriteriaItem.getCustomCriteria() [A]
    // This method is called when +link{showCustomOption} is true and the user selects the 
    // custom option.  Implement this method by allowing the user to enter custom criteria, for
    // example, by opening a modal dialog.  Once the user has input customer criteria, fire the
    // callback method with the resulting criteria.
    // 
    // @param callback (Callback) callback to fire when custom criteria has been gathered.
    //              Expects parameters "criteria,title".  The "title" will be displayed as the 
    //              currently selected value when custom criteria have been chosen.
    // 
    // @visibility external
    //<
    getCustomCriteria : function (callback) {
    },

    getCustomCriteriaReply : function (criteria, title) {
        this.valueMap["customOption"] = title;
        this.options["customOption"] = criteria;
    },

    getCriteriaValue : function () {
        return this.getCriterion();
    },

    hasAdvancedCriteria : function () {
        return true;
    }

    ,

    //> @method presetCriteriaItem.getCriterion()
    // Get the criterion based on the value selected by the user.
    //
    // @return (Criterion or AdvancedCriteria) the criteria for the selected option
    // 
    // @visibility external
    //<
    getCriterion : function () {
        var key = this.getValue(),
            crit = this.options[key]
        ;

        //var result = this.updateCriteriaFieldNames(crit);
        var result = crit;

        return result;
    },

    updateCriteriaFieldNames : function (crit) {
        for (var key in crit) {
            var subCrit = crit[key];

            if (isc.DataSource.isAdvancedCriteria(subCrit)) {
                subCrit = this.updateCriteriaFieldNames(subCrit);
            } else {
                if (!subCrit.fieldName) {
                    subCrit.fieldName = this.getCriteriaFieldName();
                }
            }
        }

        return crit;
    },
    
    setValue : function (value) {
        value = this.matchCriteria(value);
        this.Super("setValue", arguments);
    },
    
    setValueMap : function (valueMap) {
        // valueMap is not supported for this FormItem type - we use this.options instead - 
        // ignore this call
        return null;
    },
    
    canEditCriterion : function (criterion) {
        var fieldNames = isc.DS.isAdvancedCriteria(criterion) ?
                isc.DS.getCriteriaFields(criterion).getUniqueItems() :
                criterion.criteria ? criterion.criteria.getProperty("fieldName").getUniqueItems() :
                [criterion.fieldName]
        ;
        
        var fieldName = this.getCriteriaFieldName();
        return fieldNames.contains(fieldName);
    },

    setCriterion : function (criterion) {
        this.setValue(isc.shallowClone(criterion));
    },

    getCriterion : function () {
        var value = this.getValue(),
            crit = this.options[value]
        ;
        return crit;
    },

    // find the entry in this.options that maps to the criteria object passed in
    matchCriteria : function (criteria, passedOptions) {
        var options = passedOptions || this.options,
            keys = isc.getKeys(options)
        ;

        for (var i=0; i<keys.length; i++) {
            var option = options[keys[i]];

            if (this.objectsAreEqual(option, criteria)) {
                return keys[i];
            }
        }

        return criteria;
    },

    // helper method to compare the properties on two objects, not dissimilar to 
    // DS.compareCriteria
    objectsAreEqual : function (object1, object2) {
        if (!object1 && !object2) return true;
        if (!object1 || !object2) return false;

        var keys1 = isc.getKeys(object1);

        for (var i=0; i<keys1.length; i++) {
            var key = keys1[i];

            if (key == "_constructor") continue;

            var prop1 = object1[key],
                prop2 = object2[key]
            ;

            if (isc.isAn.Array(prop1)) {
                for (var j=0; j<prop1.length; j++) {
                    if (isc.isAn.Object(prop1[j])) {
                        if (!this.objectsAreEqual(prop1[j], prop2[j])) return false;
                    } else {
                        if (prop1[j] != prop2[j]) return false;
                    }
                }
            } else if (isc.isA.Date(prop1)) {
                if (Date.compareDates(prop1, prop2) != 0) return false;
            } else if (isc.isAn.Object(prop1)) {
                if (!this.objectsAreEqual(prop1, prop2)) return false;
            } else {
                if (object1[key] != object2[key]) return false;
            }
        }
        return true;
    }

});

//> @class PresetDateRangeItem
// Allows the user to pick from pre-set date ranges or choose a custom date range via a
// +link{DateRangeDialog}.
// <P>
// To use this item in the +link{listGrid.showFilterEditor,FilterEditor} or 
// +link{FilterBuilder}, create a trivial +link{ClassFactory.defineClass,subclass} which 
// defines +link{PresetDateRangeItem.options,preset options}, then set
// +link{listGridField.filterEditorType} to use this class with the FilterEditor, or define a
// custom operator and set +link{operator.editorType} to use it with the FilterBuilder.
// <P>
// See the +explorerExample{dateRangeFilterPresets,Date Range (Presets)} example for sample code.
// @visibility external
//<
isc.defineClass("PresetDateRangeItem", "PresetCriteriaItem");

isc.PresetDateRangeItem.addProperties({

    customOptionTitle: "Custom Date Range",

    getCustomCriteria : function (callback) {
        this._callback = callback;
        isc.DateRangeDialog.askForRange(
            true, { returnCriterion: true }, null, this.getID()+".showDateRangeDialogReply(value)"
        );
    },

    showDateRangeDialogReply : function (value) {
        var callback = this._callback;
        delete this._callback;
        this.fireCallback(callback, "criteria,title", [value,this.customOptionTitle]);
    }

});

}
