~daker/webbrowser-app/fix.1317428

« back to all changes in this revision

Viewing changes to src/Ubuntu/Web/selection02.js

  • Committer: CI bot
  • Author(s): Olivier Tilloy
  • Date: 2014-06-30 08:11:35 UTC
  • mfrom: (574.2.13 selection)
  • Revision ID: ps-jenkins@lists.canonical.com-20140630081135-edips5hb81eprmm5
Re-enable contextual selection that had been disabled when switching to oxide.

Packaging change: renamed the qtdeclarative5-ubuntu-ui-extras-browser-plugin-assets package to qtdeclarative5-ubuntu-web-plugin-assets. Fixes: 1324292

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
17
 */
18
18
 
 
19
function elementContainedInBox(element, box) {
 
20
    var rect = element.getBoundingClientRect();
 
21
    return ((box.left <= rect.left) && (box.right >= rect.right) &&
 
22
            (box.top <= rect.top) && (box.bottom >= rect.bottom));
 
23
}
 
24
 
19
25
function getImgFullUri(uri) {
20
26
    if ((uri.slice(0, 7) === 'http://') ||
21
27
        (uri.slice(0, 8) === 'https://') ||
98
104
    return data;
99
105
}
100
106
 
 
107
function adjustSelection(selection) {
 
108
    // FIXME: allow selecting two consecutive blocks, instead of
 
109
    // interpolating to the containing block.
 
110
    var centerX = (selection.left + selection.right) / 2;
 
111
    var centerY = (selection.top + selection.bottom) / 2;
 
112
    var element = document.elementFromPoint(centerX, centerY);
 
113
    var parent = element;
 
114
    while (elementContainedInBox(parent, selection)) {
 
115
        parent = parent.parentNode;
 
116
    }
 
117
    element = parent;
 
118
    return getSelectedData(element);
 
119
}
 
120
 
101
121
document.documentElement.addEventListener('contextmenu', function(event) {
102
122
    var element = document.elementFromPoint(event.clientX, event.clientY);
103
123
    var data = getSelectedData(element);
104
124
    var w = document.defaultView;
105
125
    data['scaleX'] = w.outerWidth / w.innerWidth * w.devicePixelRatio;
106
126
    data['scaleY'] = w.outerHeight / w.innerHeight * w.devicePixelRatio;
107
 
    oxide.sendMessage('contextmenu', data);
 
127
    if (('img' in data) || ('href' in data)) {
 
128
        oxide.sendMessage('contextmenu', data);
 
129
    } else {
 
130
        oxide.sendMessage('selection', data);
 
131
    }
108
132
});
109
133
 
110
134
document.defaultView.addEventListener('scroll', function(event) {
111
135
    oxide.sendMessage('scroll', {});
112
136
});
 
137
 
 
138
oxide.addMessageHandler("adjustselection", function (msg) {
 
139
    var w = document.defaultView;
 
140
    var scaleX = w.outerWidth / w.innerWidth * w.devicePixelRatio;
 
141
    var scaleY = w.outerHeight / w.innerHeight * w.devicePixelRatio;
 
142
    var selection = new Object;
 
143
    selection.left = msg.args.x / scaleX;
 
144
    selection.right = selection.left + msg.args.width / scaleX;
 
145
    selection.top = msg.args.y / scaleY;
 
146
    selection.bottom = selection.top + msg.args.height / scaleY;
 
147
    var adjusted = adjustSelection(selection);
 
148
    adjusted['scaleX'] = scaleX;
 
149
    adjusted['scaleY'] = scaleY;
 
150
    msg.reply(adjusted);
 
151
});