<commit_before>﻿import common = require("ui/gestures/gestures-common");
import definition = require("ui/gestures");
import view = require("ui/core/view");

//var OWNER = "_owner";
//var CALLBACK = "_callback";
//var TYPE = "_type";
//var TARGET = "_target";

// merge the exports of the request file with the exports of this file
declare var exports;
require("utils/module-merge").merge(common, exports);

class UIGestureRecognizerImpl extends NSObject {
    static new(): UIGestureRecognizerImpl {
        return <UIGestureRecognizerImpl>super.new();
    }

    private _owner: GesturesObserver;
    private _type: any;
    private _callback: Function;

    public initWithOwnerTypeCallback(owner: GesturesObserver, type: any, callback?: Function): UIGestureRecognizerImpl {
        this._owner = owner;
        this._type = type;
        if (callback) {
            this._callback = callback;
        }

        return this;
    }

    public static ObjCExposedMethods = {
        "recognize": { returns: interop.types.void, params: [UIGestureRecognizer] }
    };

    public recognize(recognizer: UIGestureRecognizer): void {
        var callback = this._callback ? this._callback : this._owner._callback;
        var type = this._type;
        var target = this._owner._target;

        var args = {
            type: type,
            view: target,
            ios: recognizer,
            android: undefined
        };

        if (callback) {
            callback(args);
        }
    }
}

export class GesturesObserver implements definition.GesturesObserver {
    public _callback: (args: definition.GestureEventData) => void;
    public _target: view.View;
    private _recognizers: {};

    constructor(callback: (args: definition.GestureEventData) => void) {
        this._callback = callback;
        this._recognizers = {};
    }

    public observe(target: view.View, type: definition.GestureTypes) {
        this.disconnect();

        this._target = target;

        if (this._target && this._target.ios && this._target.ios.addGestureRecognizer) {
            var nativeView = <UIView>this._target.ios;

            if (type & definition.GestureTypes.tap) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.tap));
            }

            if (type & definition.GestureTypes.doubleTap) {
                var r = <UITapGestureRecognizer>this._createRecognizer(definition.GestureTypes.doubleTap);
                r.numberOfTapsRequired = 2;

                nativeView.addGestureRecognizer(r);
            }

            if (type & definition.GestureTypes.pinch) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.pinch, args => {
                    this._executeCallback(_getPinchData(args));
                }));
            }

            if (type & definition.GestureTypes.pan) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.pan, args => {
                    this._executeCallback(_getPanData(args, this._target.ios));
                }));
            }

            if (type & definition.GestureTypes.swipe) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.swipe, args => {
                    this._executeCallback(_getSwipeData(args));
                }));
            }

            if (type & definition.GestureTypes.rotation) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.rotation, args => {
                    this._executeCallback(_getRotationData(args));
                }));
            }

            if (type & definition.GestureTypes.longPress) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.longPress));
            }
        }
    }

    public disconnect() {
        if (this._target && this._target.ios) {

            for (var name in this._recognizers) {
                if (this._recognizers.hasOwnProperty(name)) {
                    var item = <RecognizerCache>this._recognizers[name];
                    this._target.ios.removeGestureRecognizer(item.recognizer);

                    item.recognizer = null;
                    item.target = null;
                }
            }

            this._recognizers = {};
        }

        this._target = null;
    }

    private _executeCallback(args: definition.GestureEventData) {
        if (this._callback) {
            this._callback(args);
        }
    }

    private _createRecognizer(type: definition.GestureTypes, callback?: (args: definition.GestureEventData) => void): UIGestureRecognizer {
        var recognizer: UIGestureRecognizer;
        var name = definition.toString(type);
        var target = _createUIGestureRecognizerTarget(this, type, callback);
        var recognizerType = _getUIGestureRecognizerType(type);

        if (recognizerType) {
            recognizer = recognizerType.alloc().initWithTargetAction(target, "recognize");
            if (recognizer) {
                this._recognizers[name] = <RecognizerCache>{ recognizer: recognizer, target: target };
            }
        }

        return recognizer;
    }
}

function _createUIGestureRecognizerTarget(owner: GesturesObserver, type: definition.GestureTypes, callback?: (args: definition.GestureEventData) => void): any {
    return UIGestureRecognizerImpl.new().initWithOwnerTypeCallback(owner, type, callback);
}

interface RecognizerCache {
    recognizer: UIGestureRecognizer;
    target: any;
}

function _getUIGestureRecognizerType(type: definition.GestureTypes): any {<commit_msg>Fixed:  Swipe gesture is not recognized by Page. #301<commit_after>﻿import common = require("ui/gestures/gestures-common");
import definition = require("ui/gestures");
import view = require("ui/core/view");

//var OWNER = "_owner";
//var CALLBACK = "_callback";
//var TYPE = "_type";
//var TARGET = "_target";

// merge the exports of the request file with the exports of this file
declare var exports;
require("utils/module-merge").merge(common, exports);

class UIGestureRecognizerImpl extends NSObject {
    static new(): UIGestureRecognizerImpl {
        return <UIGestureRecognizerImpl>super.new();
    }

    private _owner: GesturesObserver;
    private _type: any;
    private _callback: Function;

    public initWithOwnerTypeCallback(owner: GesturesObserver, type: any, callback?: Function): UIGestureRecognizerImpl {
        this._owner = owner;
        this._type = type;
        if (callback) {
            this._callback = callback;
        }

        return this;
    }

    public static ObjCExposedMethods = {
        "recognize": { returns: interop.types.void, params: [UIGestureRecognizer] }
    };

    public recognize(recognizer: UIGestureRecognizer): void {
        var callback = this._callback ? this._callback : this._owner._callback;
        var type = this._type;
        var target = this._owner._target;

        var args = {
            type: type,
            view: target,
            ios: recognizer,
            android: undefined
        };

        if (callback) {
            callback(args);
        }
    }
}

export class GesturesObserver implements definition.GesturesObserver {
    public _callback: (args: definition.GestureEventData) => void;
    public _target: view.View;
    private _recognizers: {};

    constructor(callback: (args: definition.GestureEventData) => void) {
        this._callback = callback;
        this._recognizers = {};
    }

    public observe(target: view.View, type: definition.GestureTypes) {
        this.disconnect();

        this._target = target;
        if (this._target && this._target._nativeView && this._target._nativeView.addGestureRecognizer) {
            var nativeView = <UIView>this._target._nativeView;

            if (type & definition.GestureTypes.tap) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.tap));
            }

            if (type & definition.GestureTypes.doubleTap) {
                var r = <UITapGestureRecognizer>this._createRecognizer(definition.GestureTypes.doubleTap);
                r.numberOfTapsRequired = 2;

                nativeView.addGestureRecognizer(r);
            }

            if (type & definition.GestureTypes.pinch) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.pinch, args => {
                    this._executeCallback(_getPinchData(args));
                }));
            }

            if (type & definition.GestureTypes.pan) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.pan, args => {
                    this._executeCallback(_getPanData(args, this._target._nativeView));
                }));
            }

            if (type & definition.GestureTypes.swipe) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.swipe, args => {
                    this._executeCallback(_getSwipeData(args));
                }, UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionDown));

                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.swipe, args => {
                    this._executeCallback(_getSwipeData(args));
                }, UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionLeft));

                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.swipe, args => {
                    this._executeCallback(_getSwipeData(args));
                }, UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionRight));

                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.swipe, args => {
                    this._executeCallback(_getSwipeData(args));
                }, UISwipeGestureRecognizerDirection.UISwipeGestureRecognizerDirectionUp));
            }

            if (type & definition.GestureTypes.rotation) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.rotation, args => {
                    this._executeCallback(_getRotationData(args));
                }));
            }

            if (type & definition.GestureTypes.longPress) {
                nativeView.addGestureRecognizer(this._createRecognizer(definition.GestureTypes.longPress));
            }
        }
    }

    public disconnect() {
        if (this._target && this._target._nativeView) {

            for (var name in this._recognizers) {
                if (this._recognizers.hasOwnProperty(name)) {
                    var item = <RecognizerCache>this._recognizers[name];
                    this._target._nativeView.removeGestureRecognizer(item.recognizer);

                    item.recognizer = null;
                    item.target = null;
                }
            }

            this._recognizers = {};
        }

        this._target = null;
    }

    private _executeCallback(args: definition.GestureEventData) {
        if (this._callback) {
            this._callback(args);
        }
    }

    private _createRecognizer(type: definition.GestureTypes, callback?: (args: definition.GestureEventData) => void, swipeDirection?: UISwipeGestureRecognizerDirection): UIGestureRecognizer {
        var recognizer: UIGestureRecognizer;
        var name = definition.toString(type);
        var target = _createUIGestureRecognizerTarget(this, type, callback);
        var recognizerType = _getUIGestureRecognizerType(type);

        if (recognizerType) {
            if (type === definition.GestureTypes.swipe && swipeDirection) {
                name = name + swipeDirection.toString();
                recognizer = recognizerType.alloc().initWithTargetAction(target, "recognize");
                (<UISwipeGestureRecognizer>recognizer).direction = swipeDirection;
            }
            else {
                recognizer = recognizerType.alloc().initWithTargetAction(target, "recognize");
            }
            
            if (recognizer) {
                this._recognizers[name] = <RecognizerCache>{ recognizer: recognizer, target: target };
            }
        }

        return recognizer;
    }
}

function _createUIGestureRecognizerTarget(owner: GesturesObserver, type: definition.GestureTypes, callback?: (args: definition.GestureEventData) => void): any {
    return UIGestureRecognizerImpl.new().initWithOwnerTypeCallback(owner, type, callback);
}

interface RecognizerCache {
    recognizer: UIGestureRecognizer;
    target: any;
}

function _getUIGestureRecognizerType(type: definition.GestureTypes): any {
{'fraction_non_alphanumeric': 0.0994106090373281, 'fraction_numerical': 0.0003929273084479371, 'mean_word_length': 2.417293233082707, 'pattern_counts': {'":': 2, '<': 14, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5903385', 'n_tokens_mistral': 3626, 'n_tokens_neox': 3323, 'n_words': 596}