<commit_before>###########################################################
#
# Copyright (c) 2005-2012, Southpaw Technology
#                     All Rights Reserved
#
# PROPRIETARY INFORMATION.  This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#


__all__ = ['Html5UploadWdg', 'UploadButtonWdg', 'CheckinButtonWdg', 'TestHtml5UploadWdg']

from tactic.ui.common import BaseRefreshWdg

from pyasm.common import Common
from pyasm.web import HtmlElement, DivWdg
from pyasm.widget import TextWdg, HiddenWdg


class Html5UploadWdg(BaseRefreshWdg):

    def init(my):
        name = my.kwargs.get("name")
        if not name:
            name = "table_upload"

        my.form = HtmlElement.form()

        my.form_id = my.kwargs.get("upload_id")
        if my.form_id:
            my.form.set_id(my.form_id)
        else:
            my.form_id = my.form.set_unique_id(name)


    def get_upload_id(my):
        return my.form_id

    def get_display(my):

        top = my.top
        form = my.form
        form.add_style("margin: 0px")
        form.add_style("padding: 0px")
        top.add(form)


        input = HtmlElement.input()
        form.add(input)
        input.set_attr("name", "file")
        input.add_class("spt_file")
        input.set_attr("type", "file")
        input.add_style("position: absolute")
        input.add_style("margin-left: -5000px")

        multiple = my.kwargs.get("multiple")
        if multiple in [True, 'true']:
            input.add_attr("multiple", "multiple")

        from pyasm.common import Container
        if not Container.get_dict("JSLibraries", "spt_html5upload"):
            form.add_behavior( {
            'type': 'load',
            'form_id': my.form_id,
            'cbjs_action': '''

if (spt.html5upload)
    return;

spt.Environment.get().add_library("spt_html5upload")

spt.html5upload = {};
spt.html5upload.form = $(bvr.form_id);
spt.html5upload.files = [];
spt.html5upload.events = {};
spt.html5upload.ticket;

spt.html5upload.set_form = function(form) {
    if (!form) {
        spt.alert('Cannot initialize the HTML upload. Invalid form detected.');
        return;
    }
    spt.html5upload.form = form;
}

// get the last one in the list since this FileList is readonly and it is additive if multiple attr is on
spt.html5upload.get_file = function() {
    var files = spt.html5upload.get_files();
    if (files.length == 0) {
        return null;
    }
    return files[files.length-1];
}


//FIXME: it doesn't need to be stored as files since it should be called
// every time an upload occurs
spt.html5upload.get_files = function() {
    var file_input = spt.html5upload.form.getElement('.spt_file');
    spt.html5upload.files = file_input.files;
    return spt.html5upload.files;
}


spt.html5upload.select_file = function(onchange) {
    var files = spt.html5upload.select_files(onchange);
    if (!files) {
        spt.alert('You may need to refresh this page.');
        return null;
    }
        
    if (files.length == 0) {
        return null;
    }
    return files[0];
}


spt.html5upload.select_files = function(onchange) {
    var form = spt.html5upload.form;
    if (!form) {
        spt.alert('Cannot locate the upload form. Refresh this page/tab and try again');
        return; 
    }
    var el = form.getElement(".spt_file") ;
  
    /*
    if (replace) {
        spt.html5upload.events['select_file'] = null;
        el.removeEventListener("change", onchange);
        alert('remove')
    }
    */
   
    var event_name = 'select_file';
    // ensure this listener is only added once
    if (!spt.html5upload.events[event_name]){
     
        el.addEventListener("change", onchange, true);
    }

    // This is necessary for Qt on a Mac??
    if (spt.browser.is_Qt() || spt.browser.is_Safari()) {<commit_msg>BRANCH 4.5: fixed preview button click on safari<commit_after>###########################################################
#
# Copyright (c) 2005-2012, Southpaw Technology
#                     All Rights Reserved
#
# PROPRIETARY INFORMATION.  This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#


__all__ = ['Html5UploadWdg', 'UploadButtonWdg', 'CheckinButtonWdg', 'TestHtml5UploadWdg']

from tactic.ui.common import BaseRefreshWdg

from pyasm.common import Common
from pyasm.web import HtmlElement, DivWdg
from pyasm.widget import TextWdg, HiddenWdg


class Html5UploadWdg(BaseRefreshWdg):

    def init(my):
        name = my.kwargs.get("name")
        if not name:
            name = "table_upload"

        my.form = HtmlElement.form()

        my.form_id = my.kwargs.get("upload_id")
        if my.form_id:
            my.form.set_id(my.form_id)
        else:
            my.form_id = my.form.set_unique_id(name)


    def get_upload_id(my):
        return my.form_id

    def get_display(my):

        top = my.top
        form = my.form
        form.add_style("margin: 0px")
        form.add_style("padding: 0px")
        top.add(form)


        input = HtmlElement.input()
        form.add(input)
        input.set_attr("name", "file")
        input.add_class("spt_file")
        input.set_attr("type", "file")
        input.add_style("position: absolute")
        input.add_style("margin-left: -5000px")

        multiple = my.kwargs.get("multiple")
        if multiple in [True, 'true']:
            input.add_attr("multiple", "multiple")

        from pyasm.common import Container
        if not Container.get_dict("JSLibraries", "spt_html5upload"):
            form.add_behavior( {
            'type': 'load',
            'form_id': my.form_id,
            'cbjs_action': '''

if (spt.html5upload)
    return;

spt.Environment.get().add_library("spt_html5upload")

spt.html5upload = {};
spt.html5upload.form = $(bvr.form_id);
spt.html5upload.files = [];
spt.html5upload.events = {};
spt.html5upload.ticket;

spt.html5upload.set_form = function(form) {
    if (!form) {
        spt.alert('Cannot initialize the HTML upload. Invalid form detected.');
        return;
    }
    spt.html5upload.form = form;
}

// get the last one in the list since this FileList is readonly and it is additive if multiple attr is on
spt.html5upload.get_file = function() {
    var files = spt.html5upload.get_files();
    if (files.length == 0) {
        return null;
    }
    return files[files.length-1];
}


//FIXME: it doesn't need to be stored as files since it should be called
// every time an upload occurs
spt.html5upload.get_files = function() {
    var file_input = spt.html5upload.form.getElement('.spt_file');
    spt.html5upload.files = file_input.files;
    return spt.html5upload.files;
}


spt.html5upload.select_file = function(onchange) {
    var files = spt.html5upload.select_files(onchange);
    if (!files) {
        spt.alert('You may need to refresh this page.');
        return null;
    }
        
    if (files.length == 0) {
        return null;
    }
    return files[0];
}


spt.html5upload.select_files = function(onchange) {
    var form = spt.html5upload.form;
    if (!form) {
        spt.alert('Cannot locate the upload form. Refresh this page/tab and try again');
        return; 
    }
    var el = form.getElement(".spt_file") ;
  
    /*
    if (replace) {
        spt.html5upload.events['select_file'] = null;
        el.removeEventListener("change", onchange);
        alert('remove')
    }
    */
   
    var event_name = 'select_file';
    // ensure this listener is only added once
    if (!spt.html5upload.events[event_name]){
     
        el.addEventListener("change", onchange, true);
    }

    // This is necessary for Qt on a Mac??
    if (spt.browser.is_Qt()) {
{'fraction_non_alphanumeric': 0.13404962906114096, 'fraction_numerical': 0.011256075722691225, 'mean_word_length': 2.6384364820846904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4377029', 'n_tokens_mistral': 2597, 'n_tokens_neox': 2491, 'n_words': 588}