Skip to content

Commit

Permalink
mouseDrag
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuehui Ye committed Nov 23, 2019
1 parent 4047fc4 commit cf3f919
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 12 deletions.
70 changes: 60 additions & 10 deletions file/file.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,65 @@
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
import { tilesInBBox } from "../quadtile";

element.style.display = 'none';
document.body.appendChild(element);
/**
* 文件类
*/
class Mfile {
static downloadText(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);

element.click();
element.style.display = 'none';
document.body.appendChild(element);

document.body.removeChild(element);
element.click();

document.body.removeChild(element);
}

/**
*
* @param {*Sting} rows
*/
static rows2csv(rows) {
var csvFile = rows.reduce(function (finalVal, row) {
for (var j = 0; j < row.length; j++) {
var innerValue = row[j] === null ? '' : row[j].toString();
if (row[j] instanceof Date) {
innerValue = row[j].toLocaleString();
};
var result = innerValue.replace(/"/g, '""');
if (result.search(/("|,|\n)/g) >= 0)
result = '"' + result + '"';
if (j > 0)
finalVal += ',';
finalVal += result;
}
return finalVal + '\n';
}, '')
return csvFile;
}

/**
*
* @param {*} filename
* @param {*} rows
* @param {Array} titles 提供标题栏
*/
static downloadRows(filename, rows, titles = null) {
if (rows.length > 0) {
if (!(rows[0] instanceof Array)) {
var newRows = titles ? [titles] : [Object.keys(rows[0])];
rows.map((row) => {
newRows.push(Object.values(row))
})
rows = newRows
} else if (titles) {
rows.unshift(titles)
}
this.downloadText(filename, this.rows2csv(rows))
}
}
}

// Start file download.
download("hello.txt","This is the content of my file :)");
export default Mfile
4 changes: 2 additions & 2 deletions util/mouseDrag.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class MouseDrag {
}

/**
* MouseDrag.onDragMove(ele, (v,b)=>{
* MouseDrag.onDragMove(ele, (b)=>{
ele.style.top=(ele.offsetTop+b.y)+'px';
ele.style.left=(ele.offsetLeft+b.x)+'px';
})
Expand All @@ -27,7 +27,7 @@ export default class MouseDrag {
ele.onmousemove = e=>{
let x = e.clientX-oldX, y = e.clientY-oldY;
oldX = e.clientX, oldY = e.clientY;
return callback(ele, {x, y})
return callback({x, y})
}
ele.onmouseup =e=>{
ele.onmousemove = null;
Expand Down

0 comments on commit cf3f919

Please sign in to comment.