/*
 * Copyright (c) 1997-2013, SalesLogix, NA., LLC. All rights reserved.
 */

/**
 * @class Mobile.SalesLogix.Views._MetricListMixin
 *
 * Mixin for adding KPI widgets to list views. 
 *
 * @since 3.0
 *
 * @requires Mobile.SalesLogix.Views.MetricWidget 
 *
 */
define('Mobile/SalesLogix/Views/_MetricListMixin', [
    'dojo/_base/declare',
    'dojo/_base/array',
    'dojo/_base/lang',
    'dojo/aspect',
    './MetricWidget'
], function(
    declare,
    array,
    lang,
    aspect,
    MetricWidget
) {
    return declare('Mobile.SalesLogix.Views._MetricListMixin', null, {
        // Metrics
        metricNode: null,
        metricWidgets: null,
        configurationView: 'metric_configure',
        entityName: '',

        metricWidgetsBuilt: false,

        postMixInProperties: function() {
            this.inherited(arguments);
            this.widgetTemplate =  new Simplate([
                '<div id="{%= $.id %}" title="{%= $.titleText %}" class="list {%= $.cls %}" {% if ($.resourceKind) { %}data-resource-kind="{%= $.resourceKind %}"{% } %}>',
                    '<div data-dojo-attach-point="searchNode"></div>',
                    '<div data-dojo-attach-point="metricNode" class="metric-list"></div>',
                    '<div class="overthrow scroller" data-dojo-attach-point="scrollerNode">',
                        '{%! $.emptySelectionTemplate %}',
                        '<ul class="list-content" data-dojo-attach-point="contentNode"></ul>',
                        '{%! $.moreTemplate %}',
                        '{%! $.listActionTemplate %}',
                    '</div>',
                '</div>'
            ]);
        },
        createMetricWidgetsLayout: function() {
            var filtered = [],
                metrics = [];

            metrics = App.getMetricsByResourceKind(this.resourceKind);

            if (metrics.length > 0) {
                filtered = array.filter(metrics, function(item) {
                    return item.enabled;
                });
            }

            return filtered;
        },
        postCreate: function() {
            this.inherited(arguments);
        },
        destroyWidgets: function() {
            array.forEach(this.metricWidgets, function(widget) {
                widget.destroy();
            }, this);

            this.metricWidgetsBuilt = false;
        },
        requestData: function() {
            this.inherited(arguments);
            this.rebuildWidgets();
        },
        clear: function() {
            this.inherited(arguments);
            this.destroyWidgets();
        },
        rebuildWidgets: function() {
            if (this.metricWidgetsBuilt) {
                return;
            }

            this.destroyWidgets();
            this.metricWidgets = [];

            if (this.options && this.options.simpleMode && (this.options.simpleMode === true)) {
                return;
            }

            var widgetOptions;
            // Create metrics widgets and place them in the metricNode
            widgetOptions = this.createMetricWidgetsLayout() || [];
            array.forEach(widgetOptions, function(options) {
                if (this._hasValidOptions(options)) {
                    options.returnToId = this.id;
                    options.parentResourceKind = this.resourceKind;
                    options.resourceKind = this.resourceKind;
                    options.currentSearchExpression = this.currentSearchExpression;
                    options.queryArgs._activeFilter = this._getCurrentQuery();

                    var widget = new MetricWidget(options);
                    widget.placeAt(this.metricNode, 'last');
                    widget.requestData();
                    this.metricWidgets.push(widget);
                }
            }, this);

            this.metricWidgetsBuilt = true;
        },
        _getCurrentQuery: function() {
            // Get the current query from the search box, and any context query located in options.where
            var query = this.query,
                where = this.options && this.options.where;
            return array.filter([query, where], function(item) {
                return !!item;
            }).join(' and ');
        },
        _hasValidOptions: function(options) {
            return options 
                && options.queryArgs 
                && options.queryArgs._filterName 
                && options.queryArgs._metricName;
        }
    });
});

@@ -1159,69 +1159,72 @@
 div 
-data-dojo-attach-point=%22metricNode%22 class=%22metric-list%22%3E%3C/div
+class=%22overthrow scroller%22 data-dojo-attach-point=%22scrollerNode%22
 %3E',%0A
@@ -1247,78 +1247,79 @@
     
+  
+  
 '%3Cdiv 
-class=%22overthrow scroller%22 data-dojo-attach-point=%22scrollerNode%22
+data-dojo-attach-point=%22metricNode%22 class=%22metric-list%22%3E%3C/div
 %3E',%0A

Move metrics node into the scroller.