<commit_before>function GalleryViewAssistant() {}

GalleryViewAssistant.prototype.setup = function () {
    var attributes = {
        noExtractFS: true //optional, turn off using extractfs to speed up renders.
    };
    this.image_model = {
        onLeftFunction : this.wentLeft.bind(this),
        onRightFunction : this.wentRight.bind(this),
        images: [],
        current_index: 0
    }
    this.getImageUrls();
    this.controller.setupWidget('imgurView', attributes, this.image_model);
    this.imgurViewElement = $('imgurView');

    // TODO reset width/height of image view on orientation change
    // see https://github.com/cw/webos-imgur-viewer/issues#issue/1
    this.controller.stageController.setWindowOrientation('free');

    // TODO implement show spinner while retrieving image JSON
    this.controller.setupWidget("spinnerId",
        this.attributes = { spinnerSize: "large" },
        this.model = { spinning: false }
    );

    this.categoryMenuModel = { label: $L('Category'), items: [{label: $L('All'), command:'cat-all', shortcut:'l'},
        {label: $L('Business'), command:'cat-business' },
        {label: $L('Personal'), command:'cat-personal', shortcut:'p'},
        {label: $L('Unfiled'), command:'cat-unfiled', shortcut:'u'}
    ]};
    this.controller.setupWidget('category-menu', undefined, this.categoryMenuModel);

    this.imageViewChangedHandler = this.imageViewChanged.bindAsEventListener(this);
    this.orientationChangeHandler = this.orientationChanged.bindAsEventListener(this);
    Mojo.Event.listen(this.controller.get('imgurView'), Mojo.Event.imageViewChanged, this.imageViewChangedHandler);
}

GalleryViewAssistant.prototype.getImageUrls = function () {
    // TODO allow various sorting/filtering options, cache results
    var sort = "latest", // latest, popular
        view = "all", // week, month, all
        count = 50, // integer between 0 and 50
        page = 1, // integer above 0
        url = "http://api.imgur.com/2/gallery.json?sort=" + sort + 
              "&view=" + view + "&page=" + page + "&count=" + count,
        // TODO implement OAuth
        request = new Ajax.Request(url, {
            method: 'get',
            asynchronous: true,
            evalJSON: "false",
            onSuccess: this.parseResult.bind(this),
            on0: function (ajaxResponse) { Mojo.Log.error("Connection failed"); },
            onFailure: function (response) { Mojo.Log.error("Request failed"); },
            onException: function (request, ex) { Mojo.Log.error("Exception"); },
        });
}

GalleryViewAssistant.prototype.parseResult = function (transport) {
    var image_list = [],
        json,
        data = transport.responseText;
    try {
        json = data.evalJSON();
    } catch (e) {
        Mojo.Log.error(e);
    }
    if (json.images) {
        for (var img in json.images) { image_list.push(json.images[img]); }
        this.image_model.images = image_list;
        this.controller.modelChanged(this.image_model, this);
        this.imgurViewElement.mojo.centerUrlProvided(this.image_model.images[0].large_thumbnail);
    } else {
        Mojo.Log.info("json object has no images");
    }
}

// Do something when the image view changes
GalleryViewAssistant.prototype.imageViewChanged = function (event) {
    Mojo.Log.info("Current image index: " + this.image_model.current_index);
    var idx = this.image_model.current_index;
    if (idx === 0) {
        this.imgurViewElement.mojo.leftUrlProvided("");
        this.imgurViewElement.mojo.centerUrlProvided(this.image_model.images[idx].large_thumbnail);
        this.imgurViewElement.mojo.rightUrlProvided(this.image_model.images[idx + 1].large_thumbnail);
    } else if (idx > 0 && idx < this.image_model.images.length) {
        this.imgurViewElement.mojo.leftUrlProvided(this.image_model.images[idx - 1].large_thumbnail);
        this.imgurViewElement.mojo.centerUrlProvided(this.image_model.images[idx].large_thumbnail);
        this.imgurViewElement.mojo.rightUrlProvided(this.image_model.images[idx + 1].large_thumbnail);
    } else if (idx === this.image_model.images.length - 1) {
        this.imgurViewElement.mojo.leftUrlProvided(this.image_model.images[idx - 1].large_thumbnail);
        this.imgurViewElement.mojo.centerUrlProvided(this.image_model.images[idx].large_thumbnail);
        this.imgurViewElement.mojo.rightUrlProvided("");
    }
    $("main-hdr").innerHTML = this.image_model.images[idx].message;
}

// Handle orientation changes
GalleryViewAssistant.prototype.orientationChanged = function (event) {
    //Mojo.Log.info("Resize imgurView width/height to match screen dimensions");
    var width = Mojo.Environment.DeviceInfo.screenWidth,
        height = Mojo.Environment.DeviceInfo.screenHeight,
        orientation = this.controller.stageController.getWindowOrientation();
    Mojo.Log.info("screen dims: " + width + ", " + height);
    Mojo.Log.info("orientation: " + orientation);
    Mojo.Log.info("style width before: " + $("imgurView").style.width);
    switch (orientation) {
        case "left": case "right":
            $("imgurView").setStyle("width: 480px; height: 320px;"); break;
        case "up": case "down":
            $("imgurView").setStyle("width: 320px; height: 480px;"); break;
    }
    Mojo.Log.info("style width after: " + $("imgurView").style.width);
    //$("#imgurView").
}

// A flick to the right triggers a scroll to the left
GalleryViewAssistant.prototype.wentLeft = function (event) {
    if (this.image_model.current_index >= 1) {
        this.image_model.current_index -= 1;
        this.controller.modelChanged(this.image_model, this);
    }
}

// A flick to the left triggers a scroll to the right
GalleryViewAssistant.prototype.wentRight = function (event) {
    if (this.image_model.current_index < this.image_model.images.length - 1) {
        this.image_model.current_index += 1;
        this.controller.modelChanged(this.image_model, this);
    }
}

// Start listening to orientation changes
GalleryViewAssistant.prototype.activate = function () {
    this.controller.listen(this.controller.stageController.document, Mojo.Event.orientationChange, this.orientationChangeHandler);
}

// Stop listening to orientation changes
GalleryViewAssistant.prototype.deactivate = function () {
    this.controller.stopListening(this.controller.stageController.document, Mojo.Event.orientationChange, this.orientationChangeHandler);
}

// Cleanup anything we did in setup function
GalleryViewAssistant.prototype.cleanup = function () {
    Mojo.Event.stopListening(this.controller.get('imgurView'), Mojo.Event.imageViewChanged, this.imageViewChangedHandler);
}

// This function will popup a dialog, displaying the message passed in.
GalleryViewAssistant.prototype.showDialogBox = function (title, message) {
    this.controller.showAlertDialog({
        onChoose: function (value) {},
        title: title,
        message: message,
        choices: [ {label:'OK', value:'OK', type:'color'} ]
    });
}
<commit_msg>implemented basic ImageView tap event handling<commit_after>function GalleryViewAssistant() {}

GalleryViewAssistant.prototype.setup = function () {
    var attributes = {
        noExtractFS: true //optional, turn off using extractfs to speed up renders.
    };
    this.image_model = {
        onLeftFunction : this.wentLeft.bind(this),
        onRightFunction : this.wentRight.bind(this),
        images: [],
        current_index: 0
    }
    this.getImageUrls();
    this.controller.setupWidget('imgurView', attributes, this.image_model);
    this.imgurViewElement = $('imgurView');

    // TODO reset width/height of image view on orientation change
    // see https://github.com/cw/webos-imgur-viewer/issues#issue/1
    this.controller.stageController.setWindowOrientation('free');

    // TODO implement show spinner while retrieving image JSON
    this.controller.setupWidget("spinnerId",
        this.attributes = { spinnerSize: "large" },
        this.model = { spinning: false }
    );

    this.categoryMenuModel = { label: $L('Category'), items: [{label: $L('All'), command:'cat-all', shortcut:'l'},
        {label: $L('Business'), command:'cat-business' },
        {label: $L('Personal'), command:'cat-personal', shortcut:'p'},
        {label: $L('Unfiled'), command:'cat-unfiled', shortcut:'u'}
    ]};
    this.controller.setupWidget('category-menu', undefined, this.categoryMenuModel);

    this.imageViewChangedHandler = this.imageViewChanged.bindAsEventListener(this);
    this.orientationChangeHandler = this.orientationChanged.bindAsEventListener(this);
    Mojo.Event.listen(this.controller.get('imgurView'), Mojo.Event.imageViewChanged, this.imageViewChangedHandler);
    this.showImageMenuHandler = this.showImageMenu.bindAsEventListener(this)
    Mojo.Event.listen(this.controller.get('imgurView'), Mojo.Event.tap, this.showImageMenuHandler);
}

GalleryViewAssistant.prototype.getImageUrls = function () {
    // TODO allow various sorting/filtering options, cache results
    var sort = "latest", // latest, popular
        view = "all", // week, month, all
        count = 50, // integer between 0 and 50
        page = 1, // integer above 0
        url = "http://api.imgur.com/2/gallery.json?sort=" + sort + 
              "&view=" + view + "&page=" + page + "&count=" + count,
        // TODO implement OAuth
        request = new Ajax.Request(url, {
            method: 'get',
            asynchronous: true,
            evalJSON: "false",
            onSuccess: this.parseResult.bind(this),
            on0: function (ajaxResponse) { Mojo.Log.error("Connection failed"); },
            onFailure: function (response) { Mojo.Log.error("Request failed"); },
            onException: function (request, ex) { Mojo.Log.error("Exception"); },
        });
}

GalleryViewAssistant.prototype.parseResult = function (transport) {
    var image_list = [],
        json,
        data = transport.responseText;
    try {
        json = data.evalJSON();
    } catch (e) {
        Mojo.Log.error(e);
    }
    if (json.images) {
        for (var img in json.images) { image_list.push(json.images[img]); }
        this.image_model.images = image_list;
        this.controller.modelChanged(this.image_model, this);
        this.imgurViewElement.mojo.centerUrlProvided(this.image_model.images[0].large_thumbnail);
    } else {
        Mojo.Log.info("json object has no images");
    }
}

// Do something when the image view changes
GalleryViewAssistant.prototype.imageViewChanged = function (event) {
    Mojo.Log.info("Current image index: " + this.image_model.current_index);
    var idx = this.image_model.current_index;
    if (idx === 0) {
        this.imgurViewElement.mojo.leftUrlProvided("");
        this.imgurViewElement.mojo.centerUrlProvided(this.image_model.images[idx].large_thumbnail);
        this.imgurViewElement.mojo.rightUrlProvided(this.image_model.images[idx + 1].large_thumbnail);
    } else if (idx > 0 && idx < this.image_model.images.length) {
        this.imgurViewElement.mojo.leftUrlProvided(this.image_model.images[idx - 1].large_thumbnail);
        this.imgurViewElement.mojo.centerUrlProvided(this.image_model.images[idx].large_thumbnail);
        this.imgurViewElement.mojo.rightUrlProvided(this.image_model.images[idx + 1].large_thumbnail);
    } else if (idx === this.image_model.images.length - 1) {
        this.imgurViewElement.mojo.leftUrlProvided(this.image_model.images[idx - 1].large_thumbnail);
        this.imgurViewElement.mojo.centerUrlProvided(this.image_model.images[idx].large_thumbnail);
        this.imgurViewElement.mojo.rightUrlProvided("");
    }
    $("main-hdr").innerHTML = this.image_model.images[idx].message;
}

// Handle orientation changes
GalleryViewAssistant.prototype.orientationChanged = function (event) {
    //Mojo.Log.info("Resize imgurView width/height to match screen dimensions");
    var width = Mojo.Environment.DeviceInfo.screenWidth,
        height = Mojo.Environment.DeviceInfo.screenHeight,
        orientation = this.controller.stageController.getWindowOrientation();
    Mojo.Log.info("screen dims: " + width + ", " + height);
    Mojo.Log.info("orientation: " + orientation);
    Mojo.Log.info("style width before: " + $("imgurView").style.width);
    switch (orientation) {
        case "left": case "right":
            $("imgurView").setStyle("width: 480px; height: 320px;"); break;
        case "up": case "down":
            $("imgurView").setStyle("width: 320px; height: 480px;"); break;
    }
    Mojo.Log.info("style width after: " + $("imgurView").style.width);
    //$("#imgurView").
}

// Handle image taps
GalleryViewAssistant.prototype.showImageMenu = function (event) {
    this.popupIndex = event.index;
    this.controller.popupSubmenu({
        onChoose: this.popupHandler,
        items: [
            {label: 'View on Imgur', command: 'view-site'},
            {label: 'View comments', command: 'view-comments'},
            {label: 'Share', command: 'share-image'}
        ]
    });
}

// Something
GalleryViewAssistant.prototype.popupHandler = function (command) {
  // do something with the incoming command 
}

// A flick to the right triggers a scroll to the left
GalleryViewAssistant.prototype.wentLeft = function (event) {
    if (this.image_model.current_index >= 1) {
        this.image_model.current_index -= 1;
        this.controller.modelChanged(this.image_model, this);
    }
}

// A flick to the left triggers a scroll to the right
GalleryViewAssistant.prototype.wentRight = function (event) {
    if (this.image_model.current_index < this.image_model.images.length - 1) {
        this.image_model.current_index += 1;
        this.controller.modelChanged(this.image_model, this);
    }
}

// Start listening to orientation changes
GalleryViewAssistant.prototype.activate = function () {
    this.controller.listen(this.controller.stageController.document, Mojo.Event.orientationChange, this.orientationChangeHandler);
}

// Stop listening to orientation changes
GalleryViewAssistant.prototype.deactivate = function () {
    this.controller.stopListening(this.controller.stageController.document, Mojo.Event.orientationChange, this.orientationChangeHandler);
}

// Cleanup anything we did in setup function
GalleryViewAssistant.prototype.cleanup = function () {
    Mojo.Event.stopListening(this.controller.get('imgurView'), Mojo.Event.imageViewChanged, this.imageViewChangedHandler);
    Mojo.Event.stopListening(this.controller.get('imgurView'), Mojo.Event.tap, this.showImageMenuHandler);
}

// This function will popup a dialog, displaying the message passed in.
GalleryViewAssistant.prototype.showDialogBox = function (title, message) {
    this.controller.showAlertDialog({
        onChoose: function (value) {},
        title: title,
        message: message,
        choices: [ {label:'OK', value:'OK', type:'color'} ]
    });
}

{'fraction_non_alphanumeric': 0.13286290322580646, 'fraction_numerical': 0.004704301075268817, 'mean_word_length': 3.462068965517241, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1871385', 'n_tokens_mistral': 4426, 'n_tokens_neox': 4295, 'n_words': 834}